ChangeOptimizer
SRM 195 · 2004-05-18 · by schveiguy
Problem Statement
Johnny Indecision has a change purse with some change in it. However, he is deathly afraid of having to figure out what might happen if he has to spend some of it. This fear arises because there may be more than one way to give out a certain amount of change. For example, if he has 1 dime (worth 10 cents) and 2 nickels (worth 5 cents apiece), there are two ways to make 10 cents. He also does not want to incur any more change, so he wants to be sure that he has exact change for any amount up to the amount of money he has.
So he would like to exchange his current change with some more predictable coins at the bank. As the bank clerk, you must solve Johnny's dilemma by giving him enough change to allow him to be able to spend any amount of money up to the amount he currently has, but the coins you give him must provide exactly one way to make each of those amounts. In addition, he would like to have as few coins as possible, even if it means having more smaller denomination coins. For example, if Johnny brings 49 cents to the bank, and the only coins available are 1, 10, and 25 cent pieces, there are three valid options:
- one 25-cent piece and 24 1-cent pieces
- four 10-cent pieces and nine 1-cent pieces
- 49 1-cent pieces
- two 1-cent pieces, a one 3-cent piece, and a 6-cent piece
- one 1-cent piece, two 2-cent pieces, and a 6-cent piece
You will be given a
Constraints
- coinTypes has between 1 and 50 elements, inclusive.
- Each element of coinTypes is between 1 and 100,000,000, inclusive.
- There will be no repeated values in coinTypes.
- There will be exactly one element in coinTypes equal to 1.
- value is between 1 and 1,000,000,000, inclusive.
{1,10,25}
49
Returns: { 9, 4, 0 }
From the problem statement
{1,3,6,2}
11
Returns: { 2, 1, 1, 0 }
From the problem statement
{1,2,3,4,5,6,7,8,9,10}
1234567
Returns: { 1, 1, 0, 1, 0, 0, 0, 154320, 0, 0 }
{91001,3567,222123,4456,1,732234,123793,982312,14781}
65864135
Returns: { 0, 0, 0, 0, 14780, 0, 0, 0, 4455 }
{1,10,100,1000,10000}
1000000
Returns: { 1000000, 0, 0, 0, 0 }
Submissions are judged against all 58 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ChangeOptimizer with a public method vector<int> fewestCoins(vector<int> coinTypes, int value) · 58 test cases · 2 s / 256 MB per case