From f2912cab0ff3d62391195ab415dd81665dab1970 Mon Sep 17 00:00:00 2001 From: Jaran F Date: Mon, 8 Jul 2024 17:33:28 +0000 Subject: [PATCH 1/2] feat: add JS solution to lc problem: No.1562 --- .../README.md | 19 +++++++++++++++++++ .../README_EN.md | 19 +++++++++++++++++++ .../Solution.js | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 solution/1500-1599/1562.Find Latest Group of Size M/Solution.js 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..bbbd0837e8652 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,25 @@ 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..a96c13cd5c127 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,25 @@ 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..7bc97ae3ab125 --- /dev/null +++ b/solution/1500-1599/1562.Find Latest Group of Size M/Solution.js @@ -0,0 +1,19 @@ +/** + * @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 +} From 54c9a596df759bec4d1644397d6cea6c9914521f Mon Sep 17 00:00:00 2001 From: jaranF Date: Mon, 8 Jul 2024 17:26:24 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md | 23 +++++++++++-------- .../README_EN.md | 23 +++++++++++-------- .../Solution.js | 23 +++++++++++-------- 3 files changed, 39 insertions(+), 30 deletions(-) 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 bbbd0837e8652..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 @@ -296,16 +296,19 @@ 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 -} + 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 a96c13cd5c127..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 @@ -276,16 +276,19 @@ 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 -} + 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 index 7bc97ae3ab125..79c6d3e0247dd 100644 --- 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 @@ -7,13 +7,16 @@ 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 -} + 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; +};