Skip to content

Commit 1621698

Browse files
committed
feat: 128. Longest Consecutive Sequence
1 parent 46211b9 commit 1621698

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import List
2+
3+
4+
class Node:
5+
def __init__(self, value):
6+
self.value = value
7+
self.parent = None
8+
self.child = None
9+
10+
class Solution:
11+
def longestConsecutive(self, nums: List[int]) -> int:
12+
answer = 0
13+
dict = {}
14+
15+
for num in nums:
16+
if dict.get(num) is None:
17+
dict[num] = Node(num)
18+
if dict.get(num + 1) is not None:
19+
dict[num + 1].child = dict[num]
20+
dict[num].parent = dict[num + 1]
21+
22+
if dict.get(num - 1) is not None:
23+
dict[num].child = dict[num - 1]
24+
dict[num - 1].parent = dict[num]
25+
26+
for key in dict.keys():
27+
if dict[key].parent is None:
28+
node = dict[key]
29+
count = 1
30+
31+
while node.child is not None:
32+
count += 1
33+
node = node.child
34+
35+
answer = max(answer, count)
36+
37+
return answer
38+

0 commit comments

Comments
 (0)