LeetCode Combination Sum & Combination Sum II

Combination Sum

Description:

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.

Solution:

The key idea of solving this problem is surely to list all the possible solutions.

And there are two common ways to get this: 

1. DP(dynamic programming)

we use a 2-dimensional array, like dp[i], represents that when the sum is i, there are dp[i] possible ways. And when we use a 2-dimensional LinkedList, we can surely use LinkedList.get(i)

the initial condition is dp[0] = "" or true; and  that dp[i+candidates[j]] = dp[i] + "j"; and some statements similar to this.

2. DFS

we can tell from the above that the time complexity if very large for DP because we have to record all the inter-statement in the whole process. and in DFS, we can spare this action.

DFS is very similar, one difference is that we have a temporary variable representing the current combination. And when it comes to the boundary condition, it will pause, and add this current combination to final result.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution {
	List<List<Integer>> list;

	public List<List<Integer>> combinationSum(int[] candidates, int target) {
		list = new ArrayList<List<Integer>>();
		Arrays.sort(candidates);

		int limit = (int) Math.ceil(1.0 * target / candidates[0]);

		ArrayList<Integer> l = new ArrayList<Integer>();
		dfs(0, 0, 0, limit, l, candidates, target);
		return list;
	}

	void dfs(int currentSum, int tot, int currentNum, int limitNum,
			ArrayList<Integer> str, int[] nums, int target) {
		if (currentSum > target) {
			return;
		}
		if (tot == nums.length) {
			if (currentSum == target) {
				list.add(new ArrayList<>(str));
			}
			return;
		}

		int tempSum;
		for (int i = currentNum; i <= limitNum; i++) {
			tempSum = currentSum + nums[tot] * (i - currentNum);
			if (i > currentNum)
				str.add(nums[tot]);
			dfs(tempSum, tot + 1, i, limitNum, str, nums, target);
		}
		for (int i = currentNum; i < limitNum; i++) {
			str.remove(str.size() - 1);
		}
	}
}


Combination Sum II

 

Description:

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Solution:

Just like the above problem, we use DFS to solve this problem.

One difference is that each number in C may only appear once.

To get this, in each layer of DFS, we use a for loop to search all the number with same value at once, then DFS on them.

<span style="font-size:18px;">import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution {
	List<List<Integer>> list;

	public List<List<Integer>> combinationSum2(int[] candidates, int target) {
		list = new ArrayList<List<Integer>>();
		Arrays.sort(candidates);
		ArrayList<Integer> l = new ArrayList<Integer>();
		dfs(0, 0, l, candidates, target);
		return list;
	}

	void dfs(int currentSum, int tot, ArrayList<Integer> str, int[] nums,
			int target) {
		if (currentSum > target) {
			return;
		}
		if (tot == nums.length) {
			if (currentSum == target) {
				list.add(new ArrayList<>(str));
			}
			return;
		}

		int bound = tot + 1;
		for (int i = tot + 1; i < nums.length; i++) {
			if (nums[i] == nums[tot]) {
				if (i + 1 == nums.length) {
					bound = nums.length;
				}
				continue;
			}
			bound = i;
			break;
		}

		dfs(currentSum, bound, str, nums, target);
		for (int i = tot; i < bound; i++) {
			str.add(nums[tot]);
			currentSum += nums[tot];
			dfs(currentSum, bound, str, nums, target);
		}
		for (int i = tot; i < bound; i++) {
			str.remove(str.size() - 1);
			currentSum -= nums[tot];
		}
	}
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值