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];
} }

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

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

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  2. [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 ...

  3. Reorder array to construct the minimum number

    Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...

  4. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  5. Lintcode: 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. ...

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

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  7. Codeforces 279D The Minimum Number of Variables 状压dp

    The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...

  8. [Swift]LeetCode452. 用最少数量的箭引爆气球 | Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips

    In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...

随机推荐

  1. C语言:递归函数n!

    #include <stdio.h> long recursion(int n); void main(){ int n; long result; printf("input ...

  2. ubuntu google chrome 全屏显示命令

    全屏模式:kiosk默认全屏打开一个网页呢,只需要在快捷方式中加上 --kiosk [url] 就可以了.终端命令行打开: google-chrome --kiosk www.baidu.com 参考 ...

  3. date clock

    设置Linux系统时间:date -s "2017-06-22 15:44:30" 自定义时间显示格式:date "+%Y-%m-%d  %H:%M:%S" 查 ...

  4. sprigmvc 传值jsp页面

    https://blog.csdn.net/qq_41357573/article/details/84675535#如何将controller层值传递到jsp页面

  5. Python 天气查询到实现语音播放

    import requests #引用requests模块import pygame # 获取天气def inquery(self): url = "https://free-api.hew ...

  6. Vue语法学习第四课(2)——class与style的绑定

    之前学到的指令 v-bind 在用于绑定class和style时,表达式结果可以是字符串.数组.对象. 一.绑定HTMLClass ① 对象语法 <div class="static& ...

  7. selenium中的alter弹框

    from selenium import webdriverimport timedriver=webdriver.Chrome()driver.get('http://ui.imdsx.cn/uit ...

  8. [Kafka] [All about it]

    Overview 设计目标: 以O(1) 常数级时间复杂度的访问性能,提供消息持久化能力. 高吞吐率. 支持 kafka server 间的消息分区,及分布式消费,同时保证每个partition内部的 ...

  9. python网络之web框架

    逐步引入: 1. 最简单的web server #!/usr/bin/env python # coding:utf-8 import socket sk = socket.socket() sk.b ...

  10. mysql 的存储过程 循环 变更某个表里的字段

    /*Navicat MySQL Data Transfer Source Server : localhost_3306Source Server Version : 50505Source Host ...