当前位置:网站首页>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;
}边栏推荐
- PHP uses JWT
- MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
- WPF uses Maui's self drawing logic
- Why does the SQL statement hit the index faster than it does not?
- A summary of the experiment of continue and break in C language
- Working principle of asemi three-phase rectifier bridge
- 小白一键重装官网下载使用方法
- Penetration information collection steps (simplified version)
- Understand JS high-order function and write a high-order function
- CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)
猜你喜欢

buuctf(pwn)

Prototypical Networks for Few-shot Learning

How to open the DWG file of the computer

Essais de pénétration - sujets d'autorisation

以太网是什么要怎么连接电脑

OLAP analysis engine kylin4.0
![[image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]](/img/a1/f7a35a04e180e89d7f2fdbf89c1160.jpg)
[image fusion] image fusion based on MATLAB directional discrete cosine transform and principal component analysis [including Matlab source code 1907]

Summary of SQL injection (I)

buuctf web

Two hours to take you into the software testing industry (with a full set of software testing learning routes)
随机推荐
WPF uses Maui's self drawing logic
Electric store stores data
API interface management setup -eolinker4.0
The SQL response is slow. What are your troubleshooting ideas?
Working principle of asemi three-phase rectifier bridge
How to choose the years of investment in financial products?
Reading and writing of nodejs excel (.Xlsx) files
IronOCR 2022.1 Crack
The print area becomes smaller after epplus copies the template
cuda编译报错
HR took the initiative to raise the salary of the test lady. How did she do it?
Array: force deduction dichotomy
2021-10-24
两小时带你进入软件测试行业风口(附全套软件测试学习路线)
渗透测试-目录遍历漏洞
How PHP gets the user's City
Deeply understand the characteristics of standard flow and off standard elements
Compatible with Internet Explorer
Penetration test - directory traversal vulnerability
Detailed summary of position positioning