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..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 @@ -291,4 +291,94 @@ func longestSquareStreak(nums []int) (ans int) { + + +### 方法三:计数 + + + +#### 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; +}