algo/rust/41_dynamic_programming/coin_change.rs
caitlin.gao a948635286 feat(geektime_algo): add 41 dynamic programming
min_dis_path, coin_change
2019-09-04 16:46:48 +08:00

21 lines
506 B
Rust

fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let mut dp = vec![amount+1; (amount+1) as usize];
dp[0] = 0;
for i in 1..=amount as usize {
for &coin in coins.iter() {
if i as i32 >= coin {
dp[i] = dp[i].min(dp[i-coin as usize] + 1);
}
}
}
let last = *dp.last().unwrap();
if last > amount { -1 } else { last }
}
fn main() {
let coins = vec![1, 3, 5];
let m = coin_change(coins, 9);
println!("{}", m); // 3
}