Created
July 8, 2019 20:09
-
-
Save sogwiz/81659948b543a5864f3d24daabeaf4fc to your computer and use it in GitHub Desktop.
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
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