xxxxxxxxxx
//User function template for JAVA
class Pair{
int first;
int second;
public Pair(int i,int j){
first=i;
second=j;
}
@Override
public boolean equals(Object o){
if(this==o)return true;
if(o==null)return false;
if(this.getClass()!=o.getClass())return false;
Pair oo=(Pair)o;
return oo.first==this.first&&oo.second==this.second;
}
@Override
public int hashCode(){
// return first.hashCode()+second.hashCode();
final int prime=31;
int result=1;
result=result*prime+first;
result=result*prime+second;
return result;
}
@Override
public String toString(){
return "("+first+","+second+")";
}
}
class Solution
{
//Function to return a list of indexes denoting the required
//combinations whose sum is equal to given number.
static ArrayList<ArrayList<Integer>> combinationSum(ArrayList<Integer> A, int B){
//REMOVE DUPLICATES BECAUSE IT NOT AFFECT FINAL RESULT BASED ON THAT The same number may be chosen from the array any number of times
TreeSet<Integer> tr=new TreeSet<Integer>(A);
A.clear();
for(Integer i:tr){
A.add(i);
}
//sort
Collections.sort(A);
Pair[][] hm=new Pair[B+1][A.size()+1];//[i][j]:how many combinations to form sum i by head j elements of A
//if use TreeMap<Pair, ArrayList<ArrayList<Integer>>>, then class Pair have to implement Comparable interface
HashMap<Pair, ArrayList<ArrayList<Integer>>> m=new HashMap<Pair, ArrayList<ArrayList<Integer>>>();
for(int i=0;i<=B;i++){
for(int j=0;j<=A.size();j++){
hm[i][j]=new Pair(i,j);
}
}
//System.out.println(howMany[0][1].second);System.out.println(howMany[2][0].second);
//ArrayList<ArrayList<Integer>> z=new ArrayList<ArrayList<Integer>>();
for(int i=1;i<=B;i++){
for(int j=1;j<=A.size();j++){
ArrayList<ArrayList<Integer>> z=new ArrayList<ArrayList<Integer>>();
ArrayList<ArrayList<Integer>> y=m.get(hm[i][j-1]);//exclude jth element
if(y!=null){
z=new ArrayList<ArrayList<Integer>>(y);
}
int t=A.get(j-1);
if(i>t){
y=m.get(hm[i-t][A.size()]);//include jth element
if(y!=null){
for(ArrayList<Integer> a:y){//because arraylist implements iterable interface
if(a.get(a.size()-1)<=t){// Elements in a combination ascending
ArrayList<Integer> b=new ArrayList<Integer>(a);
b.add(t);
z.add(b);
}
}
}
}
if(i==t){
ArrayList<Integer> c=new ArrayList<Integer>();
c.add(t);
z.add(c);
}
if(z.size()!=0){
m.put(hm[i][j],z);
}
}
}
ArrayList<ArrayList<Integer>> x=m.get(hm[B][A.size()]);//new ArrayList<ArrayList<Integer>>();
if(x!=null){
Collections.sort(x,new Comparator<ArrayList<Integer>>(){
/*
@Override
public int compare(ArrayList<Integer> l1,ArrayList<Integer> l2){//because l1 and l2 already sorted
return l1.get(0)-l2.get(0);
//or --> return l1.get(0).compareTo(l2.get(0));<--compareTo in interface Comparable<Integer> implemented by Integer
}*/
@Override
public int compare(ArrayList<Integer> l1,ArrayList<Integer> l2){
int min=Math.min(l1.size(),l2.size());
for(int i=0;i<min;i++){
Integer i1=l1.get(i);
Integer i2=l2.get(i);
if(i1!=i2){
return i1.compareTo(i2); //compareTo in interface Comparable<Integer> implemented by Integer
}
}
return l1.size()<=l2.size()?-1:1;//Integer.valueOf(l1.size()).compareTo(Integer.valueOf(l2.size()));
}
});
}else{
x=new ArrayList<ArrayList<Integer>>();
}
return x;
}
}
xxxxxxxxxx
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
}
}
xxxxxxxxxx
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes){
}
xxxxxxxxxx
public class Solution {
public IList<IList<int>> CombinationSum(int[] candidates, int target) {
}
}
xxxxxxxxxx
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function(candidates, target) {
};
xxxxxxxxxx
# @param {Integer[]} candidates
# @param {Integer} target
# @return {Integer[][]}
def combination_sum(candidates, target)
end
xxxxxxxxxx
class Solution {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
}
}
xxxxxxxxxx
class Solution {
/**
* @param Integer[] $candidates
* @param Integer $target
* @return Integer[][]
*/
function combinationSum($candidates, $target) {
}
}
xxxxxxxxxx
function combinationSum(candidates: number[], target: number): number[][] {
};
xxxxxxxxxx
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
}
};
xxxxxxxxxx
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** combinationSum2(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes){
}