Skip to content

Commit f2912ca

Browse files
author
Jaran F
committed
feat: add JS solution to lc problem: No.1562
1 parent b0f757b commit f2912ca

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

solution/1500-1599/1562.Find Latest Group of Size M/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,25 @@ func findLatestStep(arr []int, m int) int {
289289
}
290290
```
291291

292+
#### JavaScript
293+
294+
```javascript
295+
const findLatestStep = function (arr, m) {
296+
let result = -1;
297+
const len = arr.length;
298+
const reOnes = new RegExp(`\\b1{${m}}\\b`);
299+
arr.reduce( (accum, item, iIndex) => {
300+
accum[item-1] = '1';
301+
iIndex++;
302+
if (iIndex >= m && reOnes.test(accum.join(''))) {
303+
result = iIndex;
304+
}
305+
return accum
306+
}, [...(" ".repeat(len))] );
307+
return result
308+
}
309+
```
310+
292311
<!-- tabs:end -->
293312

294313
<!-- solution:end -->

solution/1500-1599/1562.Find Latest Group of Size M/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ func findLatestStep(arr []int, m int) int {
269269
}
270270
```
271271

272+
#### JavaScript
273+
274+
```javascript
275+
const findLatestStep = function (arr, m) {
276+
let result = -1;
277+
const len = arr.length;
278+
const reOnes = new RegExp(`\\b1{${m}}\\b`);
279+
arr.reduce( (accum, item, iIndex) => {
280+
accum[item-1] = '1';
281+
iIndex++;
282+
if (iIndex >= m && reOnes.test(accum.join(''))) {
283+
result = iIndex;
284+
}
285+
return accum
286+
}, [...(" ".repeat(len))] );
287+
return result
288+
}
289+
```
290+
272291
<!-- tabs:end -->
273292

274293
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} arr
3+
* @param {number} m
4+
* @return {number}
5+
*/
6+
const findLatestStep = function (arr, m) {
7+
let result = -1;
8+
const len = arr.length;
9+
const reOnes = new RegExp(`\\b1{${m}}\\b`);
10+
arr.reduce( (accum, item, iIndex) => {
11+
accum[item-1] = '1';
12+
iIndex++;
13+
if (iIndex >= m && reOnes.test(accum.join(''))) {
14+
result = iIndex;
15+
}
16+
return accum
17+
}, [...(" ".repeat(len))] );
18+
return result
19+
}

0 commit comments

Comments
 (0)