-
Notifications
You must be signed in to change notification settings - Fork 17
/
longest-consecutive-sequence.py
55 lines (39 loc) · 1.57 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
from typing import List
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
def find(array: List[int], pos: int):
root = pos
while array[root] != root:
root = array[root]
while array[pos] != root:
array[pos], pos = root, array[pos]
return root
def union(root_left: int, root_right: int, count: List[int], array: List[int]):
root_less, root_more = tuple(
sorted([root_left, root_right], key=lambda x: count[x])
)
array[root_less] = root_more
count[root_more] += count[root_less]
union_find = list(range(len(nums)))
num_to_pos = {v: k for k, v in enumerate(nums)}
count = [1] * len(nums)
for num in nums:
for neigh in [num - 1, num + 1]:
if neigh not in num_to_pos:
continue
root_left, root_right = (
find(union_find, num_to_pos[neigh]),
find(union_find, num_to_pos[num]),
)
if root_left != root_right:
union(root_left, root_right, count, union_find)
return max(count) if count else 0
class TestSolution:
def setup(self):
self.sol = Solution()
def test_empty(self):
assert self.sol.longestConsecutive([]) == 0
def test_case1(self):
assert self.sol.longestConsecutive([100, 4, 200, 1, 3, 2]) == 4
def test_case2(self):
assert self.sol.longestConsecutive([1, 0, -1]) == 3