思路:动态规划。对于属于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. Hdu1796 How many integers can you find 2017-06-27 15:54 25人阅读 评论(0) 收藏

    How many integers can you find Time Limit : 12000/5000ms (Java/Other)   Memory Limit : 65536/32768K ...

  2. Scala界面事件处理编程实战详解.

    今天学习了一个Scala界面事件处理编程,让我们从代码出发. import scala.swing._import scala.swing.event._ object GUI_Panel exten ...

  3. cocos游戏的例子(摘抄记录,非原创)

    3.1 搭建Cocos2d-JS v3.x 开发环境 下载所需的软件包 下载 Cocos Code IDE.目前 Cocos Code IDE 最新发布版本是 1.0.0-RC2.我们为什么 Coco ...

  4. JAVA的初始化顺序

    这里主要是介绍JAVA的类的初始化顺序,比较基础:主要是以例子演示为主: 例子一: 1 package com.cnblog.GDUTtiantian; 2 3 /** 4 * 5 * @author ...

  5. bash编程-Shell基础

    1. Shell脚本执行方式 直接运行,需要在脚本文件头部指定解释器,如#!/bin/bash ./myshell.sh 运行时指定shell解释器 bash myshell.sh 2. Shell命 ...

  6. MySQL--binlog和relay log的生成和删除

    ##================================================================================================== ...

  7. mosh——Linux下基于UDP的SSH连接工具

    一:TCP over UDP 1.安装mosh yum install epel-releaseyum install mosh 2.使用客户端连接 mosh user@ip -p 3.查看mosh的 ...

  8. C# 判断access建库、建表、文件是否存在等

    1.创建数据库 2.判断表是否存在 3.创建表 1.    #region access数据库操作 之 创建数据库         private void creatMDB(string dbNam ...

  9. Git项目下载部分文件或文件夹

    我们常常要在Github下载一些源码.示例等,但有时候项目库会比较大,而我关心的只是其中很少的一部分内容,由于众所周知的原因,我们下载git库是比较慢的,过大的项目经常会下载失败,所以只下载部分内容就 ...

  10. Webapi文件上传

    1/  multipart/form-data方式 using Abp.UI; using Abp.Web.Models; using System; using System.Collections ...