class Solution {
public int coinChange(int[] coins, int amount) {
int[] memo = new int[amount+1];
for (int i=1; i<=amount; i++){
for(int coin : coins){
// check the conis in previous amounts
if (i-coin>=0 && memo[i-coin]!=-1){
memo[i]= memo[i]>0? Math.min(memo[i], memo[i-coin]+1) : memo[i-coin]+1;
}
}
// make sure to invalid the amount if no coins match
if (memo[i]==0) memo[i]=-1;
}
return memo[amount];
}
}