1
class Solution {
2
int[] nums;
3

4
public int splitArray(int[] nums, int m) {
5
this.nums = nums;
6
int low = 0, high = 0, min = Integer.MAX_VALUE;
7
for (int i = 0; i < nums.length; i++) {
8
low = Math.max(low, nums[i]);
9
high += nums[i];
10
}
11
while (low <= high) {
12
int mid = (low + high) / 2;
13
if (required_no_of_chunks(mid, m)) {
14
min = Math.min(min, mid);
15
high = mid - 1;
16
} else low = mid + 1;
17
}
18
return min;
19
}
20

21
private boolean required_no_of_chunks(int mid, int m) {
22
int chunks = 0, i = 0;
23
while (i < nums.length) {
24
int val = 0;
25
while (i < nums.length && nums[i] + val <= mid) val += nums[i++];
26
chunks++;
27
}
28
return chunks <= m;
29
}
30
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0