Coin Change change convertible issue

Given the different denominations of coins coins and a total amount of amount. We can write a function to calculate the minimum number of coins needed to make up the total amount. If there is no combination can be composed of any of a total amount of 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

Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/coin-change
problem-solving ideas:
This title is a dynamic programming algorithm class.
Dynamic programming problem solving ideas:
a determined state:
  1, in the active state dynamic programming belongs Stand.
  2, dynamic programming solution when you need to open an array. Such as an array f [i] or f [i] [J] mean anything.
  3, to determine the need for two states of consciousness: - the last step - child issues.
  For this problem, for example, amount to 27, coins [2,5,7].
  Although we do not know what the optimal policy, but the best strategy is certainly K coins a1, a2, ... ak add up to 27. So there must be the last coin ak, so get rid of the coin ak, ak-1 remaining coins add up to 27-ak. Because it is the best policy, so the 27-ak to spell out the minimum number of coins is or it is not the optimal strategy . So sub-question is : at least spell out how many coins with 27-ak . The original question is: How many gold coins with a minimum of 27 spell . (We will issue as smaller scale called sub-problems) we set the state of f (x) = X spell out the minimum number of coins used .
  The last coin could be any one of 2,5,7. Finally, if the coin is 2, then f [x] = f (27-2 ) +1; if last coin is 5, f [x] = f (27-5 ) +1; if the last one 7 is a coin, the f [x] = f (27-7 ) +1.
Second, the transfer equation:
  Therefore, the final transfer equation: F [X] = {F Math.min (27-2) +. 1, F (27-5) +. 1, F (27-7)} + 1'd .
Third, the initial and boundary conditions:
  if f (27-2), f (27-5 ), f (27-7) is less than zero how to do? We defined as positive infinity. Initial conditions f [0] = 0;
Fourth, the calculation order:
for this problem we are in ascending order, we first calculate the f [0], f [1 ] ... f [27]
Code:

class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] f = new int[amount+1];
        int n = coins.length;
        f[0] = 0;
        int i,j;
        for(i=1;i<=amount;i++){
            f[i] = Integer.MAX_VALUE;
                for(j=0;j<n;j++){
                    if(i>=coins[j] && f[i - coins[j]] !=Integer.MAX_VALUE){
                        f[i] = Math.min(f[i-coins[j]]+1,f[i]);
                    }
                }
        }
        if(f[amount] == Integer.MAX_VALUE){
            f[amount]=-1;
        }
        return f[amount];
    }
}

Resolve : First, open an array used to store the number of coins different from 1-amount of money needed. Followed by n array storing the length of the coin denomination. Then you need to be initialized to zero dollars zero coins. Then to achieve the transfer equation in a for loop, first the required number of coins assignment is infinite, attention need to compare prices is greater than the price given coin denomination and the required number of coins is positive infinity. *

Published 54 original articles · won praise 7 · views 3244

Guess you like

Origin blog.csdn.net/weixin_43690348/article/details/98794356