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];
} }
Google - Find minimum number of coins that make a given value的更多相关文章
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [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 ...
- Reorder array to construct the minimum number
Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...
- Leetcode: Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 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. ...
- 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Codeforces 279D The Minimum Number of Variables 状压dp
The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...
- [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 ...
- [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 ...
随机推荐
- spring security 学习
1.默认登录 user /df1fc617-bb94-494e-8adb-0234046bf092 取消校验 在启动类上添加下面的注解 @EnableAutoConfiguration(excl ...
- ubuntu安装后问题
ubuntu安装后桌面显示不正常,出现闪屏,或者是缺图标的问题多数是ubuntu的3D加速显示问题 解决方法,vmware workstations 中的菜单栏:虚拟机->设置->显示器- ...
- MyBatis-day2
Properties 属性: 如果属性在不只一个地方进行了配置,那么 MyBatis 将按照下面的顺序来加载: 在 properties 元素体内指定的属性首先被读取. 然后根据 properties ...
- 小程序中input设置宽度后宽度还有空间,但是placeholder被遮挡问题
最近在做小程序,已经设置了宽高,placeholder没有超出input宽度,却被挡住了一部分,上代码看一下: wxml: <view class='container'> <inp ...
- 使用Babel将单独的js文件 中的 ES6转码为ES5
如果你并没有接触过ES6,当你看到下面的代码时,肯定是有点懵逼的(这是什么鬼?心中一万头神兽奔腾而过),但是你没看错,这就是ES6.不管你看不看它,它都在这里. 1 2 3 4 5 6 7 8 9 ...
- [Java concurrent][Collections]
同步容器类 同步容器类包括Vector和Hashtable,二者是早期JDK的一部分.以及一些在JDK1.2中添加的可以由Collections.synchronizedXxx等工厂方法创建的. 这些 ...
- Linux 网络编程(一)--Linux操作系统概述
一.Linux的内核版本 Linux内核的编号采用如下编号形式: 主版本号.此版本号.主补丁号.次补丁号 例如:2.6.26.3 第一个数字”2”是主版本号,表示第2大版本. 第二个数字”6”是此版本 ...
- Labview笔记-创建自定义控件
labview中的控件种类很多,但是样式或者外观有时不能满足我们的需求.如何制作一个好看酷酷的自定义控件呢? 以开关为例,我们先添加一个labview中自带的确定开关控件 之后右键该控件--高级--自 ...
- wpf 事件
事件的本质是 系统消息 再winform中 消息被封装成了事件触发 这种就是 普通事件,特点就是 1对1,以及事件必须能访问到控件 才能绑定 在 wpf中 新概念 路由事件 ,路由区别是 不在是 ...
- 工具类:mybatis中使用Threadlocal开启session及关闭session
1.线程容器,给线程绑定一个Object 内容,后只要线程不变,可以随时取出. 1.1 改变线程,无法取出内容. final ThreadLocal threadLocal = new ThreadL ...