128. Longest Consecutive Sequence
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Example 1:
Example 2:
Example 3:
- 0 <= nums.length <= 105
- -109 <= nums[i] <= 109
Constraints:
Notes
Intuition
A number is the start of a consecutive sequence if there's no number exactly one less than it in the array. Once we identify a sequence start, we can count how long the sequence extends by checking for consecutive numbers.
Implementation
Store all numbers in a hashmap for O(1) lookup. Iterate through the array and check if each number is a sequence start (num - 1 not in map). If it is, walk forward counting consecutive numbers (num + 1, num + 2, etc.) and track the maximum length found.
Edge-cases
Duplicates can cause us to process the same sequence start multiple times. Initialize the hashmap with all False values and mark a sequence start as True once processed to avoid redundant work.
- Time: O(n) — each number is part of at most one sequence traversal
- Space: O(n) — hashmap stores all numbers