-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
37 lines (35 loc) · 865 Bytes
/
solution.ts
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
function splitArray (nums: number[], k: number): number {
let low = 0;
let high = nums.reduce((acc, item) => acc + item, 0);
let res = 0;
while (low <= high) {
const mid = (low + high) >> 1;
const current = calc(nums, mid);
if (current > k) {
low = mid + 1;
} else {
res = mid;
high = mid - 1;
}
}
return res;
}
function calc (nums:number[], limit:number) {
let res = 0;
let index = 0;
while (index < nums.length) {
let sum = 0;
while (index < nums.length) {
if (nums[index] > limit) {
return Infinity;
}
if (sum + nums[index] > limit) {
break;
} else {
sum += nums[index++];
}
}
res++;
}
return res;
}