Combination Sum LeetCode

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

  1. Input: candidates = [2,3,6,7], target = 7
  2. Output: [[2,2,3],[7]]
  3. Explanation:
    1. 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations.
Example 2:
  • Input: candidates = [2,3,5], target = 8
  • Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
  • Input: candidates = [2], target = 1
  • Output: []
Example 4:
  • Input: candidates = [1], target = 1
  • Output: [[1]]
Example 5:
  • Input: candidates = [1], target = 2
  • Output: [[1,1]]

NOTE:

  • 1 <= candidates.length <= 30
  • 1 <= candidates[i] <= 200
  • All elements of candidates are distinct.
  • 1 <= target <= 500
This problem is popular in LeetCode and GeeksForGeeks A collection of hundreds of interview questions and solutions are available in our blog at Interview Question

Solution: Java

Solution

/**
* Given a set of candidate numbers (C) and a target number (T),
* find all unique combinations in C where the candidate numbers sums to T.
*
* The same repeated number may be chosen from C unlimited number of times.
*
* Note:
* All numbers (including target) will be positive integers.
* Elements in a combination (a1, a2, ... , ak) must be in non-descending order. (ie, a1 &le; a2 &le; &le; ak).
* The solution set must not contain duplicate combinations.
* For example, given candidate set 2,3,6,7 and target 7,
* A solution set is:
* [7]
* [2, 2, 3]
*/
package com.alg.leetup;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author rbaral
*
*/
public class CombinationSum {
/**
* @param args
*/
public static void main(String[] args) {
int candidates[] = new int[]{2, 3, 6, 7};
int target = 7;
//candidates = new int[]{1,2};
//target = 3;
List<List<Integer>> results = combinationSum(candidates, target);
for (List<Integer> result : results) {
System.out.println(Arrays.toString(result.toArray()));
}
}
/**
* finds the combination sum for the target in the array with positive
* numbers
*
* @param candidates
* @param target
* @return
*/
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
//base case, if target is 0, then we can get division by zero error
if (target == 0) {
result.add(new ArrayList<Integer>());
return result;
} else if (target < 0) {
return result;//return empty
}
int quot = 0;//keep track of quotient
int rem = 0;//keep track of reminder
int count = 0;
List<Integer> combination = new ArrayList<Integer>();
for (int i = 0; i < candidates.length; i++) {
if (target % candidates[i] == 0) {
quot = target / candidates[i];
count = 0;
combination = new ArrayList<Integer>();
while (count < quot) {
combination.add(candidates[i]);
count++;
}
Arrays.sort(combination.toArray());
if (!result.contains(combination)) {
result.add(combination);
}
} else {
rem = target % candidates[i];
quot = target / candidates[i];
//check if we can get the reminder or (candidates[i]+reminder) from other numbers
for (int j = 0; j < candidates.length; j++) {
if (rem == candidates[j]) {//we found the combination
count = 0;
combination = new ArrayList<Integer>();
combination.add(rem);
while (count < quot) {
combination.add(candidates[i]);
count++;
}
if (!result.contains(combination)) {
result.add(combination);
}
} else if (candidates[j] == (candidates[i] + rem)) {
count = 0;
combination = new ArrayList<Integer>();
while (count < (quot - 1)) {
combination.add(candidates[i]);
count++;
}
combination.add(candidates[j]);
if (!result.contains(combination)) {
result.add(combination);
}
}
}
}
}
return result;
}
}

No comments:

Post a Comment