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 };
  • 相关阅读:
    LINQ to Entities 查询中的标准查询运算符
    LINQ to Entities 基于方法的查询语法
    ajax 与 form 提交的区别
    i++ & ++i 区别
    sizeof 数据类型大小 32位&64位
    标准数据类型宏定义
    long & int 区别
    类函数修饰 const
    指针
    数组
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3474947.html
Copyright © 2011-2022 走看看