Skip to content

Commit cbb4b43

Browse files
committed
Time: 2 ms (32.4%), Space: 42.4 MB (99.08%) - LeetHub
1 parent e80f483 commit cbb4b43

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int splitArray(int[] nums, int k) {
3+
int start = 0;
4+
int end = 0;
5+
for (int i = 0; i < nums.length; i++) {
6+
start = Math.max(start, nums[i]);
7+
end += nums[i];
8+
}
9+
while (start < end) {
10+
int mid = start + (end - start) / 2;
11+
int sum = 0;
12+
int pieces = 1;
13+
for (int num : nums) {
14+
if (sum + num > mid) {
15+
sum = num;
16+
pieces++;
17+
} else {
18+
sum += num;
19+
}
20+
}
21+
if (pieces <= k) {
22+
end = mid;
23+
} else {
24+
start = mid + 1;
25+
}
26+
}
27+
return end;
28+
}
29+
}

0 commit comments

Comments
 (0)