Skip to content

Instantly share code, notes, and snippets.

@sogwiz
Created July 8, 2019 20:09
Show Gist options
  • Save sogwiz/81659948b543a5864f3d24daabeaf4fc to your computer and use it in GitHub Desktop.
Save sogwiz/81659948b543a5864f3d24daabeaf4fc to your computer and use it in GitHub Desktop.
public int coinChangeIterative(int coins[], int amount){
int[] resultArr = new int[amount+1];
for(int i = 0;i<resultArr.length; i++){
resultArr[i]=Integer.MAX_VALUE;
}
resultArr[0]=0;
for(int i =0;i<resultArr.length; i++){
int min = Integer.MAX_VALUE;
for(int coin : coins){
if(i-coin<0){
}else if(resultArr[i-coin]!=Integer.MAX_VALUE){
min=Math.min(resultArr[i-coin] + 1,min);
}
}
if(min!=Integer.MAX_VALUE){
resultArr[i]=min;
}
}
return resultArr[amount]==Integer.MAX_VALUE ? -1 : resultArr[amount];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment