Skip to content

Commit 1cf3db7

Browse files
authored
Create Solution.java
1 parent 32f1346 commit 1cf3db7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int splitArray(int[] nums, int m) {
3+
long l = 0, r = 0;
4+
for (int x : nums) {
5+
l = Math.max(l, x);
6+
r += x;
7+
}
8+
while (l < r) {
9+
long mid = l + r >>> 1;
10+
if (check(nums, m, mid)) r = mid;
11+
else l = mid + 1;
12+
}
13+
return (int) r;
14+
}
15+
16+
private boolean check(int[] nums, int m, long cap) {
17+
int cnt = 1;
18+
long tot = 0;
19+
for (int x : nums) {
20+
tot += x;
21+
if (tot > cap) {
22+
++cnt;
23+
tot = x;
24+
}
25+
}
26+
return cnt <= m;
27+
}
28+
}

0 commit comments

Comments
 (0)