Longest Consecutive Sequence(math)
Input:
[100, 4, 200, 1, 3, 2]
Output:
4
Explanation:
The longest consecutive elements sequence is
[1, 2, 3, 4]
. Therefore its length is 4.class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ss = set(nums)
mm = 0
for n in ss:
if n-1 not in ss:
ans = 1
cur = n
while cur+1 in ss:
ans +=1
cur+=1
mm = max(mm,ans)
return mmLast updated