forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path90.cpp
More file actions
45 lines (35 loc) · 1.14 KB
/
90.cpp
File metadata and controls
45 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
//decision is to include the repeated element how many times, 0,1,2,..
void helper(vector<int> &nums,int startIdx, vector<vector<int>> &res,vector<int> currSet){
//base case
int n= nums.size();
if (startIdx==n){
res.push_back(currSet);
return;
}
//recursive case
int i,j;
i= startIdx+1;
//find the non-repeating index
while (i<n && nums[i]==nums[i-1]) i++;
j=i;
//include
helper(nums,j,res,currSet);
//more than 1 times
for (i=startIdx;i<j;i++){
currSet.push_back(nums[i]);
helper(nums,j,res,currSet);
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int>> res;
vector<int> currSet;
sort(nums.begin(),nums.end());
helper(nums,0,res,currSet);
return res;
}
};
//=================================
//Time complexity for the above algorithm asymptotically: O(2^n) where n is the size of the input array
//Space complexity: O(n)