当前位置:网站首页>LeetCode 322. Change exchange (dynamic planning)
LeetCode 322. Change exchange (dynamic planning)
2022-07-06 01:25:00 【zjsru_ Beginner】
Subject portal :322. Change for
Topic details :
Give you an array of integers coins , Coins of different denominations ; And an integer amount , Represents the total amount .
Calculate and return the amount needed to make up the total amount The minimum number of coins . If no combination of coins can make up the total amount , return -1 .
You can think of the number of coins of each kind as infinite .
Example 1:
Input :coins = [1, 2, 5], amount = 11
Output :3
explain :11 = 5 + 5 + 1
Example 2:Input :coins = [2], amount = 3
Output :-1
Example 3:Input :coins = [1], amount = 0
Output :0Tips :
1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104
Topic analysis :
After a simple analysis, it is found that it is very similar to a complete backpack , The number of coins is infinite , But the total amount ( The capacity of the backpack ) It is the same. , We are required to use the least number of coins , So we use dynamic programming to solve this problem .
First determine dp Array , The array stores the minimum number of coins required for the total amount we give , The subscript of the array is the total amount given , So we should finally return the array with the subscript amount Value , namely dp [amount];
Next, determine the recurrence equation :
Take an example to analyze :
Input :coins = [1, 2, 5], amount = 11
Output :3
The total amount collected 11 The minimum number of coins required is dp[11 - 5] + 1( Take the denomination as 5 The coin of , Number of coins +1), and 11-5 by 6, The total amount is 6 The minimum number of coins required is dp[6 - 5] + 1( Then take the denomination as 5 The coin of , Number of coins +1), And so on until the amount is 0. We found that 6 Also can be 3 The denomination is 2 Coins of , But at this time, the number of coins taken is greater than that used The denomination is 5 The coin plus the denomination is 1 The coin of 2 gold , Therefore, we should constantly select the smallest one to ensure the total amount The least number of coins . So the recurrence equation is dp[ i ]=min(dp[ i - coins[ j ] ] + 1,dp[ i ])(i For the amount that needs to be scraped up )
dp[ 0 ]=0, Other values are initialized to the maximum value to avoid being overwritten in the comparison .
Code (c++):
class Solution {
public:
int max=999999999;
int coinChange(vector<int>& coins, int amount) {
vector<int>dp(amount+1,max);
dp[0]=0;// The total amount is 0 You don't need a coin
int length=coins.size();
for(int i=1;i<=amount;i++)// From the amount 1 Start to the requested amount amount
{
for(int j=0;j<length;j++)
{
if(coins[j]<=i) The denomination of the coin is less than the current amount
dp[i]=min(dp[i-coins[j]]+1,dp[i]);
}
}
if(dp[amount]==max)
return -1;
else
return dp[amount];
}
};
Computer 204 zjr
边栏推荐
- Recursive method to realize the insertion operation in binary search tree
- internship:项目代码所涉及陌生注解及其作用
- 282. Stone consolidation (interval DP)
- Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
- A Cooperative Approach to Particle Swarm Optimization
- 现货白银的一般操作方法
- Docker compose配置MySQL并实现远程连接
- What is the most suitable book for programmers to engage in open source?
- 网易智企逆势进场,游戏工业化有了新可能
- WordPress collection plug-in automatically collects fake original free plug-ins
猜你喜欢

有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
![[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。](/img/3c/ec97abfabecb3f0c821beb6cfe2983.jpg)
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。

【SSRF-01】服务器端请求伪造漏洞原理及利用实例

Introduction to robotics I. spatial transformation (1) posture, transformation

Five challenges of ads-npu chip architecture design

Kotlin basics 1

ORA-00030

The growth path of test / development programmers, the problem of thinking about the overall situation

DOM introduction

leetcode刷题_平方数之和
随机推荐
JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
视频直播源码,实现本地存储搜索历史记录
Vulhub vulnerability recurrence 75_ XStream
Unity VR solves the problem that the handle ray keeps flashing after touching the button of the UI
Finding the nearest common ancestor of binary search tree by recursion
MATLB|实时机会约束决策及其在电力系统中的应用
VMware Tools installation error: unable to automatically install vsock driver
2020.2.13
Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
Pbootcms plug-in automatically collects fake original free plug-ins
【详细】快速实现对象映射的几种方式
Mysql--- query the top 5 students
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
Code review concerns
MySQL learning notes 2
Building core knowledge points
Mlsys 2020 | fedprox: Federation optimization of heterogeneous networks
ClickOnce 不支持请求执行级别“requireAdministrator”
Finding the nearest common ancestor of binary tree by recursion
Four commonly used techniques for anti aliasing