Google - Find minimum number of coins that make a given value

Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
Examples:

Input: coins[] = {25, 10, 5}, V = 30
Output: Minimum 2 coins required
We can use one coin of 25 cents and one of 5 cents 

Input: coins[] = {9, 6, 5, 1}, V = 11
Output: Minimum 2 coins required
We can use one coin of 6 cents and 1 coin of 5 cents
// "static void main" must be defined in a public class.
public class Main {
    public static void main(String[] args) {
        int[] coins = {1,3,5};
        System.out.println(new Solution().minCoinChangeRecursiveWithMemo(coins, 3, 50));
        System.out.println(new Solution().minCoinChangeDP(coins, 3, 50));
    }
}

class Solution {
    public int minCoinChangeRecursiveWithMemo(int[] coins, int m, int v){
        HashMap<Integer, Integer> map = new HashMap<>();
        return helper(coins, m, v, map);
    }

    public int helper(int[] coins, int m, int v, HashMap<Integer, Integer> map){
        if(v == 0){
            return 0;
        }
        if(map.containsKey(v)){
            return map.get(v);
        }
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < m; i++){
            if(coins[i] <= v){
                int sub_res = helper(coins, m, v-coins[i], map);
                if(sub_res !=  Integer.MAX_VALUE && sub_res + 1 < min){
                    min = sub_res+1;
                }
            }
        }
        map.put(v, min);
        return min;
    }

    public int minCoinChangeDP (int[] coins, int m, int v){
        int[] dp = new int[v+1];
        dp[0] = 0;
        for(int i= 1; i <= v; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 0; j < m; j++){
                if(coins[j] <= i){
                    if(dp[i - coins[j]] + 1 < dp[i]) {
                        dp[i] = dp[i - coins[j]] + 1;
                    }
                }
            }
        }
        return dp[v];
    }

}

原文地址:https://www.cnblogs.com/incrediblechangshuo/p/10037150.html

时间: 2024-11-09 04:36:36

Google - Find minimum number of coins that make a given value的相关文章

[Coding Made Simple] Coin Changes Minimum Number of Coins

Given coins of certain denominations and a total, how many minimum coins would you need to make this total? Dynamic Programming solution State: T[i][j]: given the first i coins, the min number of coins needed to make a total of j. Function: case 1. T

452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Interval Minimum Number

Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the resu

Reorder array to construct the minimum number

Construct minimum number by reordering a given non-negative integer array. Arrange them such that they form the minimum number. Notice The result may be very large, so you need to return a string instead of an integer. Have you met this question in a

codeforce 804B Minimum number of steps

cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring,

452. Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

Leetcode: Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

[LeetCode] 452 Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

[LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s