Imagine you have a sorted array of integers and need to organize them into consecutive sequences like a card player arranging cards into runs. Your goal is to determine if you can split the entire array into one or more subsequences where:
- Each subsequence contains consecutive integers (each number is exactly one more than the previous)
- Every subsequence has at least 3 elements
For example, the array [1,2,3,3,4,5] can be split into [1,2,3] and [3,4,5] - both are consecutive sequences of length 3 or more!
The Challenge: Given a sorted array nums, return true if such a split is possible, false otherwise.
Key Insight: This is a greedy problem disguised as a splitting problem. The trick is to always try to extend existing sequences before starting new ones!
Input & Output
Visualization
Time & Space Complexity
Single pass through unique numbers, each hash map operation is O(1)
Two hash maps storing at most n unique numbers
Constraints
- 1 โค nums.length โค 104
- -1000 โค nums[i] โค 1000
- nums is sorted in non-decreasing order