-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path128-LongestConsecutiveSequence.py
52 lines (44 loc) · 1.66 KB
/
128-LongestConsecutiveSequence.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
50
51
52
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Time complexity is dominated by the invocation of sort,
# which will run in O(nlgn) time for any sensible implementation.
# Space complexity is O(1)
if not nums:return 0
nums.sort()
longest_streak = 1
current_streak = 1
#Trick: use i, i-1 to avoid the out of bound error
for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
# Count the streak
if nums[i] == nums[i-1] + 1:
current_streak += 1
# Record the longest streak and reset current streak
else:
longest_streak = max(longest_streak, current_streak)
current_streak = 1
# If the current_streak is end at the last element in the list
# We have to return current streak
return max(longest_streak, current_streak)
def longestConsecutive1(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# Time complexity O(n)
# Space complexity O(n)
longest_streak = 0
num_set = set(nums) # no repeat element
for num in num_set:
# Ensure the num is not in any middle of streak
if num - 1 not in num_set:
current_streak = 1
while num + 1 in num_set:
current_streak += 1
num += 1
longest_streak = max(longest_streak, current_streak)
return longest_streak