-
Notifications
You must be signed in to change notification settings - Fork 31
/
CombinationsSum2.java
51 lines (41 loc) · 1.36 KB
/
CombinationsSum2.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
package Backtracking;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 27/10/18
* Time - 12:51 PM
*/
public class CombinationsSum2 {
public void addCombSum(ArrayList<ArrayList<Integer>> r, ArrayList<Integer> input, int pos,
int currentSum, int reqSum, ArrayList<Integer> temp, Map<ArrayList<Integer>,Boolean> map){
if(currentSum == reqSum){
if(!map.containsKey(temp)){
ArrayList<Integer> t = new ArrayList<>(temp);
r.add(t);
map.put(t,true);
}
return;
}
else if(currentSum > reqSum){
return;
}
for(int i=pos;i<input.size();i++){
currentSum += input.get(i);
temp.add(input.get(i));
addCombSum(r,input,i+1,currentSum,reqSum,temp,map);
currentSum -= input.get(i);
temp.remove(temp.size()-1);
}
}
public ArrayList<ArrayList<Integer>> combinationSum(ArrayList<Integer> a, int b) {
ArrayList<ArrayList<Integer>> r = new ArrayList<>();
ArrayList<Integer> temp = new ArrayList<>();
Collections.sort(a);
Map<ArrayList<Integer>,Boolean> map = new HashMap<>();
addCombSum(r,a,0,0,b,temp,map);
return r;
}
}