Longest Consecutive Sequence II
HardArray
Description
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.
Examples
Input:
nums = [100,4,200,1,3,2]Output:
4Explanation:
The longest consecutive sequence is [1, 2, 3, 4].
Input:
nums = [9,1,-3,2,4,8,3,-1,6,5,0,7,0,1]Output:
11Explanation:
The longest consecutive sequence is [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]. Note that duplicates (like the repeated 0 and 1) don't affect the consecutive sequence length.
Input:
nums = [-5,-4,-3,10,11,12,13,20]Output:
4Explanation:
There are three separate consecutive sequences: [-5, -4, -3] (length 3), [10, 11, 12, 13] (length 4), and [20] (length 1). The longest consecutive sequence has length 4.
Constraints
- •
0 ≤ nums.length ≤ 10⁵ - •
-10⁹ ≤ nums[i] ≤ 10⁹