Skip to content

Commit 5f6479d

Browse files
committed
O(n^2) worst case time and O(n) space.
1 parent 26e9a1f commit 5f6479d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
if not nums:
4+
return 0
5+
result = 1
6+
num_set = set(nums)
7+
for num in num_set:
8+
cur_num,count = num,1
9+
while cur_num + 1 in num_set:
10+
count += 1
11+
cur_num += 1
12+
result = max(result,count)
13+
return result

0 commit comments

Comments
 (0)