Non-decreasing Subsequences - Problem
Given an integer array nums, your task is to find all unique non-decreasing subsequences that contain at least two elements. A subsequence is a sequence that can be derived from the original array by deleting some or no elements without changing the order of the remaining elements.
For example, in array [4, 6, 7, 7], valid subsequences include [4, 6], [4, 7], [6, 7], [6, 7, 7], [4, 6, 7], and many more. However, [7, 4] would not be valid since it's decreasing.
Key Points:
- Subsequences must be non-decreasing (each element โฅ previous element)
- Must contain at least 2 elements
- Return all unique subsequences (no duplicates)
- Order of results doesn't matter
Input & Output
example_1.py โ Basic case
$
Input:
[4, 6, 7, 7]
โบ
Output:
[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
๐ก Note:
All possible non-decreasing subsequences with at least 2 elements. Note that [4,6,7] appears only once even though there are two 7s, because we want unique subsequences.
example_2.py โ Simple case
$
Input:
[4, 4, 3, 2, 1]
โบ
Output:
[[4,4]]
๐ก Note:
Only [4,4] forms a valid non-decreasing subsequence with at least 2 elements. Other combinations would be decreasing.
example_3.py โ Edge case
$
Input:
[1, 2, 3]
โบ
Output:
[[1,2],[1,2,3],[1,3],[2,3]]
๐ก Note:
All elements are in ascending order, so we get all possible combinations of length 2 or more.
Constraints
- 1 โค nums.length โค 15
- -100 โค nums[i] โค 100
- The number of unique subsequences will not exceed 215
- Order of result doesn't matter
Visualization
Tap to expand
Understanding the Visualization
1
Start Building
Begin with empty subsequence, consider each element
2
Local Check
At each level, use set to track tried values
3
Add & Recurse
Include element if valid and not tried at this level
4
Collect Results
Save subsequences with length โฅ 2
Key Takeaway
๐ฏ Key Insight: Use local sets at each recursion level to prevent duplicate subsequences from being generated, eliminating the need for expensive global deduplication.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code