Skip to content

Commit f6c4477

Browse files
committed
[:solved] longest-consecutive-sequence
1 parent a054fb1 commit f6c4477

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# idea: -
2+
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
sorted_nums = sorted(list(set(nums)))
6+
# sorted_nums = sorted(nums) # duplicate
7+
max_len = 1
8+
tmp = 1
9+
10+
for i in range(1, len(sorted_nums)):
11+
if sorted_nums[i] == sorted_nums[i - 1] + 1:
12+
tmp += 1
13+
else:
14+
max_len = max(max_len, tmp)
15+
tmp = 1
16+
ans = max(max_len, tmp)
17+
return ans
18+
19+
20+
21+
22+

0 commit comments

Comments
 (0)