intCount(int n, int m){ // n:the total sum of money, m:the kinds of conis // use first m kinds coins to exchange n yuan // num[i] will be storing the number of solutions for value i. // We need n+1 rows as the table is consturcted in bottom up manner using the base case (n = 0) // Base case (If given value is 0) num[0] = 1;
// Pick all coins one by one and update the num[] values // after the index greater than or equal to the value of the // picked coin for(int i=0; i<m; i++) for(int j=kind[i]; j<=n; j++) num[j] += num[j- kind[i]]; return num[target]; // target is the aimed money to exchange }
voidcountShop(){ shopmin[0] = 0; for (int i=1; i<MAXMONEY; i++) shopmin[i] = 9999; //just a large nuber for (int i=0; i<MAXCOIN; i++) { for (int j=kind[i]; j<MAXMONEY; j++) { shopmin[j] = Math.min(shopmin[j-kind[i]]+1, shopmin[j]); } } }
voidcountMy(){ // eachcoin[]: numbers of each coin mymin[0] = 0; for (int i=1; i<MAXMONEY; i++) mymin[i] = 9999; for (int c=0; c<MAXCOIN; c++) { while (eachcoin[c] > 0) { for (int i=MAXMONEY-kind[c]-1; i>=0; i--) { if (mymin[i] < 9999) mymin[i+kind[c]] = Math.min(mymin[i]+1, mymin[i+kind[c]]); } eachcoin[c]--; } } }
intfindAns(){ int ans = 9999; for (int i=charge; i<MAXMONEY; i++){ if (9999 != mymin[i]){ ans = Math.min(ans, mymin[i]+shopmin[i-charge]); } } return ans; }