minimum-sum.dart (960B)
1 //Find the minimum sum of a four digit number where the sum 2 //is the total of two seperate groups of digits contained in the number. 3 //I solved this problem in this way with string to int to allow 4 //for this algorithm to work with any length of number that is used as the input 5 //which would make it better for expandability in the future. 6 //The time complexity of this code is O(n) where n is the number 7 //of digits in the input. 8 //Time: 244ms Beats: 93.33% 9 //Memory: 140.1MB Beats: 100% 10 11 class Solution { 12 int minimumSum(int num) { 13 List<int> digits = []; 14 while(num > 0){ 15 digits.add(num % 10); 16 num = (num / 10).toInt(); 17 18 } 19 digits.sort(); 20 print(digits); 21 String first = ""; 22 String second = ""; 23 24 for(int i = 0 ; i < digits.length ; i += 2){ 25 first += digits[i].toString(); 26 second += digits[i + 1].toString(); 27 } 28 29 return int.parse(first) + int.parse(second); 30 } 31 }