Non decreasing subsequences

class Solution:
    def __init__(self):
        self.path = []
        self.res = []

    def backtrack(self, nums, startIndex):
        if len(self.path) > 1:
            self.res.append(self.path.copy())
        usedSet = set()
        for i in range(startIndex, len(nums)):
            if nums[i] in usedSet:
                continue
            if len(self.path) > 0 and self.path[-1] > nums[i]:
                continue
            usedSet.add(nums[i])
            self.path.append(nums[i])
            self.backtrack(nums, i + 1)
            self.path.pop()


    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
        self.backtrack(nums, 0)
        return self.res

Non-decreasing Subsequences

Difficulty: Medium


Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

 

Example 1:

Input: nums = [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]]

Example 2:

Input: nums = [4,4,3,2,1]
Output: [[4,4]]

 

Constraints:

  • 1 <= nums.length <= 15
  • -100 <= nums[i] <= 100