Skip to content

Commit 90318a2

Browse files
author
MJ Kang
committed
longest consecutive sequence
1 parent 085e297 commit 90318a2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
num_set = set(nums)
6+
longest = 0
7+
8+
for num in nums:
9+
if num - 1 not in num_set:
10+
cnt = 1
11+
next_num = num + 1
12+
while next_num in num_set:
13+
cnt += 1
14+
next_num += 1
15+
longest = max(longest, cnt)
16+
return longest

0 commit comments

Comments
 (0)