思路:动态规划。对于属于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. QT中的线程与事件循环理解(2)

    1. Qt多线程与Qobject的关系 每一个 Qt 应用程序至少有一个事件循环,就是调用了QCoreApplication::exec()的那个事件循环.不过,QThread也可以开启事件循环.只不 ...

  2. hdu 4925 黑白格

    http://acm.hdu.edu.cn/showproblem.php?pid=4925 给定一个N*M的网格,对于每个格子可以选择种树和施肥,默认一个苹果树收获1个苹果,在一个位置施肥的话,周围 ...

  3. iOS 使用代码创建约束,实现自动布局

    ///与下面约束对象属性截图相对应//使用Auto Layout约束,禁止将Autoresizing Mask转换为约束 [self.funcView setTranslatesAutoresizin ...

  4. Python自动化开发 - Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和mysq ...

  5. 4.css基础

    1 Css概念 CSS 指层叠样式表 (Cascading Style Sheets)(级联样式表) Css是用来美化html标签的,相当于页面化妆. ◆样式表书写位置 2选择器 2.1 写法 选择器 ...

  6. Windows的cmd窗口显示utf8字符

    用XeLaTeX的时候,查字体需要用fc-list命令,XeLaTeX用的都是utf编码,所以fc-list输出的字体信息也是utf编码.因此需要把cmd窗口也改成utf8编码才能看到这些字体信息.U ...

  7. C#中类的属性的获取

    /// <summary> /// 将多个实体转换成一个DataTable /// </summary> /// <typeparam name="T" ...

  8. MVC 视图不使用模板页的两种方法

    直接对view页面的Layout值设置null @{ Layout = null;//"~/Views/Shared/_Layout.cshtml"; } 对_ViewStart. ...

  9. [学习笔记]Splay

    其实就是一道题占坑啦 [NOI2005]维护数列 分析: 每次操作都要 \(Splay\) 一下 \(Insert\) 操作:重建一棵平衡树,把 \(l\) 变成根,\(l+2\) 变成右子树的根,那 ...

  10. 36_并发编程-multiprocess模块

    仔细说来,multiprocess不是一个模块而是python中一个操作.管理进程的包. 之所以叫multi是取自multiple的多功能的意思,在这个包中几乎包含了和进程有关的所有子模块.由于提供的 ...