File tree Expand file tree Collapse file tree 3 files changed +103
-0
lines changed
solution/0400-0499/0410.Split Array Largest Sum Expand file tree Collapse file tree 3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -190,6 +190,42 @@ func splitArray(nums []int, k int) int {
190
190
}
191
191
```
192
192
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
+
193
229
#### TypeScript
194
230
195
231
``` ts
Original file line number Diff line number Diff line change @@ -184,6 +184,42 @@ func splitArray(nums []int, k int) int {
184
184
}
185
185
```
186
186
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
+
187
223
#### TypeScript
188
224
189
225
``` ts
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments