We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 90318a2 commit a907534Copy full SHA for a907534
longest-consecutive-sequence/mandoolala.py
@@ -4,13 +4,11 @@ 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)
+ for num in num_set:
+ if num - 1 in num_set:
+ continue
+ length = 1
+ while num + length in num_set:
+ length += 1
+ longest = max(length, longest)
16
return longest
0 commit comments