We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 32f1346 commit 1cf3db7Copy full SHA for 1cf3db7
solution/0410.Split Array Largest Sum/Solution.java
@@ -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
20
+ tot += x;
21
+ if (tot > cap) {
22
+ ++cnt;
23
+ tot = x;
24
25
26
+ return cnt <= m;
27
28
+}
0 commit comments