DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

2 1

Increasing Subsequences

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

The given array may contain duplicates, and two equal integers should also be considered a special case of increasing sequence.

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

SOLUTION:

class Solution:
    def getSeq(self, nums, i, n):
        if i >= n:
            return [[]]
        op = []
        used = set()
        for j in range(i + 1, n):
            if nums[j] >= nums[i] and nums[j] not in used:
                val = self.getSeq(nums, j, n)
                op += [[nums[i], nums[j]]]
                op += [[nums[i]] + v for v in val]
                used.add(nums[j])
        return op

    def findSubsequences(self, nums: List[int]) -> List[List[int]]:
        n = len(nums)
        op = []
        used = set()
        for i in range(n):
            if nums[i] not in used:
                op += self.getSeq(nums, i, n)
                used.add(nums[i])
        return op
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay