20240325 Finished
This commit is contained in:
parent
caa2102e64
commit
a19731a7d4
|
@ -84,3 +84,4 @@ mod p2671_frequency_tracker;
|
||||||
mod p2617_minimum_number_of_visited_cells_in_a_grid;
|
mod p2617_minimum_number_of_visited_cells_in_a_grid;
|
||||||
mod p2549_count_distinct_numbers_on_board;
|
mod p2549_count_distinct_numbers_on_board;
|
||||||
mod p322_coin_change;
|
mod p322_coin_change;
|
||||||
|
mod p518_coin_change_ii;
|
39
src/problem/p518_coin_change_ii.rs
Normal file
39
src/problem/p518_coin_change_ii.rs
Normal 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]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user