Skip to main content

128. Longest Consecutive Sequence

https://leetcode.com/problems/longest-consecutive-sequence/

Python


class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
ans = 0
mapper = set(nums)

for num in nums:
if num-1 in mapper:
continue

current = num
length = 1
while current+1 in mapper:
current += 1
length += 1
ans = max(ans, length)

return ans