Skip to main content

128 - Longest Consecutive Sequence

Details

KeyValue
Linkhttps://leetcode.com/problems/longest-consecutive-sequence/
LanguagePython 3
Runtime392 ms, faster than 81.47% of Python3 online submissions for Longest Consecutive Sequence
Memory Usage28.6 MB, less than 42.76% of Python3 online submissions for Longest Consecutive Sequence
DatastructuresSet(), List[int]
AlgorithmsUse set to build sequence
ComplexityTime: O(N) Memory: O(N)

Procedure

  1. ...

Code

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

while nums:
first = last = nums.pop()
while first - 1 in nums: first -= 1; nums.remove(first)
while last + 1 in nums: last += 1; nums.remove(last)
sequence_length = max(sequence_length,last-first+1)

return sequence_length