diff --git a/solution/1500-1599/1562.Find Latest Group of Size M/README.md b/solution/1500-1599/1562.Find Latest Group of Size M/README.md index ecbb32c70a1de..bd014c3712076 100644 --- a/solution/1500-1599/1562.Find Latest Group of Size M/README.md +++ b/solution/1500-1599/1562.Find Latest Group of Size M/README.md @@ -289,6 +289,28 @@ func findLatestStep(arr []int, m int) int { } ``` +#### JavaScript + +```javascript +const findLatestStep = function (arr, m) { + let result = -1; + const len = arr.length; + const reOnes = new RegExp(`\\b1{${m}}\\b`); + arr.reduce( + (accum, item, iIndex) => { + accum[item - 1] = '1'; + iIndex++; + if (iIndex >= m && reOnes.test(accum.join(''))) { + result = iIndex; + } + return accum; + }, + [...' '.repeat(len)], + ); + return result; +}; +``` + diff --git a/solution/1500-1599/1562.Find Latest Group of Size M/README_EN.md b/solution/1500-1599/1562.Find Latest Group of Size M/README_EN.md index 871c03a645257..eabedfb4f465c 100644 --- a/solution/1500-1599/1562.Find Latest Group of Size M/README_EN.md +++ b/solution/1500-1599/1562.Find Latest Group of Size M/README_EN.md @@ -269,6 +269,28 @@ func findLatestStep(arr []int, m int) int { } ``` +#### JavaScript + +```javascript +const findLatestStep = function (arr, m) { + let result = -1; + const len = arr.length; + const reOnes = new RegExp(`\\b1{${m}}\\b`); + arr.reduce( + (accum, item, iIndex) => { + accum[item - 1] = '1'; + iIndex++; + if (iIndex >= m && reOnes.test(accum.join(''))) { + result = iIndex; + } + return accum; + }, + [...' '.repeat(len)], + ); + return result; +}; +``` + diff --git a/solution/1500-1599/1562.Find Latest Group of Size M/Solution.js b/solution/1500-1599/1562.Find Latest Group of Size M/Solution.js new file mode 100644 index 0000000000000..79c6d3e0247dd --- /dev/null +++ b/solution/1500-1599/1562.Find Latest Group of Size M/Solution.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} arr + * @param {number} m + * @return {number} + */ +const findLatestStep = function (arr, m) { + let result = -1; + const len = arr.length; + const reOnes = new RegExp(`\\b1{${m}}\\b`); + arr.reduce( + (accum, item, iIndex) => { + accum[item - 1] = '1'; + iIndex++; + if (iIndex >= m && reOnes.test(accum.join(''))) { + result = iIndex; + } + return accum; + }, + [...' '.repeat(len)], + ); + return result; +};