Skip to content

Commit

Permalink
Add 322 in c language
Browse files Browse the repository at this point in the history
  • Loading branch information
julienChemillier committed Sep 11, 2022
1 parent fd08e46 commit 2c68d9a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions c/322-Coin-Change.c
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];
}

0 comments on commit 2c68d9a

Please sign in to comment.