20240325 Finished

This commit is contained in:
jackfiled 2024-03-25 11:03:11 +08:00
parent caa2102e64
commit a19731a7d4
2 changed files with 41 additions and 1 deletions

View File

@ -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;
mod p322_coin_change;
mod p518_coin_change_ii;

View File

@ -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>) -> 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]));
}
}