Loading content...
Given an integer array nums, your task is to discover and return all the distinct subsequences of the array that maintain an ascending or equal order (i.e., each element is greater than or equal to the preceding element in the subsequence).
A subsequence is a sequence that can be derived from the original array by deleting some or no elements without changing the relative order of the remaining elements. Note that a subsequence is different from a subarray—the elements in a subsequence need not be contiguous.
Key Requirements:
subsequence[i] ≤ subsequence[i+1] for all valid indices.This problem tests your ability to generate combinations using backtracking while handling duplicates efficiently. The challenge lies in avoiding redundant subsequences when the input array contains repeated values.
nums = [4,6,7,7][[4,6],[4,7],[6,7],[7,7],[4,6,7],[4,7,7],[6,7,7],[4,6,7,7]]All valid ascending subsequences with at least 2 elements are listed. Notice that [7,7] is valid since equal elements satisfy the ascending condition. Each subsequence maintains the relative order from the original array.
nums = [4,4,3,2,1][[4,4]]Since the array is mostly descending after the first two elements, the only valid ascending subsequence with at least 2 elements is [4,4] formed by the first two equal elements.
nums = [1,2,3][[1,2],[1,3],[2,3],[1,2,3]]For a strictly increasing array, we can form subsequences of various lengths. All possible combinations of 2 or more elements are valid since the array is already sorted in ascending order.
Constraints