-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0322_coinChange.cc
195 lines (184 loc) · 4.55 KB
/
Problem_0322_coinChange.cc
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <cstdint>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int f(vector<int>& coins, int index, int amount)
{
if (index == coins.size())
{
return amount == 0 ? 0 : INT32_MAX;
}
int ways = INT32_MAX;
for (int zhang = 0; zhang * coins[index] <= amount; zhang++)
{
int next = f(coins, index + 1, amount - zhang * coins[index]);
if (next != INT32_MAX)
{
ways = std::min(ways, next + zhang);
}
}
return ways;
}
// 暴力尝试,超时
int coinChange1(vector<int>& coins, int amount)
{
if (amount == 0)
{
return 0;
}
int ans = f(coins, 0, amount);
return ans == INT32_MAX ? -1 : ans;
}
int dfs(vector<int>& coins, int index, vector<int>& seen, int amount)
{
if (index == coins.size())
{
return amount == 0 ? 0 : INT32_MAX;
}
if (seen[amount] != INT32_MAX)
{
return seen[amount];
}
int ways = INT32_MAX;
for (int zhang = 0; zhang * coins[index] <= amount; zhang++)
{
int next = dfs(coins, index + 1, seen, amount - zhang * coins[index]);
if (next != INT32_MAX)
{
ways = std::min(ways, next + zhang);
}
}
// 注意这里的 ways 也有可能是 INT32_MAX
// 假如seen数组的初始值为 -1,
// if(seen[amount] != -1) return seen[amount]; 会把 INT32_MAX 返回,导致没有计算某个步骤
seen[amount] = ways;
return ways;
}
// 记忆化搜索,超时
int coinChange2(vector<int>& coins, int amount)
{
if (amount == 0)
{
return 0;
}
vector<int> seen(amount + 1, INT32_MAX);
int ans = dfs(coins, 0, seen, amount);
return ans == INT32_MAX ? -1 : ans;
}
// 递归改dp
int coinChange3(vector<int>& coins, int amount)
{
if (coins.size() == 0 || amount < 0)
{
return -1;
}
int n = coins.size();
vector<vector<int>> dp(n + 1, vector<int>(amount + 1));
for (int j = 1; j <= amount; j++)
{
// 数组到尽头,还有剩余
dp[n][j] = -1;
}
for (int i = n - 1; i >= 0; i--)
{
for (int rest = 0; rest <= amount; rest++)
{
dp[i][rest] = -1;
if (dp[i + 1][rest] != -1)
{
// 不用当前面值
dp[i][rest] = dp[i + 1][rest];
}
if (rest - coins[i] >= 0 && dp[i][rest - coins[i]] != -1)
{
// 用当前面值
if (dp[i][rest] == -1)
{
dp[i][rest] = dp[i][rest - coins[i]] + 1;
}
else
{
dp[i][rest] = std::min(dp[i][rest], dp[i][rest - coins[i]] + 1);
}
}
}
}
return dp[0][amount];
}
int coinChange4(vector<int>& coins, int aim)
{
if (coins.size() == 0 || aim < 0)
{
return -1;
}
int N = coins.size();
vector<vector<int>> dp(N, vector<int>(aim + 1));
// dp[i][0] = 0 0列不需要填
// dp[0][1...] = arr[0]的整数倍,有张数,倍数,其他的格子-1(表示无方案)
for (int j = 1; j <= aim; j++)
{
if (j % coins[0] != 0)
{
dp[0][j] = -1;
}
else
{
dp[0][j] = j / coins[0];
}
}
for (int i = 1; i < N; i++)
{
for (int j = 1; j <= aim; j++)
{
dp[i][j] = INT32_MAX;
if (dp[i - 1][j] != -1)
{
dp[i][j] = dp[i - 1][j];
}
if (j - coins[i] >= 0 && dp[i][j - coins[i]] != -1)
{
dp[i][j] = std::min(dp[i][j], dp[i][j - coins[i]] + 1);
}
if (dp[i][j] == INT32_MAX)
{
dp[i][j] = -1;
}
}
}
return dp[N - 1][aim];
}
};
void testCoinChange()
{
Solution s;
vector<int> n1 = {1, 2, 5};
vector<int> n2 = {2};
vector<int> n3 = {1};
vector<int> n4 = {1, 2, 5, 10};
EXPECT_EQ_INT(3, s.coinChange1(n1, 11));
EXPECT_EQ_INT(-1, s.coinChange1(n2, 3));
EXPECT_EQ_INT(0, s.coinChange1(n3, 0));
EXPECT_EQ_INT(4, s.coinChange1(n4, 18));
EXPECT_EQ_INT(3, s.coinChange2(n1, 11));
EXPECT_EQ_INT(-1, s.coinChange2(n2, 3));
EXPECT_EQ_INT(0, s.coinChange2(n3, 0));
EXPECT_EQ_INT(4, s.coinChange2(n4, 18));
EXPECT_EQ_INT(3, s.coinChange3(n1, 11));
EXPECT_EQ_INT(-1, s.coinChange3(n2, 3));
EXPECT_EQ_INT(0, s.coinChange3(n3, 0));
EXPECT_EQ_INT(4, s.coinChange3(n4, 18));
EXPECT_EQ_INT(3, s.coinChange4(n1, 11));
EXPECT_EQ_INT(-1, s.coinChange4(n2, 3));
EXPECT_EQ_INT(0, s.coinChange4(n3, 0));
EXPECT_EQ_INT(4, s.coinChange4(n4, 18));
EXPECT_SUMMARY;
}
int main()
{
testCoinChange();
return 0;
}