Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit be114e6

Browse files
authored
feat: add solutions to lc problem: No.2501 (doocs#3685)
1 parent c5bd26b commit be114e6

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

solution/2500-2599/2501.Longest Square Streak in an Array/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,46 @@ func longestSquareStreak(nums []int) (ans int) {
287287
}
288288
```
289289

290+
#### TypeScript
291+
292+
```ts
293+
function longestSquareStreak(nums: number[]): number {
294+
const set = new Set(nums);
295+
const cache = new Map<number, number>();
296+
const dfs = (x: number): number => {
297+
if (cache.has(x)) return cache.get(x)!;
298+
if (!set.has(x)) return 0;
299+
cache.set(x, 1 + dfs(x ** 2));
300+
return cache.get(x)!;
301+
};
302+
303+
for (const x of set) dfs(x);
304+
const ans = Math.max(...cache.values());
305+
306+
return ans > 1 ? ans : -1;
307+
}
308+
```
309+
310+
#### JavaScript
311+
312+
```js
313+
function longestSquareStreak(nums) {
314+
const set = new Set(nums);
315+
const cache = new Map();
316+
const dfs = x => {
317+
if (cache.has(x)) return cache.get(x);
318+
if (!set.has(x)) return 0;
319+
cache.set(x, 1 + dfs(x ** 2));
320+
return cache.get(x);
321+
};
322+
323+
for (const x of set) dfs(x);
324+
const ans = Math.max(...cache.values());
325+
326+
return ans > 1 ? ans : -1;
327+
}
328+
```
329+
290330
<!-- tabs:end -->
291331

292332
<!-- solution:end -->

solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,46 @@ func longestSquareStreak(nums []int) (ans int) {
287287
}
288288
```
289289

290+
#### TypeScript
291+
292+
```ts
293+
function longestSquareStreak(nums: number[]): number {
294+
const set = new Set(nums);
295+
const cache = new Map<number, number>();
296+
const dfs = (x: number): number => {
297+
if (cache.has(x)) return cache.get(x)!;
298+
if (!set.has(x)) return 0;
299+
cache.set(x, 1 + dfs(x ** 2));
300+
return cache.get(x)!;
301+
};
302+
303+
for (const x of set) dfs(x);
304+
const ans = Math.max(...cache.values());
305+
306+
return ans > 1 ? ans : -1;
307+
}
308+
```
309+
310+
#### JavaScript
311+
312+
```js
313+
function longestSquareStreak(nums) {
314+
const set = new Set(nums);
315+
const cache = new Map();
316+
const dfs = x => {
317+
if (cache.has(x)) return cache.get(x);
318+
if (!set.has(x)) return 0;
319+
cache.set(x, 1 + dfs(x ** 2));
320+
return cache.get(x);
321+
};
322+
323+
for (const x of set) dfs(x);
324+
const ans = Math.max(...cache.values());
325+
326+
return ans > 1 ? ans : -1;
327+
}
328+
```
329+
290330
<!-- tabs:end -->
291331

292332
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function longestSquareStreak(nums) {
2+
const set = new Set(nums);
3+
const cache = new Map();
4+
const dfs = x => {
5+
if (cache.has(x)) return cache.get(x);
6+
if (!set.has(x)) return 0;
7+
cache.set(x, 1 + dfs(x ** 2));
8+
return cache.get(x);
9+
};
10+
11+
for (const x of set) dfs(x);
12+
const ans = Math.max(...cache.values());
13+
14+
return ans > 1 ? ans : -1;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function longestSquareStreak(nums: number[]): number {
2+
const set = new Set(nums);
3+
const cache = new Map<number, number>();
4+
const dfs = (x: number): number => {
5+
if (cache.has(x)) return cache.get(x)!;
6+
if (!set.has(x)) return 0;
7+
cache.set(x, 1 + dfs(x ** 2));
8+
return cache.get(x)!;
9+
};
10+
11+
for (const x of set) dfs(x);
12+
const ans = Math.max(...cache.values());
13+
14+
return ans > 1 ? ans : -1;
15+
}

0 commit comments

Comments
 (0)