File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed
algorithms/cpp/coinChange Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change 11// Source : https://leetcode.com/problems/coin-change/
2- // Author : Calinescu Valentin
2+ // Author : Calinescu Valentin, Hao Chen
33// Date : 2015-12-28
44
55/* **************************************************************************************
@@ -60,3 +60,24 @@ class Solution {
6060 return -1 ;
6161 }
6262};
63+
64+
65+ // Another DP implmentation, same idea above
66+ class Solution {
67+ public:
68+ int coinChange (vector<int >& coins, int amount) {
69+ const int MAX = amount +1 ;
70+ vector<int > dp (amount+1 , MAX);
71+ dp[0 ]=0 ;
72+
73+ for (int i=1 ; i<=amount; i++) {
74+ for (int j=0 ; j<coins.size (); j++){
75+ if (i >= coins[j]) {
76+ dp[i] = min ( dp[i], dp[i-coins[j]] + 1 );
77+ }
78+ }
79+ }
80+
81+ return dp[amount]==MAX ? -1 : dp[amount];
82+ }
83+ };
You can’t perform that action at this time.
0 commit comments