Cash Machine Notes
MediumOnline Assessment
Run a day of requests through a cash machine that holds a limited number of notes of each denomination.
The machine has the denominations denoms (listed from largest to smallest) with stock[i] notes of value denoms[i] loaded. Process commands in order; each is one of:
"withdraw CARD AMOUNT": the cardCARDasks forAMOUNTin cash. The request is approved only if all of these hold, and declined otherwise (a declined request changes nothing, including the card's daily total):- the card's approved total today plus
AMOUNTis at mostdaily_limit; AMOUNTcan be paid exactly using the notes currently in stock;- the payout needs at most
max_notesnotes.
The machine pays with the fewest notes possible. If several payouts use the same fewest number of notes, it picks the one that uses more notes of the largest denomination, and if still tied, more of the next largest, and so on. Approved notes leave the machine and AMOUNT is added to the card's total.
"refill D N": addNnotes of denominationD(one ofdenoms)."newday": every card's daily total goes back to 0 (the stock stays as it is).
Input: denoms, stock, max_notes, daily_limit, commands.
Output: [payouts, stock]. payouts has one entry per withdraw command, in order: the list of notes paid, in the order of denoms (e.g. [1, 0, 0, 1]), or [] if it was declined. stock is the final number of notes of each denomination.
Example 1
- Input:
- [[50,30,20,10],[2,4,5,6],5,200,["withdraw ann 60","withdraw bob 110","withdraw ann 90","withdraw ann 50","withdraw ann 10","withdraw bob 100"]]
- Output:
- [[[1,0,0,1],[1,2,0,0],[0,2,1,1],[0,0,2,1],[],[]],[0,0,2,3]]
- Explanation
- Stock holds two 50s, four 30s, five 20s and six 10s. 60 can be paid with two notes in two ways (50+10 or 30+30); the tie goes to more of the largest note, so 50+10. Bob's 110 is 50+30+30. Ann's 90 needs four notes, either 30+30+20+10 or 30+20+20+20; the first has more 30s, so [0, 2, 1, 1]. Ann's 50 is 20+20+10 (no 50s left). Ann has now taken 200, so 10 is declined by the daily limit, and Bob's 100 cannot be made from the remaining notes.
Example 2
- Input:
- [[50,30,20,10],[1,1,1,1],3,150,["withdraw ann 110","withdraw ann 100","withdraw ann 40","refill 20 3","withdraw ann 40","withdraw ann 20","newday","withdraw ann 20"]]
- Output:
- [[[],[1,1,1,0],[],[0,0,2,0],[],[0,0,1,0]],[0,0,0,1]]
- Explanation
- 110 would use all four notes but max_notes is 3, so it is declined. 100 = 50+30+20 is approved (Ann's total 100). 40 cannot be paid (only a 10 is left), and declining it does not count. After refilling three 20s, 40 = 20+20 is approved (total 140). 20 would make 160 > 150, so it is declined. After newday the total resets and 20 is approved from the remaining 20.
Example 3
- Input:
- [[30,20],[2,3],4,100,["withdraw a 40","withdraw b 60","withdraw b 40"]]
- Output:
- [[[0,2],[2,0],[]],[0,1]]
- Explanation
- Greedy fails here: for 40, taking a 30 leaves 10 which cannot be paid, so the answer is two 20s. Then 60 is two 30s (two notes beats three 20s). For the last 40 there is only one 20 left, so it is declined.
Constraints
- 1 ≤ denoms.length ≤ 6; denoms is strictly decreasing, 1 ≤ denoms[i] ≤ 1000
- 0 ≤ stock[i] ≤ 40; 1 ≤ max_notes ≤ 60; 1 ≤ daily_limit ≤ 105
- 0 ≤ commands.length ≤ 40; amounts are between 1 and 3000; card names are short lowercase words; refill counts are between 1 and 20 and refilled denominations are always one of denoms