Skip to content

Commit 405f6e6

Browse files
committed
longest-consecutive-sequence solution
1 parent 76ce972 commit 405f6e6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
TC: O(n)
3+
SC: O(n)
4+
"""
5+
6+
class Solution:
7+
def longestConsecutive(self, nums: List[int]) -> int:
8+
longest = 0
9+
num_set = set(nums)
10+
print(num_set)
11+
12+
for n in num_set:
13+
if (n-1) not in num_set:
14+
length = 1
15+
while (n+length) in num_set:
16+
length += 1
17+
longest = max(longest, length)
18+
19+
return longest

0 commit comments

Comments
 (0)