思路:动态规划。对于属于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. bootstrap 图片切换

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  2. scrapy分布式

    开始之前我们得知道scrapy-redis的一些配置:PS 这些配置是写在Scrapy项目的settings.py中的! #启用Redis调度存储请求队列SCHEDULER = "scrap ...

  3. ASP.NET MVC IActionFilter IResultFilter IExceptionFilter/HandleError

    一.IActionFilter 1.基本定义 在action的执行前后进行AOP拦截. IActionFilter接口定义如下: public interface IActionFilter { // ...

  4. 动态设置和访问cxgrid列的Properties

    动态设置和访问cxgrid列的Properties   设置: cxGrid1DBTableView1Column.PropertiesClass   =   TcxTextEditPropertie ...

  5. Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明2

    1. 安装build-essentials 安装开发所需要的一些基本包 sudo apt-get install build-essential 2. 安装NVIDIA驱动 (3.4.0) 2.1 准 ...

  6. 【Vue】浅谈Vue不同场景下组件间的数据交流

    浅谈Vue不同场景下组件间的数据“交流”   Vue的官方文档可以说是很详细了.在我看来,它和react等其他框架文档一样,讲述的方式的更多的是“方法论”,而不是“场景论”,这也就导致了:我们在阅读完 ...

  7. 【BZOJ3545】 [ONTAK2010]Peaks

    BZOJ3545 [ONTAK2010]Peaks Solution 既然会加强版,直接把强制在线的操作去掉就好了. 代码实现 #include<stdio.h> #include< ...

  8. cad.net的undo返回操作

    这是提供给许多从lisp转移到c#的开发人员的一个函数,这个函数利用后绑代码实现undo返回操作. 本代码由edata提供: edata博客 /// <summary> /// 命令动作编 ...

  9. 初探日志框架Logback

    一. 背景 最近因为学习项目时需要使用logback日志框架来打印日志, 使用过程中碰到很多的疑惑, 而且需要在控制台打印mybatis执行的sql语句, 于是决定沉下心来 研究一下logback的使 ...

  10. 深入理解String类

    1.String str = "eee" 和String str = new String("eee")的区别 先看一小段代码, public static v ...