TopCoder problem "ChangePurse" used in SRM 195 (Division II Level Three)



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. If multiple ways exist to give Johnny change, return the one with the most coins of the highest denomination. If multiple ways exist which have the most coins of the highest denomination, return the one with the most coins of the second-highest denomination, and so on. 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:

  1. one 25-cent piece and 24 1-cent pieces
  2. four 10-cent pieces and nine 1-cent pieces
  3. 49 1-cent pieces
Each of these combinations results in only one way to make 1 to 49 cents, and even though it does not produce the fewest coins, the first way is preferable because it contains a high-valued 25 cent piece.

You will be given a int[] coinTypes, which will be a list containing the value of each type of coin in the given monetary system (which will always include a coin valued at 1). You will also be given an int value specifying the amount of money Johnny has. The return value should be a int[] indicating how many coins of each type you should give Johnny back. Element i of the return value should be how many coins valued at coinTypes[i] should be given to Johnny.

 

Definition

    
Class:ChangePurse
Method:optimalCoins
Parameters:int[], int
Returns:int[]
Method signature:int[] optimalCoins(int[] coinTypes, int value)
(be sure your method is public)
    
 

Constraints

-coinTypes has between 1 and 50 elements, inclusive.
-Each element of coinTypes is between 1 and 1000000, 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 1000000000, inclusive.
 

Examples

0)
    
{1,25,10}
49
Returns: { 24,  1,  0 }
The example from the problem statement.
1)
    
{1,7}
49
Returns: { 49,  0 }
Note that {7,6} is an invalid return because even though it equals 49 cents, and it allows all values between 1 and 49, it would allow multiple ways to make 7 cents. This is the thing that Johnny fears the most.
2)
    
{11,5,10,1}
109
Returns: { 9,  0,  0,  10 }
3)
    
{29210, 58420, 350520, 708072, 720035, 230, 42355,
 1, 59006, 985, 236024, 163, 701040}
929579039
Returns: { 1,  5,  1,  0,  0,  126,  0,  229,  0,  0,  0,  0,  1325 }
4)
    
{795, 5536, 26, 915, 18590, 60840, 49140, 2,
 119700, 162235, 369000, 383936, 478800, 505995,
 949, 95984, 455, 8, 420, 239400, 276800, 191968,
 619305, 654810, 706420, 393120, 738000, 767872,
 425880, 786240, 830400, 676, 4500, 851760, 957600,
 648940, 1, 112, 180, 457}
687245439
Returns: 
{ 0,  0,  0,  0,  0,  0,  0,  3,  0,  0,  0,  1,  0,  0,  0,  1,  0,  13,  0,  0,  0,  1,  0,  0,  0,  0,  0,  894,  0,  0,  0,  0,  0,  0,  0,  0,  1,  856,  0,  0 }
5)
    
{494208, 722376, 731798, 809064, 920448, 1, 988416, 9152, 158,
 991014, 282720, 40132, 608, 143, 289755, 734, 579510, 828400,
 330338, 816, 460224, 27456, 675783, 331, 436, 82368, 729, 306,
 202266, 247104, 414200, 705}
419088383
Returns: 
{ 1,  0,  0,  0,  0,  142,  423,  2,  0,  0,  0,  0,  0,  63,  0,  0,  0,  0,  0,  0,  0,  2,  0,  0,  0,  2,  0,  0,  0,  1,  0,  0 }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2442

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5070&pm=2442

Writer:

schveiguy

Testers:

lbackstrom , brett1479

Problem categories:

Advanced Math