Skip to content

Commit 5df1e6b

Browse files
committed
Added Combination Sum IV
1 parent daf81a0 commit 5df1e6b

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@
228228
- [x] [Missing Number](https://leetcode.com/problems/missing-number/) - [Solution](src/missing_number.rs)
229229
- [x] [Reverse Bits](https://leetcode.com/problems/reverse-bits/) - [Solution](src/reverse_bits.rs)
230230

231-
### Dynamic Programming
231+
### Dynamic Programming
232232

233233
- [x] [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) - [Solution](src/climbing_stairs.rs)
234234
- [x] [Coin Change](https://leetcode.com/problems/coin-change/) - [Solution](src/coin_change.rs)
235235
- [x] [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) - [Solution](src/longest_increasing_subsequence.rs)
236236
- [x] [Word Break](https://leetcode.com/problems/word-break/) - [Solution](src/word_break.rs)
237-
- [ ] [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)
237+
- [x] [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) - [Solution](src/combination_sum_iv.rs)
238238
- [x] [House Robber](https://leetcode.com/problems/house-robber/) - [Solution](src/house_robber.rs)
239239
- [x] [House Robber II](https://leetcode.com/problems/house-robber-ii/) - [Solution](src/house_robber_ii.rs)
240240
- [x] [Decode Ways](https://leetcode.com/problems/decode-ways/) - [Solution](src/decode_ways.rs)

src/combination_sum_iv.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::collections::HashMap;
2+
3+
/// Given an array of *distinct* integers `nums` and a target integer `target`, return the number
4+
/// of possible combinations that add up to `target`.
5+
///
6+
/// The test cases are generated so that the answer can fit in a 32-bit integer.
7+
struct Solution;
8+
9+
impl Solution {
10+
11+
fn worker(results: &mut HashMap<i32, i32>, nums: &Vec<i32>, target: i32) -> i32 {
12+
let mut result = 0;
13+
14+
if results.contains_key(&target) {
15+
result = results[&target];
16+
} else {
17+
for &num in nums {
18+
let current = target - num;
19+
if current == 0 {
20+
result += 1;
21+
} else if current > 0 {
22+
result += Self::worker(results, nums, current);
23+
}
24+
}
25+
results.insert(target, result);
26+
}
27+
28+
result
29+
}
30+
31+
pub fn combination_sum4(nums: Vec<i32>, target: i32) -> i32 {
32+
let mut results = HashMap::new();
33+
Self::worker(&mut results, &nums, target)
34+
}
35+
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::Solution;
41+
42+
#[test]
43+
fn example_1() {
44+
let nums = vec![1, 2, 3];
45+
let target = 4;
46+
let result = Solution::combination_sum4(nums, target);
47+
assert_eq!(result, 7);
48+
}
49+
50+
#[test]
51+
fn example_2() {
52+
let nums = vec![9];
53+
let target = 3;
54+
let result = Solution::combination_sum4(nums, target);
55+
assert_eq!(result, 0);
56+
}
57+
58+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ pub mod sum_of_two_integers; // 371 ✓
355355

356356
pub mod guess_number_higher_or_lower; // 374
357357

358+
pub mod combination_sum_iv; // 377 ✓
358359
pub mod kth_smallest_element_in_a_sorted_matrix; // 378
359360

360361
pub mod insert_delete_getrandom_o1; // 380 ✓

0 commit comments

Comments
 (0)