Skip to content

Commit 40b3f84

Browse files
committed
Add solution for FrobeniusCoinProblem task
1 parent 35280ea commit 40b3f84

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package by.andd3dfx.numeric;
2+
3+
import by.andd3dfx.dynamic.ChangeWithMinNumberOfCoins;
4+
5+
/**
6+
* See <a href="https://en.wikipedia.org/wiki/Coin_problem">article</a> in wiki
7+
* <p>
8+
* Find the largest monetary amount that cannot be obtained using only coins of specified denominations.
9+
*/
10+
public class FrobeniusCoinProblem {
11+
12+
public static int find(int[] nominals) {
13+
var capAmount = determineStartingValueEnhanced(nominals);
14+
for (var amount = capAmount; amount >= 1; amount--) {
15+
if (ChangeWithMinNumberOfCoins.determine_usingMemoization(nominals, amount) == -1) {
16+
return amount;
17+
}
18+
}
19+
return 1;
20+
}
21+
22+
/**
23+
* Method wasn't deleted for demo purposes
24+
*/
25+
private static int determineStartingValuePrimitive(int[] nominals) {
26+
var result = 1;
27+
for (int nominal : nominals) {
28+
result *= nominal;
29+
}
30+
System.out.println(result);
31+
return result;
32+
}
33+
34+
private static int determineStartingValueEnhanced(int[] nominals) {
35+
return LeastCommonMultiple.find(nominals);
36+
}
37+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package by.andd3dfx.numeric;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class FrobeniusCoinProblemTest {
8+
9+
@Test
10+
public void testFind() {
11+
// Formula g = a1*a2-a1-a2 works only for relatively prime numbers a1, a2
12+
assertThat(FrobeniusCoinProblem.find(new int[]{2, 5})).isEqualTo(2 * 5 - 2 - 5);
13+
assertThat(FrobeniusCoinProblem.find(new int[]{3, 7})).isEqualTo(3 * 7 - 3 - 7);
14+
assertThat(FrobeniusCoinProblem.find(new int[]{6, 7})).isEqualTo(6 * 7 - 6 - 7);
15+
16+
// McNuggets problem
17+
assertThat(FrobeniusCoinProblem.find(new int[]{6, 9, 20})).isEqualTo(43);
18+
}
19+
}

0 commit comments

Comments
 (0)