Skip to content

Commit 0c2d078

Browse files
committed
Done classical DP problem
1 parent 22c2cbe commit 0c2d078

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_executable(daily_challenge p2021_3_10_AddOneRowToTree.cpp)
1+
add_executable(daily_challenge p2021_3_12_CoinChange.cpp)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Created by 10578 on 2021/3/12.
3+
//
4+
5+
#include "iostream"
6+
#include "vector"
7+
#include "algorithm"
8+
#include "functional"
9+
#include "sstream"
10+
#include "fstream"
11+
#include "iterator"
12+
#include "string"
13+
#include "limits"
14+
15+
#define ALTER_IN(filename) std::fstream in_f(filename); std::cin.rdbuf(in_f.rdbuf());
16+
#define BOOST_IO std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
17+
18+
#ifdef __GUNG__
19+
__attribute__((constructor))
20+
void my_startup_fn(){
21+
BOOST_IO;
22+
}
23+
#endif
24+
25+
using namespace std;
26+
27+
class Solution {
28+
public:
29+
static int coinChange(vector<int> &coins, int amount) {
30+
vector<int> dp(amount + 1, numeric_limits<int>::max());
31+
dp[0] = 0;
32+
33+
for (int i = 1; i < dp.size(); ++i) {
34+
for (auto &&item: coins) {
35+
if (item <= i && dp[i - item] != numeric_limits<int>::max()) {
36+
dp[i] = min(dp[i], dp[i - item] + 1);
37+
}
38+
}
39+
}
40+
41+
return dp.back() == numeric_limits<int>::max() ? -1 : dp.back();
42+
}
43+
};
44+
45+
int main() {
46+
ALTER_IN("in.txt");
47+
48+
int T;
49+
cin >> T;
50+
51+
Solution s;
52+
string line;
53+
int amount;
54+
while (T--) {
55+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
56+
getline(cin, line);
57+
stringstream ss(line);
58+
istream_iterator<int> start(ss), end;
59+
60+
vector<int> coins(start, end);
61+
cin >> amount;
62+
63+
cout << (Solution::coinChange(coins, amount)) << endl;
64+
}
65+
66+
return 0;
67+
}

0 commit comments

Comments
 (0)