-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy pathsubsets.java
69 lines (54 loc) · 2.16 KB
/
subsets.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
BACKTRACKING PROBLEM!!!
INTUTION, subsets are just you either TAKE the element we are on or DONT TAKE the element
Therefore, timecomplexity should be O(2^n) because every element can be absent or present
//TC: O(n*2^n) for all the recursive calls where we will have a total of 2^n permutations,
//and copying each list to output takes an extra O(n) time in each loop,
//SC: O(n*2^n) because there are 2^n subsets for length N, and every subset needs O(n) space to store
//the temp array
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
generateSubsets(0, nums, new ArrayList<Integer>(), ans);
return ans;
}
public void generateSubsets(int index, int[] nums, List<Integer> current, List<List<Integer>> subsets){
subsets.add(new ArrayList<>(current)); //FIRST THING TO DO IS ADD CURRENT SUBSET to our answer, need this to be NEW
//because we are going to modify this current
//simulate TAKING and NOT TAKING, current number we are on
for(int i=index; i<nums.length; i++){
current.add(nums[i]); //TAKING, just add current number to our current subset
generateSubsets(i+1, nums, current, subsets);
current.remove(current.size()-1); //BACKTRACK, simulate NOT TAKING IT and go back
}
}
}
//SAME CODE CLEANER
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){
list.add(new ArrayList<>(tempList));
for(int i = start; i < nums.length; i++){
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}