Skip to content

Latest commit

 

History

History
41 lines (34 loc) · 1.15 KB

README.md

File metadata and controls

41 lines (34 loc) · 1.15 KB

322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:

Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Note:

You may assume that you have an infinite number of each kind of coin.

Solutions (Rust)

1. Dynamic Programming

impl Solution {
    pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
        let mut dp = vec![-1; amount as usize + 1];
        dp[0] = 0;

        for &coin in &coins {
            for i in (coin as usize)..=(amount as usize) {
                dp[i] = match dp[i - coin as usize] {
                    x if x != -1 && (dp[i] == -1 || dp[i] > x + 1) => x + 1,
                    _ => dp[i],
                };
            }
        }

        dp[amount as usize]
    }
}