Skip to content

Commit 58e53aa

Browse files
committed
solved longest consecutive sequence
1 parent 380ebdd commit 58e53aa

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TC : O(n) where n is the length of nums
2+
# SC : O(n) where n is the size of nums
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
numSet = set(nums)
6+
longest = 0
7+
8+
for n in nums:
9+
# check whether its the start of the sequence
10+
if (n - 1) not in numSet:
11+
length = 0
12+
while (n + length) in numSet:
13+
length += 1
14+
longest = max(length, longest)
15+
return longest

0 commit comments

Comments
 (0)