当前位置:网站首页>Dynamic programming example 1 leetcode 322 coin change
Dynamic programming example 1 leetcode 322 coin change
2022-06-25 05:12:00 【Ability with desire】

Translation work
Give you an array , Elements represent coins of different denominations , An integer , Represents the total amount of money . Round up the total amount of money with the fewest coins , If the total amount cannot be rounded up, return -1.
You may need to give a specific set of denominations ;
Example 1:
Input :coins = [1,2,5],amount =11
Output :3
explain :11=5+5+1
Example 2:
Input :coins = [2],amount = 3
Input :-1
Example 3:
Input :coins = [1],amount = 0
Output :0
methodology :
With coins=[2,5,7],m=27 For example
1. Determine the state of
Array f[i]/f[i][j] What is the ?
Make sure the status has 2 Consciousness :1) The last step 2) Sub problem
1) The last step
Suppose the optimal strategy is K A coin consists of , namely a1,a2,a3,……ak,a1+……ak = 27
| 27-ak | ak |
| K-1 Coin | The last one |
notes :2 The key
a. I don't care how to spell the coins in front 27-ak Of ( Probably 1 Maybe 100 Kind of ), I don't even know ak and K, But I'm sure the front coin spelled 27-ak.
b. Because it's the best strategy , So spell out 27-ak The number of coins must be the optimal solution .
2) Sub problem
With the fewest coins you can spell 27-ak.
set up f[x] It means to spell the least coin x,ak The value is 2,5,7,m=27
f[27] The value of may be f[27-2]+1,f[27-5]+1,f[27-7]+1, From the question
f[27] = min{ f[27-2]+1,f[27-5]+1,f[27-7]+1}
2. Transfer equation
f[x] Means to spell x The minimum number of coins
For any x, Yes f[x] = min{ f[x-2]+1,f[x-5]+1,f[x-7]+1}
3. Initial conditions , Boundary situation
f[0] = 0,f[x] = Positive infinity means that the current coin cannot be spelled x
4. Computation order
initial f[0] = 0
Calculation f[1],f[2]……f[27]
Example
#include<iostream>
#include<malloc.h>
using namespace std;
//LintCode;Coin Change
//3 Grow coins 2 element 5 element 7 element , Buy a Book 27 element
// How to pay with the least combination of coins , There is no need for the other party to pay f(x) Said to buy one 27 Yuan book to make up the least coin
/******66656565{2,5,7} *** 27*/
int CoinChange(int A[], int m) {
int MAXN = 32001;
int *f = new int[m+1];
f[0] = 0;
int lenA = 0;
for (int i = 0; A[i] >= 0; i++) {
lenA++;
}
int i, j;
for (i = 1; i <= m; i++) { //1 2 3...m
f[i] = MAXN;
for ( j = 0; j < lenA; j++) { //2 5 7
if (i >= A[j] && A[i - A[j]] != MAXN) { // The required amount is greater than the face value and the current face value can make up the required amount
f[i] = f[i] < (f[i - A[j]] + 1) ? f[i] : (f[i - A[j]] + 1) ;
}
}
}
if (f[m] == MAXN) {
f[m] = -1;
}
return f[m];
}
int main() {
//Coinchange
int A[] = { 2,5,7 };
int m = 27;
cout<<CoinChange(A,m);
return 0;
}边栏推荐
- Create an environment for new projects
- WPF uses Maui's self drawing logic
- 基于SSH实现的学生成绩管理系统
- Native JS high risk reminder pop-up code snippet, "are you sure you want to do this?" and "it cannot be recovered after deletion. Do you want to continue“
- Startup mode of SoC verification environment
- Matlab notes
- Use serialize in egg to read and write split tables
- 渗透测试-目录遍历漏洞
- Various pits encountered in the configuration of yolov3 on win10
- OLAP analysis engine kylin4.0
猜你喜欢

CTFHUB SSRF

Kotlin compose listens to the soft keyboard and clicks enter to submit the event

The construction and usage of wampserver framework

2021-03-23

XSS (cross site script attack) summary (II)

Qdebug June 2022

How to use the Magic pig system reinstallation master

Working principle of asemi three-phase rectifier bridge

魔法猪系统重装大师怎么使用

What if win11 Bluetooth fails to connect? Solution of win11 Bluetooth unable to connect
随机推荐
ThinkPHP 5 log management
How micro engine uploads remote attachments
ASEMI大功率场效应管和三极管的区别
Notes on non replacement elements in the line (padding, margin, and border)
How PHP gets the user's City
Characteristics of ES6 arrow function
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
win11蓝牙无法连接怎么办?win11蓝牙无法连接的解决方法
Cookie & session & JSP (XII)
CTFHub-rce
Laravel Vonage SMS sending
《QDebug 2022年6月》
Integrate CDN to create the ultimate service experience for customers!
Detailed summary of position positioning
Method of opening data recovery of solid state disk
CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options
Database overview
Matlab notes
Kotlin compose listens to the soft keyboard and clicks enter to submit the event
Prototypical Networks for Few-shot Learning