Skip to content

Commit 89ac2fe

Browse files
committed
Feat: longest-consecutive-sequence Solution
1 parent 72b43d1 commit 89ac2fe

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
if not nums:
4+
return 0
5+
6+
nums = sorted(set(nums))
7+
longest_streak = 1
8+
current_streak = 1
9+
10+
for i in range(1, len(nums)):
11+
if nums[i] == nums[i - 1] + 1:
12+
current_streak += 1
13+
else:
14+
longest_streak = max(longest_streak, current_streak)
15+
current_streak = 1
16+
17+
return max(longest_streak, current_streak)

0 commit comments

Comments
 (0)