-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoin_Change.java
More file actions
36 lines (26 loc) · 841 Bytes
/
Copy pathCoin_Change.java
File metadata and controls
36 lines (26 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Greedy_Algorithm;
import java.util.*;
public class Coin_Change {
public static int coinChangeGreedy(int[] coins, int n) {
int result = 0;
int carry;
ArrayList<Integer> arrayList = new ArrayList<>();
while (n != 0) {
for (int i = coins.length - 1 ; i>=0 ; i--) {
if (coins[i] <= n) {
n = n - coins[i];
System.out.println("Adding " + coins[i] + " tk note");
i++;
result++;
}
}
}
return result;
}
public static void main(String[] args) {
int[] coins = {10, 20, 50, 100, 200};
int n = 50;
int result = coinChangeGreedy(coins, n);
System.out.println("You will give him " + result + " note");
}
}