philosyang.com

128. Longest Consecutive Sequence: Hashset: revisit

prep work before final round.


 1class Solution:
 2    def longestConsecutive(self, nums: List[int]) -> int:
 3        items = set(nums)
 4        best = 0
 5
 6        for num in items:
 7            if num - 1 not in items:
 8                x = num
 9                while x + 1 in items:
10                    x += 1
11                best = max(best, x - num + 1)
12        return best

#Neetcode150 #Array #Hashing #Python