-
Notifications
You must be signed in to change notification settings - Fork 0
/
099.py
45 lines (34 loc) · 1.28 KB
/
099.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
"""
Problem:
Given an unsorted array of integers, find the length of the longest consecutive
elements sequence.
For example, given [100, 4, 200, 1, 3, 2], the longest consecutive element sequence is
[1, 2, 3, 4]. Return its length: 4.
Your algorithm should run in O(n) complexity.
"""
from typing import List
def longest_consecutive_elements_sequence(arr: List[int]) -> int:
length = len(arr)
arr_elems_set = set(arr)
longest_sequence = 0
# generating the longest sequence length
for i in range(length):
if (arr[i] - 1) not in arr_elems_set:
# current element is the starting element of a sequence
j = arr[i]
while j in arr_elems_set:
j += 1
# update longest sequence length
longest_sequence = max(longest_sequence, j - arr[i])
return longest_sequence
if __name__ == "__main__":
print(longest_consecutive_elements_sequence([100, 4, 200, 1]))
print(longest_consecutive_elements_sequence([100, 4, 200, 1, 3]))
print(longest_consecutive_elements_sequence([100, 4, 200, 2, 3]))
print(longest_consecutive_elements_sequence([100, 4, 200, 1, 3, 2]))
print(longest_consecutive_elements_sequence([100, 4, 200, 1, 3, 2, 5]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""