forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd08e46
commit 2c68d9a
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Return the fewest number of coins that you need to make up that amount. | ||
Time; O(nm) where n is the amount desired and m the number of coins | ||
Space: O(n) | ||
*/ | ||
|
||
int min(unsigned int a, int b) { | ||
return a<b?a:b; | ||
} | ||
|
||
int coinChange(int* coins, int coinsSize, int amount){ | ||
if (amount==0) | ||
return 0; | ||
int dp[amount +1]; | ||
for (int i=1; i<=amount; i++) | ||
dp[i] = UINT_MAX; | ||
dp[0] = 0; | ||
for (int i=1; i<=amount; i++) { | ||
unsigned int cpt = UINT_MAX; | ||
for (int j=0; j<coinsSize; j++) { | ||
if (i>=coins[j] && cpt>dp[i-coins[j]]) | ||
cpt = dp[i-coins[j]]; | ||
} | ||
if (cpt!=UINT_MAX) | ||
dp[i] = cpt+1; | ||
} | ||
return dp[amount]==UINT_MAX?-1:dp[amount]; | ||
} |