Skip to content

Latest commit

 

History

History
122 lines (103 loc) · 4.29 KB

90.Subsets_II(Medium).md

File metadata and controls

122 lines (103 loc) · 4.29 KB

90. Subsets II (Medium)

Date and Time: Jul 27, 2024, 15:04 (EST)

Link: https://leetcode.com/problems/subsets-ii/


Question:

Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.


Example 1:

Input: nums = [1,2,2]

Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]

Example 2:

Input: nums = [0]

Output: [[],[0]]

Edge case:

Input: nums = [2,2,2,2]

Output: [[],[2],[2,2],[2,2,2],[2,2,2,2]]


Constraints:

  • 1 <= nums.length <= 10

  • -10 <= nums[i] <= 10


KeyPoints:

Similar to 78. Subsets, but in this question we will have duplicate element and not sorted array. So the first thing we need to do is to sort nums, then we can use backtrack() to append subset[] into res, when there is a duplicate element exists, we use while loop to check if 1. i + 1 is in bound. 2. nums[i] == nums[i+1]. and we self-increment i and continue the backtrack.


Python Solution:

class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        res, subset = [], []
        nums.sort() # Sort to prevent duplicates at different places
        def backtrack(i):
            if i >= len(nums):
                res.append(subset.copy())
                return
            subset.append(nums[i])
            backtrack(i+1)
            subset.pop()
            # Skip duplicates
            while i+1 < len(nums) and nums[i] == nums[i+1]:
                i += 1
            backtrack(i+1)
        backtrack(0)
        return res

Time Complexity: $O(n* 2^n)$
Space Complexity: $O(n * 2^n)$

Java Solution:

class Solution {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> subset = new ArrayList<>();
        Arrays.sort(nums);
        backtrack(nums, 0, res, subset);
        return res;
    }
    private void backtrack(int[] nums, int index, List<List<Integer>> res, List<Integer> subset) {
        if (index == nums.length) {
            res.add(new ArrayList<>(subset));
            return;
        }
        subset.add(nums[index]);
        backtrack(nums, index+1, res, subset);
        subset.remove(subset.size()-1);
        while (index + 1 < nums.length && nums[index] == nums[index+1]) {
            index++;
        }
        backtrack(nums, index+1, res, subset);
    }
}

C++ Solution:

class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> subset;
        std::sort(nums.begin(), nums.end());
        backtrack(nums, 0, res, subset);
        return res;
    }
    void backtrack(vector<int>& nums, int index, vector<vector<int>>& res, vector<int> subset) {
        if (index == nums.size()) {
            res.push_back(subset);
            return;
        }
        subset.push_back(nums[index]);
        backtrack(nums, index+1, res, subset);
        subset.pop_back();
        while (index + 1 < nums.size() && nums[index] == nums[index+1]) {
            index++;
        }
        backtrack(nums, index+1, res, subset);
    }
};

CC BY-NC-SABY: credit must be given to the creatorNC: Only noncommercial uses of the work are permittedSA: Adaptations must be shared under the same terms