Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d56c976

Browse files
authored
feat: add solutions to lc problem: No.0410 (doocs#3212)
1 parent 9cef036 commit d56c976

File tree

4 files changed

+139
-45
lines changed

4 files changed

+139
-45
lines changed

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

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,37 +190,70 @@ func splitArray(nums []int, k int) int {
190190
}
191191
```
192192

193+
#### JavaScript
194+
195+
```js
196+
/**
197+
* @param {number[]} nums
198+
* @param {number} k
199+
* @return {number}
200+
*/
201+
var splitArray = function (nums, k) {
202+
let l = Math.max(...nums);
203+
let r = nums.reduce((a, b) => a + b);
204+
205+
const check = mx => {
206+
let [s, cnt] = [0, 0];
207+
for (const x of nums) {
208+
s += x;
209+
if (s > mx) {
210+
s = x;
211+
if (++cnt === k) return false;
212+
}
213+
}
214+
return true;
215+
};
216+
217+
while (l < r) {
218+
const mid = (l + r) >> 1;
219+
if (check(mid)) {
220+
r = mid;
221+
} else {
222+
l = mid + 1;
223+
}
224+
}
225+
return l;
226+
};
227+
```
228+
193229
#### TypeScript
194230

195231
```ts
196232
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-
}
233+
let l = Math.max(...nums);
234+
let r = nums.reduce((a, b) => a + b);
235+
203236
const check = (mx: number) => {
204-
let s = 1 << 30;
205-
let cnt = 0;
237+
let [s, cnt] = [0, 0];
206238
for (const x of nums) {
207239
s += x;
208240
if (s > mx) {
209241
s = x;
210-
++cnt;
242+
if (++cnt===k) returnfalse;
211243
}
212244
}
213-
return cnt<=k;
245+
return true;
214246
};
215-
while (left < right) {
216-
const mid = (left + right) >> 1;
247+
248+
while (l < r) {
249+
const mid = (l + r) >> 1;
217250
if (check(mid)) {
218-
right = mid;
251+
r = mid;
219252
} else {
220-
left = mid + 1;
253+
l = mid + 1;
221254
}
222255
}
223-
return left;
256+
return l;
224257
}
225258
```
226259

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

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,37 +184,70 @@ func splitArray(nums []int, k int) int {
184184
}
185185
```
186186

187+
#### JavaScript
188+
189+
```js
190+
/**
191+
* @param {number[]} nums
192+
* @param {number} k
193+
* @return {number}
194+
*/
195+
var splitArray = function (nums, k) {
196+
let l = Math.max(...nums);
197+
let r = nums.reduce((a, b) => a + b);
198+
199+
const check = mx => {
200+
let [s, cnt] = [0, 0];
201+
for (const x of nums) {
202+
s += x;
203+
if (s > mx) {
204+
s = x;
205+
if (++cnt === k) return false;
206+
}
207+
}
208+
return true;
209+
};
210+
211+
while (l < r) {
212+
const mid = (l + r) >> 1;
213+
if (check(mid)) {
214+
r = mid;
215+
} else {
216+
l = mid + 1;
217+
}
218+
}
219+
return l;
220+
};
221+
```
222+
187223
#### TypeScript
188224

189225
```ts
190226
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-
}
227+
let l = Math.max(...nums);
228+
let r = nums.reduce((a, b) => a + b);
229+
197230
const check = (mx: number) => {
198-
let s = 1 << 30;
199-
let cnt = 0;
231+
let [s, cnt] = [0, 0];
200232
for (const x of nums) {
201233
s += x;
202234
if (s > mx) {
203235
s = x;
204-
++cnt;
236+
if (++cnt===k) returnfalse;
205237
}
206238
}
207-
return cnt<=k;
239+
return true;
208240
};
209-
while (left < right) {
210-
const mid = (left + right) >> 1;
241+
242+
while (l < r) {
243+
const mid = (l + r) >> 1;
211244
if (check(mid)) {
212-
right = mid;
245+
r = mid;
213246
} else {
214-
left = mid + 1;
247+
l = mid + 1;
215248
}
216249
}
217-
return left;
250+
return l;
218251
}
219252
```
220253

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var splitArray = function (nums, k) {
7+
let l = Math.max(...nums);
8+
let r = nums.reduce((a, b) => a + b);
9+
10+
const check = mx => {
11+
let [s, cnt] = [0, 0];
12+
for (const x of nums) {
13+
s += x;
14+
if (s > mx) {
15+
s = x;
16+
if (++cnt === k) return false;
17+
}
18+
}
19+
return true;
20+
};
21+
22+
while (l < r) {
23+
const mid = (l + r) >> 1;
24+
if (check(mid)) {
25+
r = mid;
26+
} else {
27+
l = mid + 1;
28+
}
29+
}
30+
return l;
31+
};
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)returnfalse;
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)

AltStyle によって変換されたページ (->オリジナル) /