思路:动态规划。对于属于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的更多相关文章

  1. [LeetCode] 763. Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  2. LC 763. Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  3. 【LeetCode】763. Partition Labels 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

  4. 763. Partition Labels 相同字母出现在同一块中,且块数最多

    [抄题]: A string S of lowercase letters is given. We want to partition this string into as many parts ...

  5. 763. Partition Labels

    我一开始看数据范围很小,没怎么想就直接暴力了. 暴力的思路是: 对于每一段S的前缀,这个前缀中的每一个字母都不应该在前缀的补集中出现,所以直接循环i:0 to S.length然后对于每一次循环,再循 ...

  6. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  7. [LeetCode] Partition Labels 分割标签

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  8. LeetCode - Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  9. [Swift]LeetCode763. 划分字母区间 | Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

随机推荐

  1. 正确的类引用却显示* cannot be resolved

    eclipse 出现的问题:在一个类中引入自己编写的类竟然说“cannot be resolved”,这非常明显不正常的! 解决办法:很简单,project->clean.我的问题就解决了. 至 ...

  2. pycharm的注册码,所有版本

    77751S0VBA-eyJsaWNlbnNlSWQiOiI3Nzc1MVMwVkJBIiwibGljZW5zZWVOYW1lIjoi5b285bK4IHNvZnR3YXJlMiIsImFzc2lnb ...

  3. MapReduce_架构

    架构 MapReduce1.x JobTracker:JT(作业管理者) 将作业分解成一堆的任务:Task(MapTask和ReduceTask) 将任务分派给TaskTracker运行 作业的监控. ...

  4. Javascript 对象继承 原型链继承 对象冒充 call 混合方式

    一.原型链继承 function ClassA() {} ClassA.prototype.color = "blue"; ClassA.prototype.sayColor = ...

  5. html不规则表格设计

    <table border="1px" style="border-collapse:collapse;"> <tbody> <t ...

  6. JS通过ClassName禁止DIV点击

    //通过ClassName获取div,使用setAttribute设置div禁止点击 var itemRoom= document.getElementsByClassName("page- ...

  7. C# .NET 根据Url链接保存Image图片到本地磁盘

    根据一个Image的Url链接可以在浏览器中显示一个图片,如果要通过代码将图片保存在本地磁盘可以通过以下方式: 1.首先获取图片的二进制数组. static public byte[] GetByte ...

  8. 剑指offer编程题Java实现——面试题4替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. package Solution; ...

  9. Redis .Net

    一.Redis简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括 ...

  10. JVM调优推荐

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.JVM的调优主要是内存的调优,主要调两个方面: 各个代的大小 垃圾收集器选择 2.各个代的大小 常用的 ...