Skip to content

Commit 9d17101

Browse files
authored
Create 0410-split-array-largest-sum.kt
1 parent c03e540 commit 9d17101

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
fun splitArray(nums: IntArray, k: Int): Int {
3+
4+
fun canSplit(max: Int): Boolean {
5+
var subArrCnt = 1
6+
var curSum = 0
7+
for (n in nums) {
8+
curSum += n
9+
if (curSum > max) {
10+
subArrCnt++
11+
curSum = n
12+
}
13+
}
14+
return subArrCnt <= k
15+
}
16+
17+
var l = nums.max()!!
18+
var r = nums.sum()!!
19+
var res = r
20+
while (l <= r) {
21+
val m = l + (r - l) / 2
22+
if (canSplit(m)) {
23+
res = m
24+
r = m - 1
25+
} else {
26+
l = m + 1
27+
}
28+
}
29+
30+
return res
31+
}
32+
}

0 commit comments

Comments
 (0)