-
Notifications
You must be signed in to change notification settings - Fork 43
/
longest-consecutive-sequence.py
217 lines (172 loc) · 6.02 KB
/
longest-consecutive-sequence.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
128. Longest Consecutive Sequence
Medium
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Constraints:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
"""
# V0
# IDEA : sliding window
class Solution(object):
def longestConsecutive(self, nums):
# edge case
if not nums:
return 0
nums = list(set(nums))
# if len(nums) == 1: # not necessary
# return 1
# sort first
nums.sort()
res = 0
l = 0
r = 1
"""
NOTE !!!
Sliding window here :
condition : l, r are still in list (r < len(nums) and l < len(nums))
2 cases
case 1) nums[r] != nums[r-1] + 1
-> means not continous,
-> so we need to move r to right (1 idx)
-> and MOVE l to r - 1, since it's NOT possible to have any continous subarray within [l, r] anymore
case 2) nums[r] == nums[r-1] + 1
-> means there is continous subarray currently, so we keep moving r to right (r+=1) and get current max sub array length (res = max(res, r-l+1))
"""
while r < len(nums) and l < len(nums):
# case 1)
if nums[r] != nums[r-1] + 1:
r += 1
l = (r-1)
# case 2)
else:
res = max(res, r-l+1)
r += 1
# edge case : if res == 0, means no continous array (with len > 1), so we return 1 (a single alphabet can be recognized as a "continous assay", and its len = 1)
return res if res > 1 else 1
# V0'
# IDEA : SORTING + 2 POINTERS
class Solution(object):
def longestConsecutive(self, nums):
# edge case
if not nums:
return 0
nums.sort()
cur_len = 1
max_len = 1
#print ("nums = " + str(nums))
# NOTE : start from idx = 1
for i in range(1, len(nums)):
### NOTE : start from nums[i] != nums[i-1] case
if nums[i] != nums[i-1]:
### NOTE : if nums[i] == nums[i-1]+1 : cur_len += 1
if nums[i] == nums[i-1]+1:
cur_len += 1
### NOTE : if nums[i] != nums[i-1]+1 : get max len, and reset cur_lent as 1
else:
max_len = max(max_len, cur_len)
cur_len = 1
# check max len again
return max(max_len, cur_len)
# V0'
# IDEA : SORTING + 2 POINTERS
class Solution:
def longestConsecutive(self, nums):
if not nums:
return 0
nums.sort()
longest_streak = 1
current_streak = 1
for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
if nums[i] == nums[i-1]+1:
current_streak += 1
else:
longest_streak = max(longest_streak, current_streak)
current_streak = 1
return max(longest_streak, current_streak)
# V0'
# IDEA : HASHSET
class Solution:
def longestConsecutive(self, nums):
longest_streak = 0
num_set = set(nums)
for num in num_set:
if num - 1 not in num_set:
current_num = num
current_streak = 1
while current_num + 1 in num_set:
current_num += 1
current_streak += 1
longest_streak = max(longest_streak, current_streak)
return longest_streak
# V1
# IDEA : BRUTE FORCE
# https://leetcode.com/problems/longest-consecutive-sequence/solution/
class Solution:
def longestConsecutive(self, nums):
longest_streak = 0
for num in nums:
current_num = num
current_streak = 1
while current_num + 1 in nums:
current_num += 1
current_streak += 1
longest_streak = max(longest_streak, current_streak)
return longest_streak
# V2
# IDEA : SORTING
# https://leetcode.com/problems/longest-consecutive-sequence/solution/
class Solution:
def longestConsecutive(self, nums):
if not nums:
return 0
nums.sort()
longest_streak = 1
current_streak = 1
for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
if nums[i] == nums[i-1]+1:
current_streak += 1
else:
longest_streak = max(longest_streak, current_streak)
current_streak = 1
return max(longest_streak, current_streak)
# V3
# IDEA : HashSet and Intelligent Sequence Building
# https://leetcode.com/problems/longest-consecutive-sequence/solution/
class Solution:
def longestConsecutive(self, nums):
longest_streak = 0
num_set = set(nums)
for num in num_set:
if num - 1 not in num_set:
current_num = num
current_streak = 1
while current_num + 1 in num_set:
current_num += 1
current_streak += 1
longest_streak = max(longest_streak, current_streak)
return longest_streak
# V4
# https://leetcode.com/problems/longest-consecutive-sequence/discuss/1231015/Concise-Python-Solution-8-lines
# https://www.youtube.com/watch?v=rc2QdQ7U78I
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
# Concise Solution from https://www.youtube.com/watch?v=rc2QdQ7U78I&t=24s
if not nums: return 0
map_ = {}
for num in set(nums):
l = map_.get(num - 1, 0)
r = map_.get(num + 1, 0)
t = l + r + 1
map_[num] = map_[num - l] = map_[num + r] = l + r + 1
return max(map_.values())