|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=39 lang=cpp |
| 3 | + * |
| 4 | + * [39] Combination Sum |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/combination-sum/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Medium (53.48%) |
| 10 | + * Likes: 3773 |
| 11 | + * Dislikes: 115 |
| 12 | + * Total Accepted: 535.7K |
| 13 | + * Total Submissions: 965.3K |
| 14 | + * Testcase Example: '[2,3,6,7]\n7' |
| 15 | + * |
| 16 | + * Given a set of candidate numbers (candidates) (without duplicates) and a |
| 17 | + * target number (target), find all unique combinations in candidates where the |
| 18 | + * candidate numbers sums to target. |
| 19 | + * |
| 20 | + * The same repeated number may be chosen from candidates unlimited number of |
| 21 | + * times. |
| 22 | + * |
| 23 | + * Note: |
| 24 | + * |
| 25 | + * |
| 26 | + * All numbers (including target) will be positive integers. |
| 27 | + * The solution set must not contain duplicate combinations. |
| 28 | + * |
| 29 | + * |
| 30 | + * Example 1: |
| 31 | + * |
| 32 | + * |
| 33 | + * Input: candidates = [2,3,6,7], target = 7, |
| 34 | + * A solution set is: |
| 35 | + * [ |
| 36 | + * [7], |
| 37 | + * [2,2,3] |
| 38 | + * ] |
| 39 | + * |
| 40 | + * |
| 41 | + * Example 2: |
| 42 | + * |
| 43 | + * |
| 44 | + * Input: candidates = [2,3,5], target = 8, |
| 45 | + * A solution set is: |
| 46 | + * [ |
| 47 | + * [2,2,2,2], |
| 48 | + * [2,3,3], |
| 49 | + * [3,5] |
| 50 | + * ] |
| 51 | + * |
| 52 | + * |
| 53 | + */ |
| 54 | + |
| 55 | +#include <vector> |
| 56 | + |
| 57 | +using namespace std; |
| 58 | + |
| 59 | +// @lc code=start |
| 60 | +class Solution { |
| 61 | + public: |
| 62 | + void findCombination(vector<vector<int>>& res, const int start, |
| 63 | + const int target, vector<int>& local, |
| 64 | + const vector<int>& num) { |
| 65 | + if (target == 0) { |
| 66 | + res.push_back(local); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + for (int i = start; i < num.size(); i++) { |
| 71 | + if (num[i] > target) return; |
| 72 | + |
| 73 | + local.push_back(num[i]); |
| 74 | + findCombination(res, i, target - num[i], local, num); |
| 75 | + local.pop_back(); |
| 76 | + } |
| 77 | + } |
| 78 | + vector<vector<int>> combinationSum(vector<int>& candidates, int target) { |
| 79 | + vector<vector<int>> res; |
| 80 | + vector<int> local; |
| 81 | + sort(candidates.begin(), candidates.end()); |
| 82 | + findCombination(res, 0, target, local, candidates); |
| 83 | + return res; |
| 84 | + } |
| 85 | + |
| 86 | + int combinationSum4(vector<int>& nums, int target) { |
| 87 | + const int N = nums.size(); |
| 88 | + sort(all(nums)); |
| 89 | + vector<ll> ways(target + 1, 0); |
| 90 | + ways[0] = 1; |
| 91 | + for (int i = 1; i <= target; ++i) { |
| 92 | + for (auto num : nums) { |
| 93 | + if (i < num) break; |
| 94 | + ways[i] = ways[i] + ways[i - num]; |
| 95 | + } |
| 96 | + } |
| 97 | + return ways[target]; |
| 98 | + } |
| 99 | +}; |
| 100 | +// @lc code=end |
0 commit comments