Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d21ed51

Browse files
fix: update solutions to lc problem: No.2501 (doocs#4314)
No.2501.Longest Square Streak in an Array
1 parent ee4e016 commit d21ed51

File tree

16 files changed

+384
-431
lines changed

16 files changed

+384
-431
lines changed

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

Lines changed: 131 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ class Solution:
8383
def longestSquareStreak(self, nums: List[int]) -> int:
8484
s = set(nums)
8585
ans = -1
86-
for v in nums:
86+
for x in nums:
8787
t = 0
88-
while v in s:
89-
v *= v
88+
while x in s:
89+
x *= x
9090
t += 1
9191
if t > 1:
9292
ans = max(ans, t)
@@ -98,15 +98,14 @@ class Solution:
9898
```java
9999
class Solution {
100100
public int longestSquareStreak(int[] nums) {
101-
Set<Integer> s = new HashSet<>();
102-
for (int v : nums) {
103-
s.add(v);
101+
Set<Long> s = new HashSet<>();
102+
for (long x : nums) {
103+
s.add(x);
104104
}
105105
int ans = -1;
106-
for (int v : nums) {
106+
for (long x : s) {
107107
int t = 0;
108-
while (s.contains(v)) {
109-
v *= v;
108+
for (; s.contains(x); x *= x) {
110109
++t;
111110
}
112111
if (t > 1) {
@@ -126,14 +125,14 @@ public:
126125
int longestSquareStreak(vector<int>& nums) {
127126
unordered_set<long long> s(nums.begin(), nums.end());
128127
int ans = -1;
129-
for (int& v : nums) {
128+
for (long long x : nums) {
130129
int t = 0;
131-
long long x = v;
132-
while (s.count(x)) {
133-
x *= x;
130+
for (; s.contains(x); x *= x) {
134131
++t;
135132
}
136-
if (t > 1) ans = max(ans, t);
133+
if (t > 1) {
134+
ans = max(ans, t);
135+
}
137136
}
138137
return ans;
139138
}
@@ -145,24 +144,78 @@ public:
145144
```go
146145
func longestSquareStreak(nums []int) int {
147146
s := map[int]bool{}
148-
for _, v := range nums {
149-
s[v] = true
147+
for _, x := range nums {
148+
s[x] = true
150149
}
151150
ans := -1
152-
for _, v := range nums {
151+
for x := range s {
153152
t := 0
154-
for s[v] {
155-
v *= v
153+
for s[x] {
154+
x *= x
156155
t++
157156
}
158-
if t > 1 && t > ans {
159-
ans = t
157+
if t > 1 {
158+
ans = max(ans, t)
160159
}
161160
}
162161
return ans
163162
}
164163
```
165164

165+
#### TypeScript
166+
167+
```ts
168+
function longestSquareStreak(nums: number[]): number {
169+
const s = new Set(nums);
170+
let ans = -1;
171+
172+
for (const num of nums) {
173+
let x = num;
174+
let t = 0;
175+
176+
while (s.has(x)) {
177+
x *= x;
178+
t += 1;
179+
}
180+
181+
if (t > 1) {
182+
ans = Math.max(ans, t);
183+
}
184+
}
185+
186+
return ans;
187+
}
188+
```
189+
190+
#### JavaScript
191+
192+
```js
193+
/**
194+
* @param {number[]} nums
195+
* @return {number}
196+
*/
197+
var longestSquareStreak = function (nums) {
198+
const s = new Set(nums);
199+
let ans = -1;
200+
201+
for (const num of nums) {
202+
let x = num;
203+
let t = 0;
204+
205+
while (s.has(x)) {
206+
x *= x;
207+
t += 1;
208+
}
209+
210+
if (t > 1) {
211+
ans = Math.max(ans, t);
212+
}
213+
}
214+
215+
return ans;
216+
};
217+
```
218+
166219
<!-- tabs:end -->
167220

168221
<!-- solution:end -->
@@ -190,35 +243,35 @@ func longestSquareStreak(nums []int) int {
190243
class Solution:
191244
def longestSquareStreak(self, nums: List[int]) -> int:
192245
@cache
193-
def dfs(x):
246+
def dfs(x: int) -> int:
194247
if x not in s:
195248
return 0
196249
return 1 + dfs(x * x)
197250

198251
s = set(nums)
199-
ans = max(dfs(x) for x in nums)
252+
ans = max(dfs(x) for x in s)
200253
return -1 if ans < 2 else ans
201254
```
202255

203256
#### Java
204257

205258
```java
206259
class Solution {
207-
private Map<Integer, Integer> f = new HashMap<>();
208-
private Set<Integer> s = new HashSet<>();
260+
private Map<Long, Integer> f = new HashMap<>();
261+
private Set<Long> s = new HashSet<>();
209262

210263
public int longestSquareStreak(int[] nums) {
211-
for (int v : nums) {
212-
s.add(v);
264+
for (long x : nums) {
265+
s.add(x);
213266
}
214267
int ans = 0;
215-
for (int v : nums) {
216-
ans = Math.max(ans, dfs(v));
268+
for (long x : s) {
269+
ans = Math.max(ans, dfs(x));
217270
}
218271
return ans < 2 ? -1 : ans;
219272
}
220273

221-
private int dfs(int x) {
274+
private int dfs(long x) {
222275
if (!s.contains(x)) {
223276
return 0;
224277
}
@@ -240,16 +293,20 @@ public:
240293
int longestSquareStreak(vector<int>& nums) {
241294
unordered_set<long long> s(nums.begin(), nums.end());
242295
int ans = 0;
243-
unordered_map<int, int> f;
244-
function<int(int)> dfs = [&](int x) -> int {
245-
if (!s.count(x)) return 0;
246-
if (f.count(x)) return f[x];
247-
long long t = 1ll * x * x;
248-
if (t > INT_MAX) return 1;
296+
unordered_map<long long, int> f;
297+
auto dfs = [&](this auto&& dfs, long long x) -> int {
298+
if (!s.contains(x)) {
299+
return 0;
300+
}
301+
if (f.contains(x)) {
302+
return f[x];
303+
}
249304
f[x] = 1 + dfs(x * x);
250305
return f[x];
251306
};
252-
for (int& v : nums) ans = max(ans, dfs(v));
307+
for (long long x : s) {
308+
ans = max(ans, dfs(x));
309+
}
253310
return ans < 2 ? -1 : ans;
254311
}
255312
};
@@ -260,8 +317,8 @@ public:
260317
```go
261318
func longestSquareStreak(nums []int) (ans int) {
262319
s := map[int]bool{}
263-
for _, v := range nums {
264-
s[v] = true
320+
for _, x := range nums {
321+
s[x] = true
265322
}
266323
f := map[int]int{}
267324
var dfs func(int) int
@@ -275,8 +332,8 @@ func longestSquareStreak(nums []int) (ans int) {
275332
f[x] = 1 + dfs(x*x)
276333
return f[x]
277334
}
278-
for _, v := range nums {
279-
if t := dfs(v); ans < t {
335+
for x := range s {
336+
if t := dfs(x); ans < t {
280337
ans = t
281338
}
282339
}
@@ -291,130 +348,54 @@ func longestSquareStreak(nums []int) (ans int) {
291348

292349
```ts
293350
function longestSquareStreak(nums: number[]): number {
294-
const set = new Set(nums);
295-
const cache = new Map<number, number>();
351+
const s = new Set(nums);
352+
const f = new Map<number, number>();
296353
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)!;
354+
if (f.has(x)) {
355+
return f.get(x)!;
356+
}
357+
if (!s.has(x)) {
358+
return 0;
359+
}
360+
f.set(x, 1 + dfs(x ** 2));
361+
return f.get(x)!;
301362
};
302363

303-
for (const x of set) dfs(x);
304-
const ans = Math.max(...cache.values());
305-
364+
for (const x of s) {
365+
dfs(x);
366+
}
367+
const ans = Math.max(...f.values());
306368
return ans > 1 ? ans : -1;
307369
}
308370
```
309371

310372
#### JavaScript
311373

312374
```js
313-
function longestSquareStreak(nums) {
314-
const set = new Set(nums);
315-
const cache = new Map();
375+
/**
376+
* @param {number[]} nums
377+
* @return {number}
378+
*/
379+
var longestSquareStreak = function (nums) {
380+
const s = new Set(nums);
381+
const f = new Map();
316382
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-
330-
<!-- tabs:end -->
331-
332-
<!-- solution:end -->
333-
334-
<!-- solution:start -->
335-
336-
### 方法三:计数
337-
338-
<!-- tabs:start -->
339-
340-
#### TypeScript
341-
342-
```ts
343-
function longestSquareStreak(nums: number[]): number {
344-
const cnt: Record<number, number> = {};
345-
const squares = new Set<number>();
346-
347-
for (const x of new Set(nums)) {
348-
cnt[x] = (cnt[x] ?? -1) + 1;
349-
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
350-
}
351-
352-
for (const key in cnt) {
353-
const x = +key;
354-
if (cnt[x] || cnt[x ** 2]) {
355-
squares.add(x);
356-
}
357-
}
358-
359-
if (squares.size <= 1) return -1;
360-
361-
const iterator = squares[Symbol.iterator]();
362-
let [max, c, x] = [0, 0, iterator.next().value];
363-
364-
while (x !== undefined) {
365-
if (squares.has(x)) {
366-
squares.delete(x);
367-
x **= 2;
368-
c++;
369-
} else {
370-
max = Math.max(max, c);
371-
x = iterator.next().value;
372-
c = 0;
383+
if (f.has(x)) {
384+
return f.get(x);
373385
}
374-
}
375-
376-
return max;
377-
}
378-
```
379-
380-
#### JavaScript
381-
382-
```js
383-
function longestSquareStreak(nums) {
384-
const cnt = {};
385-
const squares = new Set();
386-
387-
for (const x of new Set(nums)) {
388-
cnt[x] = (cnt[x] ?? -1) + 1;
389-
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
390-
}
391-
392-
for (const key in cnt) {
393-
const x = +key;
394-
if (cnt[x] || cnt[x ** 2]) {
395-
squares.add(x);
386+
if (!s.has(x)) {
387+
return 0;
396388
}
397-
}
398-
399-
if (squares.size <= 1) return -1;
400-
401-
const iterator = squares[Symbol.iterator]();
402-
let [max, c, x] = [0, 0, iterator.next().value];
389+
f.set(x, 1 + dfs(x ** 2));
390+
return f.get(x);
391+
};
403392

404-
while (x !== undefined) {
405-
if (squares.has(x)) {
406-
squares.delete(x);
407-
x **= 2;
408-
c++;
409-
} else {
410-
max = Math.max(max, c);
411-
x = iterator.next().value;
412-
c = 0;
413-
}
393+
for (const x of s) {
394+
dfs(x);
414395
}
415-
416-
return max;
417-
}
396+
constans=Math.max(...f.values());
397+
return ans >1? ans :-1;
398+
};
418399
```
419400

420401
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /