Skip to content

Commit c2c53ac

Browse files
committed
longest consecutive
1 parent 0711835 commit c2c53ac

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
if not nums:
4+
return 0
5+
6+
num_set = set(nums)
7+
longest = 0
8+
9+
visited = set()
10+
11+
for num in num_set:
12+
if num not in visited and num - 1 not in num_set:
13+
current = num
14+
current_streak = 0
15+
while current in num_set:
16+
visited.add(current)
17+
current_streak += 1
18+
current += 1
19+
longest = max(longest, current_streak)
20+
21+
return longest

0 commit comments

Comments
 (0)