Skip to content

Commit 46b0ed5

Browse files
committed
solution: 0128. Longest Consecutive Sequence
1 parent 87256c4 commit 46b0ed5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Diff for: .vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
},
1717
"python.testing.pytestArgs": ["solutions"],
1818
"python.testing.unittestEnabled": false,
19-
"python.testing.pytestEnabled": true
19+
"python.testing.pytestEnabled": true,
20+
"github.copilot.enable": {
21+
"python": "false"
22+
}
2023
}

Diff for: solutions/solution_0128/__init__.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
class Solution:
2-
def longestConsecutive(self, nums: list[int]) -> int: ...
2+
def longestConsecutive(self, nums: list[int]) -> int:
3+
num_set = set(nums)
4+
max_length = 0
5+
6+
for num in num_set:
7+
if num - 1 not in num_set:
8+
current_num = num
9+
current_length = 1
10+
11+
while current_num + 1 in num_set:
12+
current_num += 1
13+
current_length += 1
14+
15+
max_length = max(max_length, current_length)
16+
17+
return max_length

0 commit comments

Comments
 (0)