Opening your workspace…
Workspace/Coding room
Opening your workspace…
When a blend runs out, the roaster wakes a dragon who charges by the yawn. The shelf list stock gives the blend ID of each bag, and orders gives the requested blend IDs in arrival order. For each order, use one available bag of its blend and record 0. If none is left, wake the dragon and record 1. A freshly roasted bag goes straight to that customer. Return the records in order, one per request, so the dragon can send her invoice.
[4,4,9], [4,9,4,4]Output[0,0,0,1]The first three orders take both bags of blend 4 and the bag of blend 9. Nothing is left for the last order, so it needs a new bag.
[], [2,2,0]Output[1,1,1]Nothing is in stock, so every order needs a new bag. That includes the second order for blend 2, because the bag roasted for the first one was not added to stock.
A set can tell you whether a blend is in stock, but not how many bags of it are left. Keep a count for each blend instead.
/** @param {number[]} stock * @param {number[]} orders * @returns {number[]} */ function solve(stock, orders) { // Mark each order that needs a newly roasted bag. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.
[4,4,9], [4,9,4,4][0,0,0,1]