-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcoinChange.h
72 lines (60 loc) · 1.81 KB
/
coinChange.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// coinChange.h
// dp
//
// Created by junl on 2019/10/4.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef coinChange_hpp
#define coinChange_hpp
#include <stdio.h>
/*
硬币找零问题。比如我们现在有硬币1元,3元,5元,我们需要支付w元,那么最少需要多少个硬币.
思路:
1. 贪心法,每次首先尝试最大面值的币种
*/
class coinChange {
public:
//贪心法,首先尝试最大面值的币种
void solve(const int coins[], int n, int money, int currentCoinCount){
if (money == 0 || currentCoinCount == n) {
result = std::min(result, currentCoinCount);
return;
}
for (int i=n-1; i>=0; i--) {
if (money >= coins[i]) {
solve(coins, n, money - coins[i], currentCoinCount+1);
}
}
}
int solve(const int coins[], int n, int money){
solve(coins, n, money, 0);
return result;
}
private:
int result = INT_MAX;
};
int min(int x, int y){ return x < y ? x : y;}
int coinChange(int* coins, int coinsSize, int amount){
if (coins == NULL || coinsSize <= 0) return 0;
int st[amount+1];
memset(st, 0, sizeof(st));
for(int i = 1; i <= amount; i++){
int m_coin = INT_MAX;
for(int j=0; j < coinsSize; j++){
if (i >= coins[j] && st[i-coins[j]]!= -1){
m_coin = min(m_coin, st[i-coins[j]] + 1);
}
}
st[i] = m_coin == INT_MAX ? -1 : m_coin;
}
return st[amount];
}
void test_coinChange(){
std::cout << "------------硬币找零问题----------" << std::endl;
class coinChange so;
int coins[] = {1,3,5};
std::cout << so.solve(coins, 3, 9) << std::endl;
std::cout << coinChange(coins, 3, 9) << std::endl;
}
#endif /* coinChange_hpp */