Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions CoinsChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time Complexity:O(n*m)
// Space complexity: O(n*m)
//We build a 2D DP table where dp[i][j] is the min coins to make amount j using first i coins.
// If we can't pick the current coin, we just use the value from the row above.
// Otherwise, we take the min between skipping it and picking it (which adds 1 coin)

class Solution {
public int coinChange(int[] coins, int amount) {
int rows = coins.length;
int columns = amount;
// declare a 2-D Array to capture the values of no of coins for a given amount
int[][] dp = new int [rows+1][columns+1];
// fill first row with value > total amount
for (int col = 0; col<= columns; col++) {
dp[0][col] = amount+1;
}
// fill the remaining rows and clolumns of the table
for(int i = 1; i <= rows; i++){

for(int j= 1; j<= columns; j++) {
if(j < coins[i-1]){
dp[i][j] = dp[i-1][j];
} else {
dp[i][j] = Math.min(dp[i-1][j], dp[i][j-coins[i-1]]+1);
}
}
}
if(dp[rows][columns] > amount) return -1;
return dp[rows][columns];
}
}