LeetCode: 322. Coin Change

LeetCode刷题:322. Coin Change LeetCode刷题:322. Coin Change 原题链接:https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest ... 阅读详情

LeetCode: 322. Coin Change

题目描述

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:

Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Note:
You may assume that you have an infinite number of each kind of coin.

解题思路 —— 动态规划

  1. 记:dp[i] 为 钱数为 i 需要的最少硬币数 (-1 表示不可达)
  2. 状态转移方程:dp[i] = min{dp[i-coins[j]]+1},其中 j = 0...len(coins)

AC 代码

func min(lhv, rhv int) int {
    switch {
        case lhv == -1: return rhv
        case rhv == -1: return lhv
        case lhv < rhv: return lhv
        default: return rhv
    }
}

func coinChange(coins []int, amount int) int {
    //dp[i]: amount 的钱需要的最少硬币数 (-1 表示不可达)
    dp := []int {0}
    
    for i := 1; i <= amount; i++ {
        dp = append(dp, -1)
        
        for j := 0; j < len(coins); j++ {
            if i >= coins[j] && dp[i-coins[j]] != -1 {
                dp[i] = min(dp[i], dp[i-coins[j]]+1)
            }
        }
    }
    
    return dp[amount]
}
NXP MCU CAN波特率(位时间)配置详解 1. 概述 本文将会详细讲解如何设置NXP MCU的CAN波特率、位时间、采样点等属性。 波特率即CAN总线传输频率,位时间是波特率的倒数,例如波特率是500K,那么位时间 = 1 / 500000 = 0.000002s = 2000ns。 我们先来看一下S32K144单片机的EB中与CAN总线波特率相关的可配置参数有哪些: ... 阅读详情

相关推荐

OpenFOAM算例文件结构深度解析:以cavity案例为例教你读懂每个文件夹的作用

本文以OpenFOAM经典cavity案例为例,深度解析CFD算例文件结构。详细阐述了0、constant、system三个核心目录的作用:0存放初始与边界条件,constant定义物理属性和网格,system控制求解策略。通过剖析关键文件,帮助用户理解每个参数的意义,掌握从读懂到定制算例的能力,为高效使用这一开源CFD工具奠定坚实基础。

beer8的博客 255

【算法】322.Coin Change

对给定的硬币进行遍历,并对每个硬币可取的数量进行遍历,找到最优解。

weixin_47524121的博客 299

Prompt Learning 简介

• Prompt Learning 可以将所有的任务归一化预训练语言模型的任务• 避免了预训练和fine-tuning 之间的gap,几乎所有 NLP 任务都可以直接使用,不需要训练数据。• 在少样本的数据集上,能取得超过fine-tuning的效果。• 使得所有的任务在方法上变得一致。

jxsdq的博客 1万+

LeetCode:322. Coin Change - Python

322. 零钱兑换 问题描述: 给定不同面额的硬币coins和一个总金额amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回-1。 示例 1: 输入: coins = [1, 2, 5], amount = 11 输出: 3 解释: 11 = 5 + 5 + 1 示例 2: 输入: coins = [2], amount = 3 输出...

Growth Diary 857

Anaconda详细安装及使用教程(带图文)

Anacond的介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。因为包含了大量的科学包,Anaconda的下载文件比较大(约531 MB),如果只需要某些包,或者需要节省带宽或存储空间,也可以使用Miniconda这个较小的发行版(仅包含conda和Python)。 Conda是一个开源的包、环境管理器,可以用于...

代码帮 186万+

(04730)半导体器件之基本放大器工作原理(一)

放大器是构成各种电子线路的基本单元电路。所谓“放大”,就是将输入的微弱信号(微弱变化的电压或电流等)去控制直流电源提供的能量,并使输出按照输入的小信号变化规律而变化的过程。所以放大器的本质是能量转换器。本节重点学习基本放大电路的工作原理和基本分析方法及主要性能指标。

黄先生的博客 3023

LeetCode322.Coin-Change<零钱兑换>

题目:思路:一开始使用的是先用大的硬币,然后用小的硬币. 类似贪心. 但是有个问题就是他可能无法整除.但是如果用别的硬币那么就可以整除. 好吧.不会只能看答案了.讲得很详细.使用的是类似递归的思想.如果我要获得11块钱 然后硬币 有 1 ,2 ,5. 那么我需要的是dp [11 - 5] + 1的硬币数. 然后 dp [6 - 5] + 1 的硬币数, 最后是 dp[1 - 1] + 1 的硬币数.逐层获得硬币然后最后的结果如果有的那么返回.如果没有那就返回 -1 .

Eminste的博客 261

全波形反演实战指南:从原理到避坑的完整流程

