From 38744d13633568eb49304bf4bdaa76e8f02ce0b2 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 28 Oct 2024 23:47:24 +0300 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2501 --- .../README.md | 90 +++++++++++++++++++ .../README_EN.md | 90 +++++++++++++++++++ .../Solution3.js | 35 ++++++++ .../Solution3.ts | 35 ++++++++ 4 files changed, 250 insertions(+) create mode 100644 solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.js create mode 100644 solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.ts diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/README.md b/solution/2500-2599/2501.Longest Square Streak in an Array/README.md index d1c1029af382a..097004bb15c2a 100644 --- a/solution/2500-2599/2501.Longest Square Streak in an Array/README.md +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/README.md @@ -291,4 +291,94 @@ func longestSquareStreak(nums []int) (ans int) { + + +### Solution 3: Counting + + + +#### TypeScript + +```ts +function longestSquareStreak(nums: number[]): number { + const cnt: Record = {}; + const squares = new Set(); + + for (const x of new Set(nums)) { + cnt[x] = (cnt[x] ?? -1) + 1; + cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1; + } + + for (const key in cnt) { + const x = +key; + if (cnt[x] || cnt[x ** 2]) { + squares.add(x); + } + } + + if (squares.size <= 1) return -1; + + const iterator = squares[Symbol.iterator](); + let [max, c, x] = [0, 0, iterator.next().value]; + + while (x !== undefined) { + if (squares.has(x)) { + squares.delete(x); + x **= 2; + c++; + } else { + max = Math.max(max, c); + x = iterator.next().value; + c = 0; + } + } + + return max; +} +``` + +#### JavaScript + +```js +function longestSquareStreak(nums) { + const cnt = {}; + const squares = new Set(); + + for (const x of new Set(nums)) { + cnt[x] = (cnt[x] ?? -1) + 1; + cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1; + } + + for (const key in cnt) { + const x = +key; + if (cnt[x] || cnt[x ** 2]) { + squares.add(x); + } + } + + if (squares.size <= 1) return -1; + + const iterator = squares[Symbol.iterator](); + let [max, c, x] = [0, 0, iterator.next().value]; + + while (x !== undefined) { + if (squares.has(x)) { + squares.delete(x); + x **= 2; + c++; + } else { + max = Math.max(max, c); + x = iterator.next().value; + c = 0; + } + } + + return max; +} +``` + + + + + diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md b/solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md index e6fc7ab9a3664..f2f2104866930 100644 --- a/solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md @@ -291,4 +291,94 @@ func longestSquareStreak(nums []int) (ans int) { + + +### Solution 3: Counting + + + +#### TypeScript + +```ts +function longestSquareStreak(nums: number[]): number { + const cnt: Record = {}; + const squares = new Set(); + + for (const x of new Set(nums)) { + cnt[x] = (cnt[x] ?? -1) + 1; + cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1; + } + + for (const key in cnt) { + const x = +key; + if (cnt[x] || cnt[x ** 2]) { + squares.add(x); + } + } + + if (squares.size <= 1) return -1; + + const iterator = squares[Symbol.iterator](); + let [max, c, x] = [0, 0, iterator.next().value]; + + while (x !== undefined) { + if (squares.has(x)) { + squares.delete(x); + x **= 2; + c++; + } else { + max = Math.max(max, c); + x = iterator.next().value; + c = 0; + } + } + + return max; +} +``` + +#### JavaScript + +```js +function longestSquareStreak(nums) { + const cnt = {}; + const squares = new Set(); + + for (const x of new Set(nums)) { + cnt[x] = (cnt[x] ?? -1) + 1; + cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1; + } + + for (const key in cnt) { + const x = +key; + if (cnt[x] || cnt[x ** 2]) { + squares.add(x); + } + } + + if (squares.size <= 1) return -1; + + const iterator = squares[Symbol.iterator](); + let [max, c, x] = [0, 0, iterator.next().value]; + + while (x !== undefined) { + if (squares.has(x)) { + squares.delete(x); + x **= 2; + c++; + } else { + max = Math.max(max, c); + x = iterator.next().value; + c = 0; + } + } + + return max; +} +``` + + + + + diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.js b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.js new file mode 100644 index 0000000000000..e552e1872c385 --- /dev/null +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.js @@ -0,0 +1,35 @@ +function longestSquareStreak(nums) { + const cnt = {}; + const squares = new Set(); + + for (const x of new Set(nums)) { + cnt[x] = (cnt[x] ?? -1) + 1; + cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1; + } + + for (const key in cnt) { + const x = +key; + if (cnt[x] || cnt[x ** 2]) { + squares.add(x); + } + } + + if (squares.size <= 1) return -1; + + const iterator = squares[Symbol.iterator](); + let [max, c, x] = [0, 0, iterator.next().value]; + + while (x !== undefined) { + if (squares.has(x)) { + squares.delete(x); + x **= 2; + c++; + } else { + max = Math.max(max, c); + x = iterator.next().value; + c = 0; + } + } + + return max; +} diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.ts b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.ts new file mode 100644 index 0000000000000..e0f94ebad443a --- /dev/null +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/Solution3.ts @@ -0,0 +1,35 @@ +function longestSquareStreak(nums: number[]): number { + const cnt: Record = {}; + const squares = new Set(); + + for (const x of new Set(nums)) { + cnt[x] = (cnt[x] ?? -1) + 1; + cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1; + } + + for (const key in cnt) { + const x = +key; + if (cnt[x] || cnt[x ** 2]) { + squares.add(x); + } + } + + if (squares.size <= 1) return -1; + + const iterator = squares[Symbol.iterator](); + let [max, c, x] = [0, 0, iterator.next().value]; + + while (x !== undefined) { + if (squares.has(x)) { + squares.delete(x); + x **= 2; + c++; + } else { + max = Math.max(max, c); + x = iterator.next().value; + c = 0; + } + } + + return max; +} From 9ace57297493cde1a2115b9afeca101669421a03 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 29 Oct 2024 08:21:31 +0800 Subject: [PATCH 2/2] Update README.md --- .../2500-2599/2501.Longest Square Streak in an Array/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/2500-2599/2501.Longest Square Streak in an Array/README.md b/solution/2500-2599/2501.Longest Square Streak in an Array/README.md index 097004bb15c2a..a8b430b774697 100644 --- a/solution/2500-2599/2501.Longest Square Streak in an Array/README.md +++ b/solution/2500-2599/2501.Longest Square Streak in an Array/README.md @@ -293,7 +293,7 @@ func longestSquareStreak(nums []int) (ans int) { -### Solution 3: Counting +### 方法三:计数