commit 687a03dd4ef45ec32e59e9b9d806e905f11d8c0e parent b66efa29831c3307701382f89fba4812b7aa9ce8 Author: AndrewLockVI <andrewlaack1@gmail.com> Date: Mon, 8 May 2023 15:28:04 -0500 Completed minimum sum problem using dart Diffstat:
| A | minimum-sum-of-four-digit-number-after-splitting-digits/minimum-sum.dart | | | 31 | +++++++++++++++++++++++++++++++ |
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/minimum-sum-of-four-digit-number-after-splitting-digits/minimum-sum.dart b/minimum-sum-of-four-digit-number-after-splitting-digits/minimum-sum.dart @@ -0,0 +1,31 @@ +//Find the minimum sum of a four digit number where the sum +//is the total of two seperate groups of digits contained in the number. +//I solved this problem in this way with string to int to allow +//for this algorithm to work with any length of number that is used as the input +//which would make it better for expandability in the future. +//The time complexity of this code is O(n) where n is the number +//of digits in the input. +//Time: 244ms Beats: 93.33% +//Memory: 140.1MB Beats: 100% + +class Solution { + int minimumSum(int num) { + List<int> digits = []; + while(num > 0){ + digits.add(num % 10); + num = (num / 10).toInt(); + + } + digits.sort(); + print(digits); + String first = ""; + String second = ""; + + for(int i = 0 ; i < digits.length ; i += 2){ + first += digits[i].toString(); + second += digits[i + 1].toString(); + } + + return int.parse(first) + int.parse(second); + } +}