-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc_410.java
More file actions
56 lines (42 loc) · 1.2 KB
/
Lc_410.java
File metadata and controls
56 lines (42 loc) · 1.2 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
//410. Split Array Largest Sum
/*
* find the min and max subarray sum
* the max and min subarray sum would be the range of the search
* */
import java.util.Arrays;
public class Lc_410 {
public static void main(String[] args) {
int[] nums = {7,2,5,10,8};
System.out.println(splitArray(nums, 5));;
}
static int splitArray(int[] nums, int s){
int start = nums[0], end = 0;
for(int num : nums){
start = Math.max(start, num);
end = end + num;
}
while(start < end){
int mid = start + (end - start)/2;
int sum = 0, slice = 1;
for(int num : nums){
//if sum is more than mid, move to next slice and reset sum to current number
if(sum + num > mid){
sum = num;
slice++;
}
//if sum is less than mid continue addition
else{
sum += num;
}
}
// check for slice
if(slice > s){
start = mid +1;
}
else{
end = mid;
}
}
return end;
}
}