-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_of_coins.java
45 lines (39 loc) · 1.61 KB
/
sum_of_coins.java
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
36
37
38
39
40
41
42
43
44
45
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] elements = in.nextLine().substring(7).split(", ");
int[] coins = new int[elements.length];
for (int i = 0; i < coins.length; i++) {
coins[i] = Integer.parseInt(elements[i]);
}
int targetSum = Integer.parseInt(in.nextLine().substring(5));
Map<Integer, Integer> usedCoins = chooseCoins(coins, targetSum);
for (Map.Entry<Integer, Integer> usedCoin : usedCoins.entrySet()) {
System.out.println(usedCoin.getKey() + " -> " + usedCoin.getValue());
}
}
public static Map<Integer, Integer> chooseCoins(int[] coins, int targetSum) {
// TODO
Map<Integer, Integer> usedCoins = new LinkedHashMap<>();
List<Integer> sortedCoins = Arrays.stream(coins).boxed().sorted(Collections.reverseOrder())
.collect(Collectors.toList());
int currentSum = 0;
int index = 0;
while (currentSum != targetSum && index < sortedCoins.size()) {
int currentCoin = sortedCoins.get(index);
int remainingSum = targetSum - currentSum;
int numberOfCoins = remainingSum / currentCoin;
if (numberOfCoins > 0) {
usedCoins.put(currentCoin, numberOfCoins);
currentSum += currentCoin * numberOfCoins;
}
index++;
}
if (currentSum != targetSum) {
throw new IllegalArgumentException();
}
return usedCoins;
}
}