Egg carton packer
EasyOnline Assessment
Pack egg orders into cartons for a farm stand and report what is left on the shelf.
The stand sells eggs in two carton sizes: a dozen carton holds 12 eggs and a half-dozen carton holds 6. Eggs are never sold loose and cartons are never left partly packed on purpose, but a carton may be only partly full if the order does not fill it.
For an order of n eggs, choose the cartons as follows:
- Use the fewest cartons whose total capacity is at least
n. - If several choices use that many cartons, take the one with the smallest total capacity (the least wasted space).
(For example, 7 eggs go into one dozen carton, 13 eggs into one dozen and one half-dozen.)
Orders are handled one after another. If the stand does not have all the cartons the chosen packing needs, the order is refused: nothing is taken from the shelf for it, and the stand does not substitute other cartons (two half-dozens never stand in for a dozen). Later orders are still handled.
Input: dozens and halves, the number of dozen and half-dozen cartons in stock at the start; orders, a list of egg counts in the order received.
Output: a list [dozens_left, halves_left, refused]: the cartons left after all orders and the number of refused orders.
Example 1
- Input:
- [3,2,[30,7,13]]
- Output:
- [0,1,1]
- Explanation
- 30 eggs need two dozen cartons (24 eggs) and then 6 more, which fit in exactly one half-dozen carton. That leaves 1 dozen and 1 half-dozen carton. 7 eggs: two half-dozens would be 2 cartons, but one dozen carton is only 1 carton, so a dozen it is (it leaves 0 dozens). 13 eggs need a dozen plus a half-dozen, but no dozens are left, so the order is refused and the half-dozen is not touched. Result
[0, 1, 1].
Example 2
- Input:
- [5,5,[6,12,18,19]]
- Output:
- [1,3,0]
- Explanation
- 6 eggs: one half-dozen. 12: one dozen. 18: a dozen and a half-dozen (2 cartons, no waste). 19: 2 cartons are needed and a dozen plus a half-dozen only holds 18, so two dozens. Stock of 5 dozens and 5 halves goes down to 5 - 1 - 1 - 2 = 1 dozen and 5 - 1 - 1 = 3 half-dozens, none refused:
[1, 3, 0].
Example 3
- Input:
- [0,5,[6,12,5,24]]
- Output:
- [0,3,2]
- Explanation
- No dozens in stock. 6 eggs use a half-dozen. 12 eggs need a dozen carton, and two half-dozens are not allowed instead, so the order is refused. 5 eggs use a half-dozen. 24 eggs need two dozens: refused. Result
[0, 3, 2].
Constraints
0 <= dozens, halves <= 100,0000 <= orders.length <= 401 <= orders[i] <= 1,000