File tree Expand file tree Collapse file tree 2 files changed +68
-1
lines changed Expand file tree Collapse file tree 2 files changed +68
-1
lines changed Original file line number Diff line number Diff line change 1- add_executable (daily_challenge p2021_3_10_AddOneRowToTree .cpp)
1+ add_executable (daily_challenge p2021_3_12_CoinChange .cpp)
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments