diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 34c570b..e771d8c 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -83,4 +83,5 @@ mod p1969_minimum_non_zero_product_of_the_array_elements; mod p2671_frequency_tracker; mod p2617_minimum_number_of_visited_cells_in_a_grid; mod p2549_count_distinct_numbers_on_board; -mod p322_coin_change; \ No newline at end of file +mod p322_coin_change; +mod p518_coin_change_ii; \ No newline at end of file diff --git a/src/problem/p518_coin_change_ii.rs b/src/problem/p518_coin_change_ii.rs new file mode 100644 index 0000000..4406e9f --- /dev/null +++ b/src/problem/p518_coin_change_ii.rs @@ -0,0 +1,39 @@ +/** + * [518] Coin Change II + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn change(amount: i32, coins: Vec) -> i32 { + let amount = amount as usize; + + let mut dp = vec![0; amount + 1]; + dp[0] = 1; + + for coin in &coins { + let coin = *coin as usize; + + for i in coin..=amount { + dp[i] += dp[i - coin]; + } + } + + dp[amount] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_518() { + assert_eq!(4, Solution::change(5, vec![1, 2, 5])); + assert_eq!(0, Solution::change(3, vec![2])); + assert_eq!(1, Solution::change(10, vec![10])); + } +}