Sushi belt bill
EasyOnline Assessment
Total up the bill of every table at a conveyor-belt sushi bar, where a plate that is put back quickly is not charged.
Plates are priced by colour. prices lists entries "<colour> <price>", such as "red 120". The belt camera produces log, a list of strings "<seconds> <table> <action> <colour>" such as "95 t4 take red", already in time order (events with the same time are in the order given). The action is take or return.
takeputs one plate of that colour on the table's bill. A colour that is not inprices(the free dip dishes, say) is ignored, and so is areturnof such a colour.- A diner may put a plate back on the belt.
returnremoves a plate from the same table's bill only if that table has a plate of that colour that was taken and not yet returned, and the most recent such plate was taken at most 20 seconds earlier (20 itself counts). It cancels that plate. Otherwise the return is ignored and nothing changes: the plate is already half eaten or was never theirs. - Different colours never interact, and tables never interact.
Input: prices and log.
Output: a list of strings "<table> <total>", one for each table whose final bill is greater than 0, where total is the sum of the prices of the plates still on its bill. Order the lines by total descending, and tables with equal totals by table name in ordinary string order (so "t10" comes before "t2"). Return [] if no table owes anything.
Example 1
- Input:
- [["red 120","blue 180","gold 350","white 100"],["10 t1 take red","14 t1 take red","20 t1 take blue","30 t1 return red","31 t2 take gold","40 t2 take sauce","45 t2 return sauce","52 t1 return red"]]
- Output:
- ["t2 350","t1 300"]
- Explanation
- Table
t1takes red at 10 and 14, then blue at 20. The return of red at 30 cancels the red from 14 (16 s earlier). The return at 52 would cancel the red from 10, but that was 42 s ago, so it is ignored.t1owes 120 + 180 = 300.t2owes 350 for gold; thesauceplate is not priced and its return is ignored. Totals 350 and 300 give["t2 350", "t1 300"].
Example 2
- Input:
- [["red 120","blue 180","gold 350","white 100"],["0 t1 take red","15 t1 take red","22 t1 return red","40 t1 return red","50 t2 take white","70 t2 return white","71 t2 return white","80 t3 return gold"]]
- Output:
- ["t1 120"]
- Explanation
t1takes red at 0 and 15. The return at 22 cancels the plate from 15 (7 s earlier). The return at 40 would face the plate from 0, which is 40 s old, so it is ignored andt1still owes 120.t2takes white at 50 and returns it exactly 20 s later, which counts, so it owes nothing; its second return finds no plate.t3returns a plate it never took, which is ignored. Onlyt1is listed.
Example 3
- Input:
- [["red 120","blue 180","gold 350","white 100"],["5 t1 take red","6 t1 return red","8 t2 take gold","9 t2 return gold"]]
- Output:
- []
- Explanation
- Every plate is returned within a second, so no table owes anything and the result is empty.
Constraints
1 <= prices.length <= 20; each entry is"<colour> <price>"with a lowercase colour and an integer price from1to10,000, colours distinct0 <= log.length <= 2,000; each entry is"<seconds> <table> <action> <colour>"withsecondsfrom0to100,000, in non-decreasing order ofseconds- table names are non-empty strings of lowercase letters and digits