Created
October 23, 2016 23:22
-
-
Save hadielmougy/b4a1ee42fcfec513f779143e2cd18519 to your computer and use it in GitHub Desktop.
Coin Change
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
package com.algorithms; | |
/** | |
* Created by Hadi on 10/21/2016. | |
*/ | |
public class CoinChange { | |
public static void main(String[] args){ | |
int arr[] = {1, 3, 5}; | |
int n = 21; | |
System.out.println(getCoins(arr, n)); | |
} | |
private static int getCoins(int[] ARR, int N){ | |
int[] states = new int[N +1]; | |
states[0] = 0; | |
for(int y = 1; y<states.length;y++) | |
for(int i = 0; i <ARR.length;i++){ | |
int coin = ARR[i]; | |
if(y-coin >= 0){ | |
states[y] = states[y-coin]+1; | |
} | |
} | |
return states[states.length-1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment