Skip to content

Commit

Permalink
Optimized #2501 in Medium
Browse files Browse the repository at this point in the history
  • Loading branch information
Lei-Tin committed Nov 2, 2024
1 parent c7c323e commit 671705e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Medium/#2501 longestSquareStreak.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,47 @@ def longestSquareStreak(self, nums: List[int]) -> int:
max_val = max(seen.values())

return max_val if max_val != 1 else -1


# Union find solution
class UF:
def __init__(self, vals):
self.p = {i: i for i in vals}

def find(self, x):
if x != self.p[x]:
self.p[x] = self.find(self.p[x])
return self.p[x]

def union(self, x, y):
xp, yp = self.find(x), self.find(y)
if xp == yp:
return False
self.p[xp] = yp
return True

# class Solution:
# # UF Solution
# def longestSquareStreak(self, nums: List[int]) -> int:
# # Init UF
# # Each entry is connecting to itself
# uf = UF(nums)
#
# for num in nums:
# # Union its sqrt and its square
# sqrt_num = math.sqrt(num)
# if sqrt_num == int(sqrt_num) and int(sqrt_num) in uf.p:
# uf.union(num, sqrt_num)
#
# sq_num = int(num ** 2)
# if sq_num in uf.p:
# uf.union(num, sq_num)
#
# counts = {}
# for n in uf.p:
# val = uf.find(n)
# counts[val] = counts.get(val, 0) + 1
#
# ans = max(counts.values())
#
# return -1 if ans == 1 else ans

0 comments on commit 671705e

Please sign in to comment.