Medium
Longest Consecutive Sequence
- Time limit
- 2 s
- Memory limit
- 256 MB
- Estimated
- 20 min
- Hash Tables
Description
Description
Given an unsorted integer array nums, return the length of the longest run of consecutive integers that all appear somewhere in nums (the run does not need to appear contiguously in the array itself). Your algorithm should run in O(n) time.
Examples
Example 1
- Input:
nums = [100, 4, 200, 1, 3, 2] - Output:
4 - Explanation: The longest run is
[1, 2, 3, 4].
Example 2
- Input:
nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1] - Output:
9
Example 3
- Input:
nums = [] - Output:
0
Expected Function Behavior
Implement longestConsecutive(nums) and return the length of the longest consecutive run.
Constraints
0 <= nums.length <= 100000-1000000000 <= nums[i] <= 1000000000
Sample cases
3Sample 1
Input
nums = [100,4,200,1,3,2]
Expected output
4
Sample 2
Input
nums = [0,3,7,2,5,8,4,6,0,1]
Expected output
9
Skills and languages
Concepts
- Hash Sets
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer