Split Array into Consecutive Subsequences
1 approach, code in every language
Problem description
You are given an integer array that is sorted in non-decreasing order. Decide whether it is possible to partition all of its elements into one or more subsequences, where each subsequence is a run of consecutive increasing integers.
Every subsequence must have a length of at least three. Return true if such a partition exists and false otherwise.
Example 1
Input: nums = [1,2,3,3,4,5]
Output: true
The array splits into [1,2,3] and [3,4,5], each a consecutive run of length 3.
Example 2
Input: nums = [1,2,3,4,4,5]
Output: false
No valid split exists: any attempt leaves a leftover run shorter than three consecutive integers.
Constraints
1 <= nums.length <= 10^4-1000 <= nums[i] <= 1000nums is sorted in non-decreasing order.
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.
1,194 problems · 15,062 graded tests · free