Skip to content

Commit 4c8cc3c

Browse files
author
Prashant Jain
committed
Added problem in Backtracking and DynamicProgramming
1 parent 27f96ed commit 4c8cc3c

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package in.knowledgegate.dsa.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* SAME PROBLEM IS SOLVED IN DP ALSO
8+
* <p>
9+
* Given an integer array nums of unique elements,
10+
* return all possible subsets (the power set).
11+
* <p>
12+
* The solution set must not contain duplicate
13+
* subsets. Return the solution in any order.
14+
* <p>
15+
* Example 1: Input: nums = [1,2,3] Output:
16+
* [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
17+
* <p>
18+
* Example 2: Input: nums = [0] Output: [[],[0]]
19+
* <p>
20+
* Constraints: 1 <= nums.length <= 10 -10 <=
21+
* nums[i] <= 10 All the numbers of nums are
22+
* unique.
23+
*/
24+
public class Subsets {
25+
public static void main(String[] args) {
26+
Subsets subsets = new Subsets();
27+
int[] nums = new int[]{};
28+
System.out.println("PowerSet is :" +
29+
subsets.subsets(nums));
30+
}
31+
32+
public List<List<Integer>> subsets(int[] nums) {
33+
List<List<Integer>> output = new ArrayList<>();
34+
for (int i = 0; i <= nums.length; i++) {
35+
backtrack(output, i, 0, new ArrayList<>(),
36+
nums);
37+
}
38+
return output;
39+
}
40+
41+
private void backtrack(List<List<Integer>> output,
42+
int length, int beg, List<Integer> curr,
43+
int[] nums) {
44+
45+
if (curr.size() == length) {
46+
output.add(new ArrayList<>(curr));
47+
return;
48+
}
49+
50+
for (int i = beg; i < nums.length; i++) {
51+
curr.add(nums[i]);
52+
backtrack(output, length, i + 1, curr,
53+
nums);
54+
curr.remove(curr.size() - 1);
55+
}
56+
}
57+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package in.knowledgegate.dsa.dynamicprogramming;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* SAME PROBLEM IS SOLVED IN BACKTRACKING ALSO
8+
* <p>
9+
* Given an integer array nums of unique elements,
10+
* return all possible subsets (the power set).
11+
* <p>
12+
* The solution set must not contain duplicate
13+
* subsets. Return the solution in any order.
14+
* <p>
15+
* Example 1: Input: nums = [1,2,3] Output:
16+
* [[],[1],[2],[1,2],[3],[1,3],[2,3], [1,2,3]]
17+
* <p>
18+
* Example 2: Input: nums = [0] Output: [[],[0]]
19+
* <p>
20+
* Constraints: 1 <= nums.length <= 10 -10 <=
21+
* nums[i] <= 10 All the numbers of nums are
22+
* unique.
23+
*/
24+
public class Subsets {
25+
public static void main(String[] args) {
26+
Subsets subsets = new Subsets();
27+
int[] nums = new int[]{1, 2, 3, 4};
28+
System.out.println("PowerSet is:" +
29+
subsets.subsets(nums));
30+
}
31+
32+
public List<List<Integer>> subsets(int[] nums) {
33+
List<List<Integer>> res = new ArrayList<>();
34+
res.add(new ArrayList<>());
35+
36+
for (int num : nums) {
37+
int length = res.size();
38+
for (int i = 0; i < length; i++) {
39+
List<Integer> copy =
40+
new ArrayList<>(res.get(i));
41+
copy.add(num);
42+
res.add(copy);
43+
}
44+
}
45+
return res;
46+
}
47+
}

0 commit comments

Comments
 (0)