-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
54 lines (53 loc) · 1.1 KB
/
Copy pathsolution.java
File metadata and controls
54 lines (53 loc) · 1.1 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
// 410. Split Array Largest Sum
// https://leetcode.com/problems/split-array-largest-sum/
// Hard | Java | Accepted 2026-08-18
// Runtime 0 ms | Memory 43 MB
class Solution {
int[] num;
int m;
public int splitArray(int[] nums, int k) {
m = k;
num = nums;
int max = 0;
int sum = 0;
for(int i : nums)
{
max = Math.max(max, i);
sum+=i;
}
int l = max;
int r = sum;
int ans = 0;
while(l<=r)
{
int mid = l + (r-l)/2;
if(canSplit(mid))
{
ans = mid;
r = mid-1;
}
else
{
l = mid+1;
}
}
return ans;
}
public boolean canSplit(int mid)
{
int i = 0;
int count = 0;
int k = 1;
while(i<num.length)
{
if(count+num[i]>mid)
{
count = 0;
k++;
}
count+=num[i];
i++;
}
return k<=m ? true : false;
}
}