> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Non-decreasing Subsequences Python Solution

> Tested Python solution for LeetCode 491 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 491, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Backtracking](/catalog/topics/backtracking), [Bit Manipulation](/catalog/topics/bit-manipulation). [View on LeetCode](https://leetcode.com/problems/non-decreasing-subsequences/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 491   # by problem number
lcpy gen -s non_decreasing_subsequences   # by problem name
```

## Problem

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**.

### Examples

```
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]]
```

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/non_decreasing_subsequences/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/non_decreasing_subsequences/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(2^n * n)
    # Space: O(n) recursion depth (output excluded)
    def find_subsequences(self, nums: list[int]) -> list[list[int]]:
        result: list[list[int]] = []
        path: list[int] = []

        def backtrack(start: int) -> None:
            if len(path) >= 2:
                result.append(path.copy())
            seen: set[int] = set()
            for i in range(start, len(nums)):
                if nums[i] in seen or (path and nums[i] < path[-1]):
                    continue
                seen.add(nums[i])
                path.append(nums[i])
                backtrack(i + 1)
                path.pop()

        backtrack(0)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(2^n \* n) | O(n) recursion depth (output excluded) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.