325. Longest Consecutive Sequence
Medium
Sequence Start Detection
Hashing
Problem
Given an unsorted array of integers nums, return the length of the longest sequence of consecutive integer values. The values do not need to appear consecutively in the original array.
Examples
Example 1
Input: [100,4,200,1,3,2]
Output: 4
Example 2
Input: [0,3,7,2,5,8,4,6,0,1]
Output: 9
Example 3
Input: []
Output: 0
Constraints
� 0 <= nums.length <= 100000
� -1000000000 <= nums[i] <= 1000000000
Hints
?? Put every value into a hash set.
?? Only begin expanding a sequence from x when x - 1 is absent.
?? Each value is visited only as part of one sequence expansion.
Expected Complexity
Time: O(n)
Space: O(n)
Follow-up
Can you achieve expected O(n) time without sorting the array?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
[100,4,200,1,3,2]Expected
4