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. MarkdownPan2 简单使用指南

    markdown 简单使用指南 一级标题 二级标题 三级标题加代码 四级标题 这里是加粗 这里是正文and English 888 这里有正文嵌入代码这种样式 这里是代码块 这种使用的代码块 还有引用 ...

  2. Leetcode 600 不含连续1的非负整数

    给定一个正整数 n,找出小于或等于 n 的非负整数中,其二进制表示不包含 连续的1 的个数. 例如: 输入: 5 输出: 5 解释: 下面是带有相应二进制表示的非负整数<= 5: 0 : 0 1 ...

  3. java.lang.NoSuchMethodError: org.springframework.util.StreamUtils.emptyInput()Ljava/io/InputStream;

    今天写用spring的MockMvc测试controller的demo时出现了这个错误,条件反射的进行了百度,没有搜到匹配的答案,但给了一些解决问题的思路:首先NoSuchMethodError要不就 ...

  4. 给大家介绍一个实用的RN神器DeviceEventEmitter

    再不出来更新一下自己都感觉不到自己还存在了,这个监听最常用的地方莫过于单选和全选了,,当然远不止这个了,大家可以自己去多尝试几波,举个栗子 A组件全选所在 //全选 choose(bool){ //选 ...

  5. 最近学习的 Node.js 数组_函数

    数组的排序,用到了箭头函数 let arr=[, , , , , , ]; /* arr.sort(function (n1, n2){ return n1-n2; }); */ // 等价于上面的写 ...

  6. Log4net 配置文件组成

    Example: <?xml version="1.0" encoding="utf-8" ?><configuration><l ...

  7. 开始PYTHON之路

    曾经的功献给了球场酒精 曾经的激情也献给了爱情 曾经的智商用来副本求生 曾经的VB6老迈的只剩点0 曾经的SQL2000都不兼容 曾经........ 还有一些理想没有实现 还得继续在这个世界谋生 岁 ...

  8. Python_随机序列生成_白噪声

    本文介绍如何利用Python自行生成随机序列,实现了 Whichmann / Hill 生成器. 参考: [1]Random Number Generation and Monte Carlo Met ...

  9. MAC IDEA mybatis 逆向工程 无结果文件

    mac下路径是./src windows 路径是.\src 解决方法,将\纠正过来即可

  10. axios学习 - 数据获取及渲染

    在app.vue和main.js中引入axios(import axios from 'axios') 基本代码: axios.get('/axios.json') .then(function(re ...