-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
longest-square-streak-in-an-array.py
43 lines (40 loc) · 1.1 KB
/
longest-square-streak-in-an-array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Time: O(nlogn)
# Space: O(n)
# hash table
class Solution(object):
def longestSquareStreak(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
sorted_nums = sorted(set(nums))
squares = {x for x in sorted_nums if x%2 < 2} # squared_num % 4 in [0, 1]
result = 0
for x in sorted_nums:
square, cnt = x**2, 1
while square in squares:
squares.remove(square)
cnt += 1
square *= square
result = max(result, cnt)
return result if result != 1 else -1
# Time: O(nlogn)
# Space: O(n)
# dp
class Solution2(object):
def longestSquareStreak(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp = collections.defaultdict(int)
nums.sort()
result = -1
for x in nums:
sqrt_x = int(x**0.5)
if sqrt_x**2 == x:
dp[x] = dp[sqrt_x]+1
else:
dp[x] = 1
result = max(result, dp[x])
return result if result != 1 else -1