-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSubSet_Permutations_CombinationSum.java
121 lines (97 loc) · 3.96 KB
/
SubSet_Permutations_CombinationSum.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.leetcode.problems.medium;
import com.util.LogUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* This Class shows all examples of 3 common problems
* 1) Subsets
* <p>
* <p>
* 2) Permutations
* <p>
* <p>
* <p>
* 3) Combination Sum
*
* @author neeraj on 06/10/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
@SuppressWarnings("DuplicatedCode")
public class SubSet_Permutations_CombinationSum {
public static void main(String[] args) {
int[] nums = new int[]{1, 2, 3};
LogUtil.logIt("Finding Subset of int array .....");
System.out.println(subsets(nums));
LogUtil.logIt("Finding Subset of int array with duplicates .....");
System.out.println(subsetsWithDuplicates(new int[]{1, 2, 3, 1}));
LogUtil.logIt("Finding Permutations of int array .....");
System.out.println(permute(nums));
LogUtil.logIt("Finding Combination Sum......");
System.out.println(combinationSum(new int[]{2, 3, 6, 7}, 7));
LogUtil.logIt("Finding Combination Sum II");
}
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
subsetUtil(nums, 0, new ArrayList<>(), result);
return result;
}
private static void subsetUtil(int[] nums, int pointer, List<Integer> current, List<List<Integer>> all) {
all.add(new ArrayList<>(current));
for (int i = pointer; i < nums.length; i++) {
current.add(nums[i]); // Choose
subsetUtil(nums, i + 1, current, all); // Explore
current.remove(current.size() - 1); // Un-Choose
}
}
public static List<List<Integer>> subsetsWithDuplicates(int[] nums) {
Arrays.sort(nums); // Sorting them so that we can skip the duplicates
List<List<Integer>> result = new ArrayList<>();
subsetUtilForDuplicates(nums, 0, new ArrayList<>(), result);
return result;
}
private static void subsetUtilForDuplicates(int[] nums, int pointer, List<Integer> current, List<List<Integer>> all) {
all.add(new ArrayList<>(current));
for (int i = pointer; i < nums.length; i++) {
if (i != 0 && nums[i] == nums[i - 1]) continue;
current.add(nums[i]); // Choose
subsetUtil(nums, i + 1, current, all); // Explore
current.remove(current.size() - 1); // Un-Choose
}
}
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
permuteUtil(nums, new ArrayList<>(), result);
return result;
}
private static void permuteUtil(int[] nums, List<Integer> current, List<List<Integer>> all) {
if (current.size() == nums.length) {
all.add(new ArrayList<>(current));
return;
}
for (int i = 0; i < nums.length; i++) {
if (current.contains(nums[i])) continue;
current.add(nums[i]); // Choose
permuteUtil(nums, current, all); // Explore
current.remove(current.size() - 1); // Un-choose
}
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
combinationSumUtil(candidates, 0, target, new ArrayList<>(), result);
return result;
}
private static void combinationSumUtil(int[] candidates, int pointer, int target, List<Integer> combination, List<List<Integer>> allCombination) {
if (target < 0) return;
if (target == 0) {
allCombination.add(new ArrayList<>(combination));
}
for (int i = pointer; i < candidates.length; i++) {
combination.add(candidates[i]);
// Not incrementing i, since number can be re-used
combinationSumUtil(candidates, i, target - candidates[i], combination, allCombination);
combination.remove(combination.size() - 1);
}
}
}