-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestConsecutiveSequence.py
More file actions
31 lines (23 loc) · 1004 Bytes
/
longestConsecutiveSequence.py
File metadata and controls
31 lines (23 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def longestConsecutiveSequence(nums: list[int]) -> int:
# Step 1: Use a set for O(1) lookups
numSet = set(nums)
# Step 2: Initialize variable to track the longest sequence
longest = 0
# Step 3: Iterate over each number
for num in numSet:
# Only try to build sequence from numbers that are the start of a sequence
if (num - 1) not in numSet:
# This means 'num' is the start of a new sequence
length = 1
# Step 4: Expand the sequence
while (num + length) in numSet:
length += 1
# Step 5: Update the result if we found a longer streak
longest = max(length, longest)
# Step 6: Return the longest sequence length found
return longest
# Examples & test
print("Example")
nums = [2,20,4,10,3,4,5]
result = longestConsecutiveSequence(nums)
print(result)