File tree Expand file tree Collapse file tree 4 files changed +110
-0
lines changed
solution/2500-2599/2501.Longest Square Streak in an Array Expand file tree Collapse file tree 4 files changed +110
-0
lines changed Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments