File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int dp[12 + 1 ][10000 + 1 ];
4+
5+ int findLowestCoins (vector<int > &coins, int cur, int amount) {
6+ if (cur == coins.size () || amount <= 0 )
7+ return (amount == 0 ) ? 0 : INT_MAX - 1 ;
8+
9+ if (dp[cur][amount] != -1 )
10+ return dp[cur][amount];
11+
12+ int res = -1 ;
13+ if (coins[cur] > amount) {
14+ int doNotTakeCoin = 0 + findLowestCoins (coins, cur + 1 , amount - 0 );
15+ dp[cur][amount] = res = doNotTakeCoin;
16+ }
17+ else {
18+ int takeCoin = 1 + findLowestCoins (coins, cur + 0 , amount - coins[cur]);
19+ int doNotTakeCoin = 0 + findLowestCoins (coins, cur + 1 , amount - 0 );
20+ dp[cur][amount] = res = min (takeCoin, doNotTakeCoin);
21+ }
22+ return dp[cur][amount] = res;
23+ }
24+
25+ int coinChange (vector<int >& coins, int amount) {
26+ memset (dp, -1 , sizeof (dp));
27+ int res = findLowestCoins (coins, 0 , amount);
28+ return (res == INT_MAX - 1 ) ? -1 : res;
29+ }
30+ };
You can’t perform that action at this time.
0 commit comments