You are given an integer array nums
that is sorted in non-decreasing order.
Determine if it is possible to split nums
into one or more subsequences such that both of the following conditions are true:
3
or more.Return true
if you can split nums
according to the above conditions, or false
otherwise.
A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., [1,3,5]
is a subsequence of [1,2,3,4,5]
while [1,3,2]
is not).
Example 1:
Input: nums = [1,2,3,3,4,5] Output: true Explanation: nums can be split into the following subsequences: [1,2,3,3,4,5] --> 1, 2, 3 [1,2,3,3,4,5] --> 3, 4, 5
Example 2:
Input: nums = [1,2,3,3,4,4,5,5] Output: true Explanation: nums can be split into the following subsequences: [1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5 [1,2,3,3,4,4,5,5] --> 3, 4, 5
Example 3:
Input: nums = [1,2,3,4,4,5] Output: false Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
Constraints:
1 <= nums.length <= 104
-1000 <= nums[i] <= 1000
nums
is sorted in non-decreasing order.This problem asks whether a sorted integer array nums
can be partitioned into one or more subsequences, where each subsequence is a consecutive increasing sequence of length 3 or more. The solution uses a greedy approach combined with a heap (priority queue) to efficiently track the lengths of available subsequences.
Core Idea:
The algorithm iterates through the input array nums
. For each number, it attempts to extend an existing subsequence or start a new one. The key is to prioritize extending shorter subsequences to maximize the chances of creating subsequences of length 3 or more. This greedy approach works because the array is sorted; if a shorter subsequence can't be extended, it's unlikely a longer one could be either.
Data Structure:
A HashMap
(or defaultdict
in Python) is used to store subsequences. The keys represent numbers in nums
, and the values are priority queues (PriorityQueue
in Java/C++ or a custom heap in Go). Each priority queue stores the lengths of subsequences ending at the key number. Using a min-heap ensures that the shortest subsequences are processed first.
Algorithm:
Initialization: Create an empty HashMap
to store subsequences.
Iteration: Iterate through each number v
in nums
.
Extend Existing Subsequence: Check if there's an existing subsequence ending at v-1
(d.get(v-1)
). If so:
v-1
(shortest subsequence).v
.v-1
becomes empty, remove it from the HashMap
.Start New Subsequence: If there's no existing subsequence ending at v-1
, create a new subsequence of length 1 for v
.
Check Validity: After processing all numbers, check all the priority queues in the HashMap
. If any queue has a minimum length less than 3, it means there's a subsequence shorter than the required length, so return false
. Otherwise, return true
.
Time Complexity: O(N log K), where N is the length of nums
and K is the maximum number of subsequences ending at any particular number. The log K
factor comes from the heap operations (insertion and deletion). In the worst case, K could be O(N), but often it's much smaller.
Space Complexity: O(N) in the worst case to store the subsequences in the HashMap.
Code Explanation (Python):
from collections import defaultdict
import heapq
class Solution:
def isPossible(self, nums: List[int]) -> bool:
d = defaultdict(list) # using defaultdict to simplify handling missing keys
for v in nums:
if d[v - 1]: # Check for existing subsequence ending at v-1
length = heapq.heappop(d[v - 1]) + 1 # Extend the shortest subsequence
heapq.heappush(d[v], length) # Add the extended length
else:
heapq.heappush(d[v], 1) # Start a new subsequence of length 1
return all(len(seq) == 0 or seq[0] >= 3 for seq in d.values()) #Check if all subsequences are of length >=3
The other language implementations follow the same logic, using their respective priority queue implementations. The core idea of prioritizing shorter subsequences and efficiently tracking lengths remains consistent across all the provided code.