-
Notifications
You must be signed in to change notification settings - Fork 0
/
combination_sum_iv.rs
58 lines (47 loc) · 1.44 KB
/
combination_sum_iv.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
use std::collections::HashMap;
/// Given an array of *distinct* integers `nums` and a target integer `target`, return the number
/// of possible combinations that add up to `target`.
///
/// The test cases are generated so that the answer can fit in a 32-bit integer.
struct Solution;
impl Solution {
fn worker(results: &mut HashMap<i32, i32>, nums: &Vec<i32>, target: i32) -> i32 {
let mut result = 0;
if results.contains_key(&target) {
result = results[&target];
} else {
for &num in nums {
let current = target - num;
if current == 0 {
result += 1;
} else if current > 0 {
result += Self::worker(results, nums, current);
}
}
results.insert(target, result);
}
result
}
pub fn combination_sum4(nums: Vec<i32>, target: i32) -> i32 {
let mut results = HashMap::new();
Self::worker(&mut results, &nums, target)
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![1, 2, 3];
let target = 4;
let result = Solution::combination_sum4(nums, target);
assert_eq!(result, 7);
}
#[test]
fn example_2() {
let nums = vec![9];
let target = 3;
let result = Solution::combination_sum4(nums, target);
assert_eq!(result, 0);
}
}