Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,46 @@ func longestSquareStreak(nums []int) (ans int) {
}
```

#### TypeScript

```ts
function longestSquareStreak(nums: number[]): number {
const set = new Set(nums);
const cache = new Map<number, number>();
const dfs = (x: number): number => {
if (cache.has(x)) return cache.get(x)!;
if (!set.has(x)) return 0;
cache.set(x, 1 + dfs(x ** 2));
return cache.get(x)!;
};

for (const x of set) dfs(x);
const ans = Math.max(...cache.values());

return ans > 1 ? ans : -1;
}
```

#### JavaScript

```js
function longestSquareStreak(nums) {
const set = new Set(nums);
const cache = new Map();
const dfs = x => {
if (cache.has(x)) return cache.get(x);
if (!set.has(x)) return 0;
cache.set(x, 1 + dfs(x ** 2));
return cache.get(x);
};

for (const x of set) dfs(x);
const ans = Math.max(...cache.values());

return ans > 1 ? ans : -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,46 @@ func longestSquareStreak(nums []int) (ans int) {
}
```

#### TypeScript

```ts
function longestSquareStreak(nums: number[]): number {
const set = new Set(nums);
const cache = new Map<number, number>();
const dfs = (x: number): number => {
if (cache.has(x)) return cache.get(x)!;
if (!set.has(x)) return 0;
cache.set(x, 1 + dfs(x ** 2));
return cache.get(x)!;
};

for (const x of set) dfs(x);
const ans = Math.max(...cache.values());

return ans > 1 ? ans : -1;
}
```

#### JavaScript

```js
function longestSquareStreak(nums) {
const set = new Set(nums);
const cache = new Map();
const dfs = x => {
if (cache.has(x)) return cache.get(x);
if (!set.has(x)) return 0;
cache.set(x, 1 + dfs(x ** 2));
return cache.get(x);
};

for (const x of set) dfs(x);
const ans = Math.max(...cache.values());

return ans > 1 ? ans : -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function longestSquareStreak(nums) {
const set = new Set(nums);
const cache = new Map();
const dfs = x => {
if (cache.has(x)) return cache.get(x);
if (!set.has(x)) return 0;
cache.set(x, 1 + dfs(x ** 2));
return cache.get(x);
};

for (const x of set) dfs(x);
const ans = Math.max(...cache.values());

return ans > 1 ? ans : -1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function longestSquareStreak(nums: number[]): number {
const set = new Set(nums);
const cache = new Map<number, number>();
const dfs = (x: number): number => {
if (cache.has(x)) return cache.get(x)!;
if (!set.has(x)) return 0;
cache.set(x, 1 + dfs(x ** 2));
return cache.get(x)!;
};

for (const x of set) dfs(x);
const ans = Math.max(...cache.values());

return ans > 1 ? ans : -1;
}
Loading