Longest Consecutive Sequence
Find the longest run of consecutive numbers in an unsorted list
DSA, Medium, Arrays & Hashing
Which structure or algorithm, and what does it cost.
A list nums holds whole numbers in any order. A run is a set of values that follow each other with a step of 1, like 6, 7, 8, 9. The run does not need to be in order inside nums. Return the length of the longest run that nums contains. A repeated value counts once. For an empty list, return 0.
Examples
Constraints
- 0 <= length of
nums<= 100,000. - Each number is a whole number between -1,000,000,000 and 1,000,000,000.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(n) memory. Sorting costs O(n log n) and is not the target.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Sorting makes runs easy to see, but it costs more than the target. Think about how to find a run without sorting.
Hint 2 the approach
Put all the values in a set, so that you can ask in constant time whether a value is present. Repeats vanish too.
Hint 3 the key step
Only start counting at a value whose predecessor is not in the set. Count up while the next value is present, and keep the best length.
Walkthrough
Full walkthrough brute force to best
Brute force
For each number, count upward by scanning the whole list for the next value, then the next, and so on. A run of length r costs r scans of n items. In the worst case that is O(n^3) time, far too slow for 100,000 numbers.
Better method
Sort the numbers, remove repeats, and walk through the list while the next value is one more than the current one. Track the longest walk. The sort costs O(n log n) time, and the sorted copy takes O(n) memory. It is simple and correct, but the sort is more than the target.
Best method
Put every value in a set. A run has one smallest value, and that value has no predecessor in the set. So for each value x, only if x minus 1 is not in the set, treat x as the start of a run. Count up from x while x + 1, x + 2 and so on are in the set, and record the length. Values in the middle of a run are skipped, because their predecessor is present. Each value is then visited as a start at most once and as a member of one walk at most once, so the work is linear. The set also removes repeats for free.
Hand trace
Run on [40, 7, 42, 8, 41, 43, 9, 100]. The set is {7, 8, 9, 40, 41, 42, 43, 100}. Value 7: 6 is missing, so it is a start. Count 7, 8, 9, then 10 is missing, so the length is 3. best = 3. Value 8: 7 is present, skip. Value 9: skip. Value 40: 39 is missing, so it is a start. Count 40, 41, 42, 43, then 44 is missing, so the length is 4. best = 4. Values 41, 42 and 43 are skipped. Value 100: 99 is missing, so it is a start with length 1. The answer is 4.
Code
def longest_run(nums):
values = set(nums)
best = 0
for x in values:
if x - 1 not in values:
y = x
while y + 1 in values:
y += 1
best = max(best, y - x + 1)
return bestTime and memory
O(n) time on average, because every value is a start at most once and is counted inside at most one walk, and each set lookup is O(1) on average. O(n) memory for the set.
Common mistakes
- Starting a walk from every value, not only from run starts. A long run then gets walked again from each of its values, which is O(n^2).
- Sorting and counting equal neighbours as part of the run. After a sort, repeats sit together, so remove them or skip them, or the length is too large.
- Returning 1 for an empty list, or leaving the best length unset. The answer for no numbers is 0.
- Looking at the list order. The run does not need to be in order inside the list, only present.