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 c03e540 commit 9d17101Copy full SHA for 9d17101
kotlin/0410-split-array-largest-sum.kt
@@ -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