Skip to content

Commit 9075b0d

Browse files
committed
feat: update ts solution to lc problem: No.0410
1 parent 9cef036 commit 9075b0d

File tree

3 files changed

+36
-45
lines changed

3 files changed

+36
-45
lines changed

solution/0400-0499/0410.Split Array Largest Sum/README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,33 +194,30 @@ func splitArray(nums []int, k int) int {
194194

195195
```ts
196196
function splitArray(nums: number[], k: number): number {
197-
let left = 0;
198-
let right = 0;
199-
for (const x of nums) {
200-
left = Math.max(left, x);
201-
right += x;
202-
}
197+
let l = Math.max(...nums);
198+
let r = nums.reduce((a, b) => a + b);
199+
203200
const check = (mx: number) => {
204-
let s = 1 << 30;
205-
let cnt = 0;
201+
let [s, cnt] = [0, 0];
206202
for (const x of nums) {
207203
s += x;
208204
if (s > mx) {
209205
s = x;
210-
++cnt;
206+
if (++cnt === k) return false;
211207
}
212208
}
213-
return cnt <= k;
209+
return true;
214210
};
215-
while (left < right) {
216-
const mid = (left + right) >> 1;
211+
212+
while (l < r) {
213+
const mid = (l + r) >> 1;
217214
if (check(mid)) {
218-
right = mid;
215+
r = mid;
219216
} else {
220-
left = mid + 1;
217+
l = mid + 1;
221218
}
222219
}
223-
return left;
220+
return l;
224221
}
225222
```
226223

solution/0400-0499/0410.Split Array Largest Sum/README_EN.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,30 @@ func splitArray(nums []int, k int) int {
188188

189189
```ts
190190
function splitArray(nums: number[], k: number): number {
191-
let left = 0;
192-
let right = 0;
193-
for (const x of nums) {
194-
left = Math.max(left, x);
195-
right += x;
196-
}
191+
let l = Math.max(...nums);
192+
let r = nums.reduce((a, b) => a + b);
193+
197194
const check = (mx: number) => {
198-
let s = 1 << 30;
199-
let cnt = 0;
195+
let [s, cnt] = [0, 0];
200196
for (const x of nums) {
201197
s += x;
202198
if (s > mx) {
203199
s = x;
204-
++cnt;
200+
if (++cnt === k) return false;
205201
}
206202
}
207-
return cnt <= k;
203+
return true;
208204
};
209-
while (left < right) {
210-
const mid = (left + right) >> 1;
205+
206+
while (l < r) {
207+
const mid = (l + r) >> 1;
211208
if (check(mid)) {
212-
right = mid;
209+
r = mid;
213210
} else {
214-
left = mid + 1;
211+
l = mid + 1;
215212
}
216213
}
217-
return left;
214+
return l;
218215
}
219216
```
220217

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
function splitArray(nums: number[], k: number): number {
2-
let left = 0;
3-
let right = 0;
4-
for (const x of nums) {
5-
left = Math.max(left, x);
6-
right += x;
7-
}
2+
let l = Math.max(...nums);
3+
let r = nums.reduce((a, b) => a + b);
4+
85
const check = (mx: number) => {
9-
let s = 1 << 30;
10-
let cnt = 0;
6+
let [s, cnt] = [0, 0];
117
for (const x of nums) {
128
s += x;
139
if (s > mx) {
1410
s = x;
15-
++cnt;
11+
if (++cnt === k) return false;
1612
}
1713
}
18-
return cnt <= k;
14+
return true;
1915
};
20-
while (left < right) {
21-
const mid = (left + right) >> 1;
16+
17+
while (l < r) {
18+
const mid = (l + r) >> 1;
2219
if (check(mid)) {
23-
right = mid;
20+
r = mid;
2421
} else {
25-
left = mid + 1;
22+
l = mid + 1;
2623
}
2724
}
28-
return left;
25+
return l;
2926
}

0 commit comments

Comments
 (0)