Skip to content

Latest commit

 

History

History
45 lines (32 loc) · 1.19 KB

_2501. Longest Square Streak in an Array.md

File metadata and controls

45 lines (32 loc) · 1.19 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : October 28, 2024

Last updated : October 28, 2024


Related Topics : Array, Hash Table, Binary Search, Dynamic Programming, Sorting

Acceptance Rate : 53.08 %


Solutions

Python

class Solution:
    def longestSquareStreak(self, nums: List[int]) -> int:
        vals = set(nums)
        nums.sort(reverse=False)
        max_streak = 0

        for num in nums :
            cnt = 1
            curr = num ** 2
            while vals and curr in vals :
                vals.remove(curr)           # optimizing
                cnt += 1
                curr = curr ** 2
            max_streak = max(max_streak, cnt)

        return max_streak if max_streak > 1 else -1