-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum.rs
61 lines (58 loc) · 1.69 KB
/
combination_sum.rs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
///
/// Problem: Combination Sum
///
/// Given an array of **distinct** integers `candidates` and a `target` integer, find **all unique combinations** of `candidates` where the chosen numbers sum to `target`.
///
/// You may use **the same number multiple times** in a combination.
///
/// **Example 1:**
/// ```plaintext
/// Input: candidates = [2,3,6,7], target = 7
/// Output: [[2,2,3], [7]]
/// Explanation: 2+2+3=7 and 7=7.
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: candidates = [2,3,5], target = 8
/// Output: [[2,2,2,2], [2,3,3], [3,5]]
/// ```
///
/// **Constraints:**
/// - `1 <= candidates.length <= 30`
/// - `1 <= candidates[i] <= 200`
/// - `All numbers are unique`
/// - `1 <= target <= 500`
///
/// # Solution:
///
/// - **Time Complexity:** `O(2^n)`
/// - **Space Complexity:** `O(n)`
impl Solution {
pub fn combination_sum(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut result = Vec::new();
let mut current_combination = Vec::new();
Self::backtrack(&candidates, target, 0, &mut current_combination, &mut result);
result
}
fn backtrack(
candidates: &Vec<i32>,
target: i32,
start: usize,
current_combination: &mut Vec<i32>,
result: &mut Vec<Vec<i32>>,
) {
if target == 0 {
result.push(current_combination.clone());
return;
}
for i in start..candidates.len() {
if target < candidates[i] {
continue;
}
current_combination.push(candidates[i]);
Self::backtrack(candidates, target - candidates[i], i, current_combination, result);
current_combination.pop();
}
}
}