zoukankan      html  css  js  c++  java
  • LeetCode

    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 };
  • 相关阅读:
    vi文本编辑器常用指令功能
    如何利用U盘进行重装win10系统(2020.11亲测可行)
    技术文档:用户和文件权限管理
    把树莓派变身无线AP
    循环控制结构程序03 零基础入门学习C语言18
    循环控制结构程序03 零基础入门学习C语言18
    循环控制结构程序01 零基础入门学习C语言16
    循环控制结构程序04 零基础入门学习C语言19
    循环控制结构程序01 零基础入门学习C语言16
    循环控制结构程序02 零基础入门学习C语言17
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3474947.html
Copyright © 2011-2022 走看看