-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoinChangeExample.java
More file actions
35 lines (32 loc) · 953 Bytes
/
CoinChangeExample.java
File metadata and controls
35 lines (32 loc) · 953 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 com.codinginterview;
import java.util.List;
public class CoinChangeExample {
public static void main(String[] args) {
System.out.println("Coin Change Example!");
List coins = List.of(1,5,10,25);
int num = 120;
System.out.println(changeCoinCount(coins, num));
}
private static int changeCoinCount(List<Integer> coins, int num){
int min = num;
int count = 0;
if(min <= 0) return count;
while(min > 0){
if(coins.contains(min)){
return ++count;
}
for (int i = coins.size() -1; i>= 0; i--){
int temp = min - coins.get(i);
if(temp >= 0){
min = temp;
count++;
}
if(temp >= coins.get(i)){
min = temp;
i++;
}
}
}
return count;
}
}