地震波传播与波动方程是理解地下介质参数反演的基础。全波形反演通过最小化模拟与观测波形的差异,利用伴随状态法高效计算梯度,实现高分辨率速度模型构建。该技术能突破传统走时层析的分辨率极限,在石油天然气勘探、工程地质调查和深部地壳结构研究中具有重要价值。实际应用中需重点保护低频信息、建立可靠初始模型并采用多尺度频率策略,同时警惕周期跳跃和反演发散等常见问题。

weixin_30702441的博客 160

[leetcode] 322. Coin Change 零钱兑换-动态规划

Description You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amou...

Learning from the mistakes 291

9.9元ESP32-C3开发板实战:手把手教你用VSCode搭建RT-Thread最小系统(附避坑指南)

本文提供了一份详尽的实战指南,教你如何在售价仅9.9元的ESP32-C3开发板上,使用VSCode搭建RT-Thread最小系统。内容涵盖从ESP-IDF环境配置、BSP源码获取、内核补丁应用到编译下载与调试的全流程,并重点分享了版本选择、补丁应用等关键避坑技巧,旨在帮助开发者低成本、高效地启动嵌入式实时操作系统开发。

o4p5q6r7s的博客 238

Open3D 合并两个点云【2025最新版】

两个点云中的数据连接,博客长期更新,本文最新更新时间为:2025年4月26日。

点云侠的博客 7441

leetcode题集: 322. Coin Change

实在是太懒了,写博客真的懒得写。今天就一起写几篇动态规划的好了 第9题:322.Coin Change 题目大意:就是换零钱!给出可换的零钱种类,求最后换得零钱的最小张数(coin的个数),每种可换的零钱数目无限。 题目分析:思路很明确,就是找零钱从最大的开始找呗,依次找当前可换的最大数额的零钱,显然可以用动态规划的思想解决,是一个很简单的动态规划问题,状态转移方程: ...

wx_14678的博客 245

【K8S系列】Kubernetes 中 Pod 无法通过 Service 名称访问服务的 DNS 解析失败问题【已解决】

在 Kubernetes 中,Service 提供了一种稳定的方式,通过名称访问一组 Pod。当其他 Pod 无法通过 Service 名称访问服务,并且出现 DNS 解析失败时,通常会导致应用无法正常工作。本文将详细分析此问题的常见原因及其解决方案。

颜淡慕潇 1万+

LeetCode322:Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...

卷卷萌的博客 393

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

每周LeetCode算法题(十一) 题目: 322. Coin Change

每周LeetCode算法题(十一)题目: 322. Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make

JacKnights的博客 410

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode : 322. Coin Change 凑齐硬币

试题 You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of mon...

心之所向 359

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode 322. Coin Change:从错误思路到正确一维 DP

leetcode​给你一个整数数组 coins,表示不同面额的硬币。给你一个整数 amount,表示总金额。返回凑成总金额所需的 最少 硬币数;如果无法凑成,返回 -1。每种硬币的数量可以视为 无限。定义 dp[i]:凑出金额 i 所需的最少硬币个数。在这个定义下,有一个特别关键的点:凑出金额 0 不需要任何硬币,所以dp[0] = 0是唯一合理的取值。这时 dp[coin] 会被更新为 2,意味着凑一个 coin 面额需要两枚硬币,这显然不符合题意。

六六哥的博客 913

Leetcode 322: Coin Change

You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...

weixin_30641465的博客 85

leetcode 322: Coin Change

You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...

weixin_30421525的博客 74

Leetcode: 322. Coin Change(Week8, Medium)

注:本题是需要装满的完全背包问题 Leetcode 322 You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up tha

linwh8的博客 455

Leetcode 322: Coin Change - 动态规划

Description: You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that am...

想做程序媛的小太阳 319

LeetCode:322. Coin Change(硬币兑换问题)

You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...

zy345293721的博客 1201

[LeetCode] 322. Coin Change_Medium tag: backpack

You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money ...

weixin_30526593的博客 60

DP问题:leetcode(322) Coin Change

问题描述: You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amoun

u014514939的博客 211

LeetCode 322: Coin Change (硬币找零, DP经典题)

硬币找零的DP经典题。

roufoo的博客 428

Leetcode summary: dynamic programing, [322] coin change & [474] ones and zeros

[322] coin change class Solution(object): def coinChange(self, coins, amount): """ :type coins: List[int] :type amount: int :rtype: int """ #init as...

只为分享 177
上一篇: LeetCode: 321. Create Maximum Number
下一篇: LeetCode:324. Wiggle Sort II
IT技术小密圈
博客等级 码龄12年 1385粉丝 322原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值