-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSplitWithMinimumSum.java
More file actions
32 lines (29 loc) · 879 Bytes
/
SplitWithMinimumSum.java
File metadata and controls
32 lines (29 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// https://leetcode.com/problems/split-with-minimum-sum
// T: O(log(N)log(log(N)))
// S: O(log(log(N)))
import java.util.ArrayList;
import java.util.List;
public class SplitWithMinimumSum {
public int splitNum(int num) {
final List<Integer> digits = getDigits(num);
digits.sort(Integer::compareTo);
int a = 0, b = 0, k = 0;
for (int digit : digits) {
if (k == 0) {
a = 10 * a + digit;
} else {
b = 10 * b + digit;
}
k ^= 1;
}
return a + b;
}
private List<Integer> getDigits(int x) {
final List<Integer> digits = new ArrayList<>();
final String number = x + "";
for (int index = 0 ; index < number.length() ; index++) {
digits.add(number.charAt(index) - '0');
}
return digits;
}
}