Dynamic Planning Series "leetcode322. Change Exchange"

322. Change Exchange

Given coins of different denominations coinsand a total amount amount. Write a function to calculate the minimum number of coins required to make up the total amount. If no coin combination can make up the total amount, return -1.

Change convertibility in line with optimal substructure, for example, you want to find amount = 11the minimum number of coins when (the original question), if you know Couchu amount = 10minimum number of coins (sub-problems), the answer is you just need to add a sub-problem (re-election A coin with a face value of 1) is the answer to the original question. Because there is no limit to the number of coins, there is no mutual restriction between the sub-questions, they are independent of each other.

How to list the correct state transition equation ?

First of all, we must clarify our purpose: use the least coins to form the target amount

  1. Determine base case

    This is very simple, apparently the target amount amountfor the algorithm returns 0:00 because no coin has been Couchu the target amount.

  2. Determine the "state", which is the variable that will change in the original problem and sub-problems .

    Since the number of coins is unlimited, the denomination of the coin is also given by the subject, only the target amount will continue to approach the base case, so the only "state" is the target amount amount.

  3. Determine "choice", that is, the behavior that causes the "state" to change .

    Why does the target amount change? Because you are choosing coins, each coin you choose is equivalent to reducing the target amount. So the face value of all coins is your "choice".

  4. Clarify the definition of the function/array .dp

    Here directly in one step, using a bottom-up approach, the array dp[amount]represents the target when the amount of amountthe minimum number of coins when needed (ie, the final solution).

    The state transition equation is:

    d p ( n ) = { 0 , n = 0 − 1 , n < 0 m i n { d p ( n − c o i n ) + 1 ∣ c o i n ∈ c o i n s } , n > 0 dp(n)= \begin{cases}0, \quad n = 0 \\-1, \quad n<0 \\min\{ dp(n-coin) + 1 \quad | \quad coin \in coins \},\quad n>0 \end{cases} dp(n)=0,n=01,n<0min{ dp(ncoin)+1coincoins},n>0

dp(n)Definition: enter a target amount n, the target amount Couchu return nthe minimum number of coins.

PS: Why dpthe array is initialized to amount + 1it, make up because amountthe number of coins may be equal to a maximum amount amount(per 1 yuan with a coin face value), the initialization is amount + 1equivalent initialized to positive infinity, and to facilitate the subsequent minimum value.

import java.util.*;

class Solution {
    
    
    public int coinChange(int[] coins, int amount) {
    
    
        int[] dp = new int[amount+1];
        Arrays.fill(dp, amount+1);
        dp[0] = 0;
        for(int i = 0; i <= amount; i++){
    
    
            for(int coin:coins){
    
    
                if(i-coin >= 0){
    
    
                    dp[i] = Math.min(dp[i-coin]+1, dp[i]);
                }
            }
        }
        return (dp[amount] == amount+1) ? -1 : dp[amount];
    }
}

Time Complexity: O(kN), kthe number of kinds of coin denominations

Space complexity:O(N)

Guess you like

Origin blog.csdn.net/weixin_44471490/article/details/109014424