Leetcode 763. Partition Labels
思路:动态规划。对于属于coins的coin,只要知道amount-coin至少需要多少个货币就能表示,那么amount需要的货币数目=amount-coin需要的货币数目+1;如果amount-coin都不能被表示,amount也不能被表示。
方法一:递归,由上至下。
class Solution {
Map<Integer, Integer> hashMap = new HashMap<>();
public int coinChange(int[] coins, int amount) {
hashMap.put(0, 0);//0元不需要被货币表示
if(hashMap.containsKey(amount)) return hashMap.get(amount);
int curMin = amount + 1;//货币的最小面值只能是1,所以最多能被表示成amount个货币,那么amount+1就相当于正无穷
for(int coin : coins) {
if(amount >= coin) {
int cur = coinChange(coins, amount - coin) + 1;
if(cur > 0 && curMin > cur) curMin = cur;//cur==0就意味着amount-coin不能被当前货币表示
}
}
if(curMin == amount + 1) {//已经遍历过的元素的表示数目要及时记录
hashMap.put(amount, -1);
}else {
hashMap.put(amount, curMin);
}
return hashMap.get(amount);
}
}
Next challenges: Longest Increasing Subsequence Arithmetic Slices II - Subsequence Super Washing Machines
方法二:递推,由下至上。
class Solution {
Map<Integer, Integer> hashMap = new HashMap<>();
public int coinChange(int[] coins, int amount) {
hashMap.put(0, 0);//0元不需要被货币表示
int MAX = amount + 1;
for(int i = 1; i <= amount; i++) {
hashMap.put(i, MAX);//面值为i能被表示成货币的数目的最小值
for(int coin : coins) {
if(i >= coin) {
hashMap.put(i, Math.min(hashMap.get(i), hashMap.get(i - coin) + 1));
}
}
}
//对于面值i,只要i映射的值大于i就相当于i不能被当前的货币表示
return hashMap.get(amount) > amount ? -1 : hashMap.get(amount);
}
}
Leetcode 763. Partition Labels的更多相关文章
- [LeetCode] 763. Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- LC 763. Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- 【LeetCode】763. Partition Labels 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
- 763. Partition Labels 相同字母出现在同一块中,且块数最多
[抄题]: A string S of lowercase letters is given. We want to partition this string into as many parts ...
- 763. Partition Labels
我一开始看数据范围很小,没怎么想就直接暴力了. 暴力的思路是: 对于每一段S的前缀,这个前缀中的每一个字母都不应该在前缀的补集中出现,所以直接循环i:0 to S.length然后对于每一次循环,再循 ...
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- [LeetCode] Partition Labels 分割标签
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- LeetCode - Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- [Swift]LeetCode763. 划分字母区间 | Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
随机推荐
- PAT甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 关于建立MySQL数据库,中文出现乱码问题
MySQL的ini文件中的默认编码设置utf-8不用改 loose-default-character-set = utf-8 注意:需要改动部分 1.如图所示 建立schema时改变字符编码 改变为 ...
- poj 3463/hdu 1688 求次短路和最短路个数
http://poj.org/problem?id=3463 http://acm.hdu.edu.cn/showproblem.php?pid=1688 求出最短路的条数比最短路大1的次短路的条数和 ...
- CentOS中自动加载802.1q模块
要想在CentOS中自动加载内核模块,需要在/etc/sysconfig/modules/目录中增加一个脚本,在此脚本中加载所需的模块. 下面是我所用的一个名为8021q.modules的脚本,用来在 ...
- 初次见识结构体与map的区别
题目 http://vjudge.net/contest/view.action?cid=51142#problem/G 自己做的结构体 #include <iostream>#incl ...
- 分形之希尔伯特-皮亚诺(Hilbert-Peano)曲线
1890年,意大利数学家皮亚诺(Peano G)发明能填满一个正方形的曲线,叫做皮亚诺曲线.后来,由希尔伯特作出了这条曲线,又名希尔伯特曲线.Hilbert-Peano曲线是一种分形图形,它可以画得无 ...
- IIS日志存入数据库之二:ETW
在上一篇文章<IIS日志存入数据库之一:ODBC>中,我提到了ODBC方式保存的缺点,即:无法保存响应时间以及接收和响应的字节数. 如果一定要获取响应时间以及接收和响应的字节数的话,就要另 ...
- sql server 查看列备注、类型、字段大小
select 列名 = a.name ,类型 = c.name ,长度 = columnproperty(a.id,a.name,'precision') ,备注 = g.value from sys ...
- 背水一战 Windows 10 (45) - 控件(图标类): IconElement, SymbolIcon, FontIcon, PathIcon, BitmapIcon
[源码下载] 背水一战 Windows 10 (45) - 控件(图标类): IconElement, SymbolIcon, FontIcon, PathIcon, BitmapIcon 作者:we ...
- Git错误日志--! [rejected]
当出现下面错误时 ,是因为和仓库两端版本不一致时,常见于刚创建仓库时,中只有readme文件的情况.执行下面的运行完毕之后.再push到仓库即可 ! [rejected] master -> m ...