题目:

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 ≤ a2 ≤ … ≤ 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]

链接:   http://leetcode.com/problems/combination-sum/

题解:

还是一道DFS + Backtracking题。 这次是考察重复元素如何处理。依然使用回溯的template。

Time Complexity - O(), Space Complexity - O()

public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if(candidates == null || candidates.length == 0)
return res;
Arrays.sort(candidates);
ArrayList<Integer> list = new ArrayList<>();
dfs(res, list, candidates, target, 0);
return res;
} private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] candidates, int target, int pos) {
if(target < 0)
return;
if(target == 0) {
res.add(new ArrayList<Integer>(list));
return;
} for(int i = pos; i < candidates.length; i++) {
if(candidates[i] > target)
return;
if(i > 0 && candidates[i] == candidates[i - 1])
continue;
list.add(candidates[i]);
dfs(res, list, candidates, target - candidates[i], i);
list.remove(list.size() - 1);
}
}
}

二刷:

典型的的dfs + backtracking, 一刷到现在有很长时间了,重新做到这题, 才会补充思考以前的不足. 这遍在想能不能用其他的方法, 比如dp或者是类似双指针之类的, 不过可惜最后还是选了这个熟悉的方法.

这里我们主要要注意的是用来回溯的helper方法如何设计, 在我用的方法里, 需要传入已有的变量有

  1. List<List<Integer>> res,这个是我们的结果变量,只有找到满足条件的combination组合之后才会用到
  2. List<Integer> combination, 这个是我们用来进行回溯用到的主buffer, 根据dfs的深度增加或者减少元素
  3. int[] candidates, 这个是题目给定的数组,我们先对其进行sort,然后从小到大遍历来选择合适的元素。这里由于可以不限量选取元素,所以假如candidates中有 <= 0的数就会stack overflow,所以题目中给定元素全是正整数。
  4. int target, 这个是我们的目标值,  dfs的时候传入下一层是 target - num
  5. int pos, 这个是我们的position参数,因为题目要求必须从小到大排序,并且不能有重复,所以我们用pos函数来控制每层dfs不遍历之前已经跳过或者遍历过的元素

接下来就是复杂度分析。这道题的复杂度分析起来比较复杂....一刷的时候就没有好好思考。

首先我们假设这个数组candidate长为n, 在其中有m个元素小于target,假设T(n) = find(target),那么我们可以根据程序得到以下分析结果:

  1. DFS Level 0: 我们首次调用辅助函数, 这一层的count = 1
  2. DFS Level 1: 这时候我们进入了辅助函数的for循环,在for循环里我们有一个pruning,当candidate[i] > target的时候,返回,所以这一层我们只对小于target的元素进行下一层DFS,如我们假设的,结果为m
  3. DFS Level 2: 根据代码,我们上一层有一个target -= num,所以这一层的target其实都不一样。
    1. 假设我们对candidate中最小的元素min进行分析,这时候新的target1 = target - min,此时我们要继续计算在candidate数组中有多少元素小于新的target1,假设这个数目为m1,则在这一层我们要对小于target1的m1个数组进行下一层的DFS
    2. 假如我们考虑其他非min的元素来计算总的复杂度时, 这时候两层总共要进行1 + m(m1 + m2 + m3 + ... + mn)次调用。
  4. 由于每一层的target和m都在改变,以我的水平比较难计算出一个漂亮的公式,那么我就想办法简化一下:
    1. 这里递归的最大深度是d= target / min, 就是最深我们可以到 target / min这么多层DFS
    2. Branching factor b = m, m是candidate数组里小于等于target的distinct元素的个数。 其实这里多算了,每一层的m都在减少,而且是不规则减少
    3. 我们利用DFS公式可以算出这里的Time Complexty = O(md),  Space Complexity = O(m)
    4. 这只是一个worst case scenario, 更精确的计算还需要再花时间。

Java:

Time Complexity - O(md),  Space Complexity - O(m),   m is distinct elements in candidate[] which candidate[i] <=  target,  d = target / min element in candidate[]

public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return res;
}
Arrays.sort(candidates);
List<Integer> comb = new ArrayList<>();
combinationSum(res, comb, candidates, target, 0);
return res;
} private void combinationSum(List<List<Integer>> res, List<Integer> comb, int[] candidates, int target, int pos) {
if (target < 0) {
return;
} else if (target == 0) {
res.add(new ArrayList<>(comb));
}
for (int i = pos; i < candidates.length; i++) {
int num = candidates[i];
if (num > target) {
return;
}
comb.add(num);
combinationSum(res, comb, candidates, target - num, i);
comb.remove(comb.size() - 1);
}
}
}

题外话:

1/22/2016

看了地里一些Amazon的Video题目...感觉现在的new graduate孩子们好幸福,随便刷刷题就可以拿offer了,羡慕。 但是在看leetcode的时候也发现有些大牛刷题时间有半年甚至1年。有些题目question提问时间是2014年,但在15年下半年还很活跃。在cnblogs里也看到一些人刷题刷了4遍+的。 我也还是先好好刷题把,刷到五遍再出关。现在才刚第二遍开头,吃不到葡萄,不要说葡萄酸。

美东这个周末据说会有很大的暴风雪, winter storm Jonas, 降雪量可能有10 - 18 inches,幸好周四提前买了好多吃喝备足了。车子加油,也有好几个加油站都卖空了。希望情况不要太严重。

三刷:

方法跟二刷一样。没有特意分析复杂度。

Java:

public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null) return res;
Arrays.sort(candidates);
getCombinations(res, new ArrayList<>(), candidates, target, 0);
return res;
} private void getCombinations(List<List<Integer>> res, List<Integer> list, int[] nums, int target, int pos) {
if (target < 0) return;
if (target == 0) {
res.add(new ArrayList<>(list));
return;
}
for (int i = pos; i < nums.length; i++) {
if (nums[i] > target) break; // Pruning
if (i > pos && nums[i] == nums[i - 1]) continue; // Pruning
list.add(nums[i]);
getCombinations(res, list, nums, target - nums[i], i);
list.remove(list.size() - 1);
}
}
}

Reference:

http://www.cis.upenn.edu/~matuszek/cit594-2012/Pages/backtracking.html

http://www.fas.harvard.edu/~cscie119/lectures/recursion.pdf

http://www3.cs.stonybrook.edu/~algorith/               <-  Backtracking Template

https://leetcode.com/discuss/37071/accepted-16ms-c-solution-use-backtracking-easy-understand

https://leetcode.com/discuss/10141/a-solution-avoid-using-set

https://leetcode.com/discuss/23818/iterative-java-dp-solution

https://leetcode.com/discuss/59204/easy-to-understand-96%25-performance-java-solution

https://leetcode.com/discuss/42670/very-elegant-python-code-using-recursive-yield-iterator

https://leetcode.com/discuss/7181/what-time-complexity-recursive-solution-this-problem-how-get

http://yucoding.blogspot.com/2012/12/leetcode-question-16-combination-sum.html

http://zhaonanleetcode.blogspot.com/2014/06/leetcode-combination-sum-ii.html

39. Combination Sum的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  2. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  3. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  4. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  5. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  6. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  7. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  8. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  9. LeetCode笔记:39. Combination Sum

    题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...

随机推荐

  1. windows7 64bit下安装Oracle 11g R2

    Win7 bit64,安装的是64位的客户端.   1.PLSql连接数据库   (1)下载 instantclient-basic-win32-11.2.0.1.0.zip解压到Oracle要目当下 ...

  2. 将开始我的WebForm控件开发之旅

    时间总是过得很快,一转眼三个月就过去了,三个月内发生了很多的事.因为学校的学习,离开了我入门WPF的公司:开发了第一个外包项目,做的是WebForm的:而且了马上要毕业了,毕业后的公司应该是专门用We ...

  3. Microsoft Press Free eBook

    微软的免费的电子书, 都是Microsoft Press 出版的 有以下价格方面 Windows Server(大体上都是Windows Server 2012 ) Microsoft Azure(好 ...

  4. cdev成员结构体file_operations文件操作结构的分析

    struct file_operations{ struct module *owner; // 指向拥有该结构的模块的指针,避免正在操作时被卸载,一般为初始化为THIS_MODULES loff_t ...

  5. Thinkphp模板中使用自定义函数的方法

    注意:自定义函数要放在项目应用目录/common/common.php中. 这里是关键. 模板变量的函数调用格式:{$varname|function1|function2=arg1,arg2,### ...

  6. Java并发编程:Lock(上)

    在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.locks包下提供了另外一种方 ...

  7. centos 64位linux系统下安装appt命令

    首先,安装apktool包 1. wget http://android-apktool.googlecode.com/files/apktool-install-linux-r04-brut1.ta ...

  8. 基于HOOK和MMF的Windows密码渗透技术

    随着计算机与网络的普及,信息安全越来越成为人们所普遍关心的大事.密码的渗透与反渗透在此领域表现的愈演愈烈.本文深入分析了各个版本Windows密码的特点,尤其是针对windws2K/XP安全性提高的情 ...

  9. Linux操作系统下软件的安装方法大全

    一.rpm包安装方式步骤: 1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd soft.version.rpm所在 ...

  10. spring @resource @ Autowired

    Spring中什么时候用@Resource,什么时候用@service 当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类 ...