• 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 };
  • 相关阅读:
    SpringBoot2.0 整合 Dubbo框架 ,实现RPC服务远程调用
    Spark家族:Win10系统下搭建Scala开发环境
    Linux系统:centos7下搭建Rocketmq4.3中间件,配置监控台
    Linux系统:Centos7环境搭建Redis单台和哨兵集群环境
    Linux系统:常用Linux系统管理命令总结
    Linux系统:centos7下安装Jdk8、Tomcat8、MySQL5.7环境
    Linux系统:centos7下搭建ZooKeeper3.4中间件,常用命令总结
    SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
    SpringBoot2.0 基础案例(17):自定义启动页,项目打包和指定运行环境
    SpringBoot2.0 基础案例(16):配置Actuator组件,实现系统监控
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3474947.html
Copyright © 2020-2023  润新知