MediumArrayHash TableGreedyHeap (Priority Queue)

Split Array into Consecutive Subsequences

LeetCode
1 approach, code in all languages

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] <= 1000
  • nums is sorted in non-decreasing order.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now