Skip to content

Commit 2ee3234

Browse files
committed
Solve #216
1 parent d64510b commit 2ee3234

File tree

3 files changed

+114
-32
lines changed

3 files changed

+114
-32
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,4 @@ mod n0212_word_search_ii;
183183
mod n0213_house_robber_ii;
184184
mod n0214_shortest_palindrome;
185185
mod n0215_kth_largest_element_in_an_array;
186+
mod n0216_combination_sum_iii;

src/n0040_combination_sum_ii.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
* [40] Combination Sum II
33
*
44
* Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
5-
*
5+
*
66
* Each number in candidates may only be used once in the combination.
7-
*
7+
*
88
* Note:
9-
*
10-
*
9+
*
10+
*
1111
* All numbers (including target) will be positive integers.
1212
* The solution set must not contain duplicate combinations.
13-
*
14-
*
13+
*
14+
*
1515
* Example 1:
16-
*
17-
*
16+
*
17+
*
1818
* Input: candidates = [10,1,2,7,6,1,5], target = 8,
1919
* A solution set is:
2020
* [
@@ -23,19 +23,19 @@
2323
* [2, 6],
2424
* [1, 1, 6]
2525
* ]
26-
*
27-
*
26+
*
27+
*
2828
* Example 2:
29-
*
30-
*
29+
*
30+
*
3131
* Input: candidates = [2,5,2,1,2], target = 5,
3232
* A solution set is:
3333
* [
3434
* [1,2,2],
3535
* [5]
3636
* ]
37-
*
38-
*
37+
*
38+
*
3939
*/
4040
pub struct Solution {}
4141

@@ -45,25 +45,31 @@ impl Solution {
4545
pub fn combination_sum2(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
4646
let mut seq = candidates;
4747
let mut res = Vec::new();
48-
seq.sort_unstable_by(|a, b| { b.cmp(a) });
48+
seq.sort_unstable_by(|a, b| b.cmp(a));
4949
let mut vec = Vec::new();
5050
Solution::backtrack(&seq, target, vec, &mut res, 0);
5151
res
5252
}
5353

54-
fn backtrack(seq: &Vec<i32>, target: i32, mut curr: Vec<i32>, result: &mut Vec<Vec<i32>>, start_idx: usize) {
54+
fn backtrack(
55+
seq: &Vec<i32>,
56+
target: i32,
57+
mut curr: Vec<i32>,
58+
result: &mut Vec<Vec<i32>>,
59+
start_idx: usize,
60+
) {
5561
let mut i = start_idx;
5662
while i < seq.len() {
5763
let item = seq[i];
5864
if target - item < 0 {
5965
i += 1;
60-
continue
66+
continue;
6167
}
6268
let mut new_vec = curr.clone();
6369
new_vec.push(item);
6470
if target == item {
6571
result.push(new_vec);
66-
} else {
72+
} else {
6773
Solution::backtrack(seq, target - item, new_vec, result, i + 1);
6874
}
6975
// skip duplicate result
@@ -82,19 +88,17 @@ mod tests {
8288

8389
#[test]
8490
fn test_40() {
85-
assert_eq!(Solution::combination_sum2(vec![1,1,1,1,1,1,1], 7), vec![vec![1,1,1,1,1,1,1]]);
86-
assert_eq!(Solution::combination_sum2(vec![10,1,2,7,6,1,5], 8),
87-
vec![
88-
vec![7,1],
89-
vec![6,2],
90-
vec![6,1,1],
91-
vec![5,2,1],
92-
]);
93-
assert_eq!(Solution::combination_sum2(vec![2,5,2,1,2], 5),
94-
vec![
95-
vec![5],
96-
vec![2,2,1],
97-
]);
98-
91+
assert_eq!(
92+
Solution::combination_sum2(vec![1, 1, 1, 1, 1, 1, 1], 7),
93+
vec![vec![1, 1, 1, 1, 1, 1, 1]]
94+
);
95+
assert_eq!(
96+
Solution::combination_sum2(vec![10, 1, 2, 7, 6, 1, 5], 8),
97+
vec![vec![7, 1], vec![6, 2], vec![6, 1, 1], vec![5, 2, 1],]
98+
);
99+
assert_eq!(
100+
Solution::combination_sum2(vec![2, 5, 2, 1, 2], 5),
101+
vec![vec![5], vec![2, 2, 1],]
102+
);
99103
}
100104
}

src/n0216_combination_sum_iii.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* [216] Combination Sum III
3+
*
4+
* <div>
5+
* Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
6+
*
7+
* Note:
8+
*
9+
*
10+
* All numbers will be positive integers.
11+
* The solution set must not contain duplicate combinations.
12+
*
13+
*
14+
* Example 1:
15+
*
16+
*
17+
* Input: k = 3, n = 7
18+
* Output: [[1,2,4]]
19+
*
20+
*
21+
* Example 2:
22+
*
23+
*
24+
* Input: k = 3, n = 9
25+
* Output: [[1,2,6], [1,3,5], [2,3,4]]
26+
*
27+
* </div>
28+
*/
29+
pub struct Solution {}
30+
31+
// submission codes start here
32+
33+
impl Solution {
34+
pub fn combination_sum3(k: i32, n: i32) -> Vec<Vec<i32>> {
35+
if k > 9 || k < 1 {
36+
return vec![]
37+
}
38+
let max = (0..k).fold(0, |acc, t| { acc + 9 - t });
39+
let min = (0..k).fold(0, |acc, t| { acc + t });
40+
if n < min || n > max {
41+
return vec![]
42+
}
43+
let mut res = Vec::new();
44+
let mut seed = Vec::new();
45+
Solution::helper(n, 0, k, seed, &mut res);
46+
res
47+
}
48+
49+
fn helper(distance: i32, prev: i32, remain: i32, mut curr: Vec<i32>, res: &mut Vec<Vec<i32>>) {
50+
if remain == 0 {
51+
if distance == 0 {
52+
res.push(curr);
53+
}
54+
return
55+
}
56+
for i in (prev+1..=9) {
57+
if distance - i < 0 {
58+
break;
59+
}
60+
let mut new_vec = curr.clone();
61+
new_vec.push(i);
62+
Solution::helper(distance - i, i, remain - 1, new_vec, res);
63+
}
64+
}
65+
}
66+
67+
// submission codes end
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
73+
#[test]
74+
fn test_216() {
75+
assert_eq!(Solution::combination_sum3(3, 9), vec![vec![1,2,6],vec![1,3,5], vec![2,3,4]]);
76+
}
77+
}

0 commit comments

Comments
 (0)