Skip to content

Instantly share code, notes, and snippets.

@hadielmougy
Created October 23, 2016 23:22
Show Gist options
  • Save hadielmougy/b4a1ee42fcfec513f779143e2cd18519 to your computer and use it in GitHub Desktop.
Save hadielmougy/b4a1ee42fcfec513f779143e2cd18519 to your computer and use it in GitHub Desktop.
Coin Change
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