随笔- 509  文章- 0  评论- 151  阅读- 22万 

Combination Sum

2013.12.15 03:10

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 (a1a2, … , 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] 

Solution:

  Given a set of candidate numbers, find all the combinations that add up to a target value. Every element can be used infinite times.

  Since every element can be used unlimited times, the first step will be to remove the duplicates from the array.

  Next step, sort the array.

  Then do the DFS, with proper pruning technique.

  Time complexity is roughly O(n!), where n is number of elements in the unique and sorted array. Space complexity is O(n), which is required by the unique and sorted array. Some bad cases can be very tricky to handle, guess my code here won't solve them...

Accepted code:

复制代码
 1 // 1WA, 1OLE, 1AC, dfs trimming condition is a weak point.
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
 8         // Note: The Solution object is instantiated only once and is reused by each test case.
 9         int i, j, n;
10         
11         n = result.size();
12         for(i = 0; i < n; ++i){
13             result[i].clear();
14         }
15         result.clear();
16         
17         sort(candidates.begin(), candidates.end());
18         v.clear();
19         i = 0;
20         n = candidates.size();
21         // remove duplicates from candidates
22         while(i < n){
23             j = i + 1;
24             while(j < n && candidates[i] == candidates[j]){
25                 ++j;
26             }
27             v.push_back(candidates[i]);
28             i = j;
29         }
30         
31         arr.clear();
32         n = v.size();
33         dfs(0, n, 0, target);
34         
35         return result;
36     }
37 private:
38     vector<int> v;
39     vector<int> arr;
40     vector<vector<int>> result;
41     
42     void dfs(int idx, int n, int sum, int target) {
43         int i, j, k;
44         
45         if(sum == target){
46             result.push_back(arr);
47             return;
48         }
49         
50         // 1WA here, if(idx >= n || sum > target) is wrong, must check $sum first.
51         if(sum > target){
52             return;
53         }
54 
55         for(i = idx; i < n; ++i){
56             for(j = 1; sum + v[i] * j <= target; ++j){
57                 for(k = 0; k < j; ++k){
58                     arr.push_back(v[i]);
59                 }
60                 dfs(i + 1, n, sum + v[i] * j, target);
61                 for(k = 0; k < j; ++k){
62                     // 1OLE here, $arr, not $v
63                     arr.pop_back();
64                 }
65             }
66         }
67     }
68 };
复制代码

 

 posted on   zhuli19901106  阅读(1375)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示