Skip to content

Commit eba888a

Browse files
solves #2587: Split With Minimum Sum in java
1 parent b4e6650 commit eba888a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@
804804
| 2566 | [Maximum Difference by Remapping a Digit](https://leetcode.com/problems/maximum-difference-by-remapping-a-digit) | [![Java](assets/java.png)](src/MaximumDifferenceByRemappingADigit.java) | |
805805
| 2570 | [Merge Two 2D Arrays by Summing Values](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values) | [![Java](assets/java.png)](src/MergeTwo2DArraysBySummingValues.java) | |
806806
| 2574 | [Left and Right Sum Differences](https://leetcode.com/problems/left-and-right-sum-differences) | [![Java](assets/java.png)](src/LeftAndRightSumDifferences.java) | |
807-
| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum) | | |
807+
| 2578 | [Split With Minimum Sum](https://leetcode.com/problems/split-with-minimum-sum) | [![Java](assets/java.png)](src/SplitWithMinimumSum.java) | |
808808
| 2582 | [Pass the Pillow](https://leetcode.com/problems/pass-the-pillow) | | |
809809
| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range) | | |
810810
| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children) | | |

src/SplitWithMinimumSum.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://leetcode.com/problems/split-with-minimum-sum
2+
// T: O(log(N)log(log(N)))
3+
// S: O(log(log(N)))
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class SplitWithMinimumSum {
9+
public int splitNum(int num) {
10+
final List<Integer> digits = getDigits(num);
11+
digits.sort(Integer::compareTo);
12+
int a = 0, b = 0, k = 0;
13+
for (int digit : digits) {
14+
if (k == 0) {
15+
a = 10 * a + digit;
16+
} else {
17+
b = 10 * b + digit;
18+
}
19+
k ^= 1;
20+
}
21+
return a + b;
22+
}
23+
24+
private List<Integer> getDigits(int x) {
25+
final List<Integer> digits = new ArrayList<>();
26+
final String number = x + "";
27+
for (int index = 0 ; index < number.length() ; index++) {
28+
digits.add(number.charAt(index) - '0');
29+
}
30+
return digits;
31+
}
32+
}

0 commit comments

Comments
 (0)