Day 87 | Subsets II

Day 87 | Subsets II

A couple days ago, I solved a problem called subsets. Today I solved a problem that built off that last problem. The two problems are extremely similar with only one difference: the input array can now include duplicates. What makes this problem a little more difficult is the fact that the solution must not contain duplicate subsets.

The Approach

If the solution from the first problem is used, duplicate subsets would exist in the output array and here is why.

Ex: [1,2,2,3]

With the backtracking method that was used earlier, you essentially pick to include or not include each number in the list. So you start with one, you create a subset that includes the one and doesn't. Then with the one, you create a subset that includes the 2 and another that doesn't. You do that again with the next two to end up with [1,2,2] and [1,2] for including and not including the second two. The problem arises once you backtrack to the subset that includes the one but not the first two. You would now choose to either include or not include the second two to that subset to end up with [1] and [1,2]. The [1,2] is now a duplicate subset which is a problem.

The way we can counteract this issue is that if we choose to not include a number in a particular subset, we must make sure that it is never included again in it's children. So with the earlier example, we have the subset which is [1]. We now create two new subsets that either include or doesn't include the first two. So we end up with [1] and [1,2]. Now for [1], we want to make sure we skip the second two and go on to create new subsets that either include or not include the three.

The Code

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        res = []
        nums.sort()
        def backtrack(i,subset):
            if i == len(nums):
                res.append(subset[::])
                return
            subset.append(nums[i])
            backtrack(i+1, subset)
            subset.pop()
            while i + 1 < len(nums) and nums[i] == nums[i+1]:
                i += 1
            backtrack(i+1, subset)
        backtrack(0, [])
        return res
                    

This solution is almost the same as the solution for the first problem. The only difference here is the while loop that skips the consecutive same numbers to make sure we don't create duplicate subsets. The solution also starts out with sorting the input array so it's in a form that works for this solution.

Complexity Analysis

The time complexity of this solution is O(2^n). The reason why this is the case is that for each number, we are either including the number or not including it. This means that the backtrack method is going to be called 2^n times.

The space complexity of this solution is also going to be O(2^n) again because the response is going to include 2^n elements as again we are either choosing to include or not include a specific number.

要查看或添加评论,请登录

Haroon Almadani的更多文章

  • Day 98 | Course Schedule

    Day 98 | Course Schedule

    Today's problem was a little different from the other graph problems I've solved so far. While usually I get a grid or…

  • Day 97 | Surrounded Regions

    Day 97 | Surrounded Regions

    In this problem, you are given a 2-D grid. This grid contains "X" an "O" characters.

  • Day 96 | Pacific Atlantic Water Flow

    Day 96 | Pacific Atlantic Water Flow

    Todays graph problem took me a little bit of time to understand. You are given a rectangular island heights where each…

  • Day 95 | Rotten Oranges

    Day 95 | Rotten Oranges

    Today I solved a problem that was practically the same as yesterdays problem with a little variation. You are given an…

  • Day 94 | Islands and Treasure

    Day 94 | Islands and Treasure

    Today I solved a problem involving a matrix again. You are given a m x n matrix that can consist of only 3 possible…

  • Day 93 | Clone Graph

    Day 93 | Clone Graph

    The last two graph problems I've solved involved an adjacency matrix representation of a graph. This problem was a…

  • Day 92 | Max Area of Islands

    Day 92 | Max Area of Islands

    Today's problem heavily built off yesterdays problem. I recommend solving number of islands first before tackling this…

  • Day 91 | Number of Islands

    Day 91 | Number of Islands

    Today I've moved on from backtracking problems. I'm onto graph problems! This problem involved counting the number of…

  • Day 90 | Letter Combinations of a Phone Number

    Day 90 | Letter Combinations of a Phone Number

    Today's problem was a pretty fun problem. It feels like it's combined everything I've learned in the past couple of…

  • Day 89 | Palindrome Partitioning

    Day 89 | Palindrome Partitioning

    Palindrome Partitioning was a problem that I initially had troubles wrapping my head around. Most backtracking problems…

社区洞察

其他会员也浏览了