-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path39.cpp
More file actions
38 lines (31 loc) · 1.01 KB
/
Copy path39.cpp
File metadata and controls
38 lines (31 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Author: btjanaka (Bryon Tjanaka)
// Problem: (LeetCode) 39
// Title: Combination Sum
// Link: https://leetcode.com/problems/combination-sum/description/
// Idea: Top-down dynamic programming (top-down necessary due to having to
// return the list of solutions.
// Difficulty: Medium
// Tags: dp
void f(int target, int idx, vector<int>& cur, vector<int>& candidates, vector<vector<int>>& results) {
if(target < 0) return;
if(target == 0) {
results.push_back(cur);
return;
}
if(idx >= candidates.size()) return;
// Use the current value.
cur.push_back(candidates[idx]);
f(target - candidates[idx], idx, cur, candidates, results);
cur.pop_back();
// Skip the current value.
f(target, idx + 1, cur, candidates, results);
}
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> results;
vector<int> cur;
f(target, 0, cur, candidates, results);
return results;
}
};