-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution410.java
43 lines (35 loc) · 969 Bytes
/
Solution410.java
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
33
34
35
36
37
38
39
40
41
42
43
package leetcode.binarysearch.greedyalgorithm;
import java.util.Arrays;
public class Solution410 {
public static void main(String[] args) {
// 4
System.out.println(new Solution410().splitArray(new int[]{1, 4, 4}, 3));
}
public int splitArray(int[] nums, int m) {
int left = nums[0];
int right = 0;
for (int num : nums) {
right += num;
left = Math.max(num, left);
}
while (left < right) {
int mid = left + right >> 1;
int tempM = 1;
int sum = 0;
for (int num : nums) {
if (sum + num <= mid) {
sum += num;
} else {
tempM++;
sum = num;
}
}
if (tempM > m) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}