-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproblem_128.py
72 lines (67 loc) · 2.2 KB
/
problem_128.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
# Problem 128: Longest Consecutive Sequence (Medium): https://leetcode.com/problems/longest-consecutive-sequence/
class Solution:
def longestConsecutive(self, nums) -> int:
# Alternatively we can convert to set
# and remove the duplicate check
sortedlist = sorted(nums)
start = 0
result = []
temp = []
for end in range(len(sortedlist)):
item = sortedlist[end]
if(start != end):
if(abs(item - sortedlist[end-1]) == 1):
temp.append(item)
# Skip duplicate numbers
# e.g 0 1 1 2
elif(abs(item - sortedlist[end-1]) == 0):
continue
else:
start = end
if(len(temp) > len(result)):
result = temp.copy()
temp.clear()
temp.append(item)
else:
temp.append(item)
if(len(temp) > len(result)):
result = temp.copy()
return len(result)
# Time Complexity: O(nlogn)
# space complexity: O(1)
class Solution2:
def longestConsecutive(self, nums: List[int]) -> int:
nums.sort()
result = 0
count = 1
if(len(nums) == 0):
return 0
for i, num in enumerate(nums):
if((i != len(nums) - 1) and (num == nums[i + 1])):
continue
if((i != len(nums) - 1) and (num + 1 == nums[i + 1])):
count += 1
else:
result = max(result, count)
count = 1
result = max(result, count)
return result
# Time Complexity: O(n)
# space complexity: O(n)
class Solution3:
def longestConsecutive(self, nums: List[int]) -> int:
s = set(nums)
count = 0
result = 0
for i, num in enumerate(s):
if(num - 1 not in s):
temp = num
count = 0
while(temp in s):
count += 1
temp += 1
result = max(result, count)
return result
if __name__ == "__main__":
nums = [1,2 ,0, 1]
print(Solution().longestConsecutive(nums)) # 4