Skip to content

Commit 87256c4

Browse files
committed
problem: 0128. Longest Consecutive Sequence
1 parent 16e95c9 commit 87256c4

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

solutions/solution_0128/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 0128. [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-interview-150)
2+
3+
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence_.
4+
5+
You must write an algorithm that runs in `O(n)` time.
6+
7+
### **Example 1:**
8+
9+
<pre><code>
10+
<strong>Input:</strong> nums = [100,4,200,1,3,2]
11+
<strong>Output:</strong> 4
12+
<strong>Explanation:</strong> The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is
13+
4.
14+
</code></pre>
15+
16+
### **Example 2:**
17+
18+
<pre><code>
19+
<strong>Input:</strong> nums = [0,3,7,2,5,8,4,6,0,1]
20+
<strong>Output:</strong> 9
21+
</code></pre>
22+
23+
### **Constraints:**
24+
25+
- <code>0 <= nums.length <= 10<sup>5</sup></code>
26+
- <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>

solutions/solution_0128/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: list[int]) -> int: ...
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import TypedDict
2+
3+
import pytest
4+
5+
from . import Solution
6+
7+
8+
class InputDict(TypedDict):
9+
nums: list[int]
10+
11+
12+
class CaseDict(TypedDict):
13+
input: InputDict
14+
expected: int
15+
16+
17+
test_cases: list[CaseDict] = [
18+
{
19+
'input': {
20+
'nums': [100, 4, 200, 1, 3, 2],
21+
},
22+
'expected': 4,
23+
},
24+
{
25+
'input': {
26+
'nums': [0, 3, 7, 2, 5, 8, 4, 6, 0, 1],
27+
},
28+
'expected': 9,
29+
},
30+
{
31+
'input': {
32+
'nums': [0, 3, 7, 2, 5, 8, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7, 8],
33+
},
34+
'expected': 9,
35+
},
36+
]
37+
38+
39+
@pytest.mark.parametrize(
40+
'test_case',
41+
test_cases,
42+
)
43+
def test_solution(test_case: CaseDict):
44+
result = Solution().longestConsecutive(**test_case['input'])
45+
assert result == test_case['expected']

0 commit comments

Comments
 (0)