-
Notifications
You must be signed in to change notification settings - Fork 0
/
128_longestConsecSeq.py
49 lines (34 loc) · 1.01 KB
/
128_longestConsecSeq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 128. Longest Consecutive Sequence
from collections import defaultdict
'''
[0,3,7,2,5,8,4,6,0,1]
[0,1,1,2]
[100,4,200,1,3,2]
[0,-1]
'''
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if len(nums)==1:
return 1
if len(nums)==0:
return 0
nums = sorted(nums)
hashmap = defaultdict(int)
ind = 0
for i in range(1,len(nums)):
if nums[i-1]==nums[i]:
continue
if nums[i-1]+1!=nums[i]:
ind = i
hashmap[nums[ind]]+=1
else:
if not hashmap:
hashmap[nums[ind]]+=1
hashmap[nums[ind]]+=1
if not hashmap:
hashmap[nums[0]]=1
maxval = -1
for key,value in hashmap.items():
if maxval<value:
maxval = value
return maxval