Opening your workspace…
Workspace/Coding room
Opening your workspace…
At the cocoa hatch, Dot can see only the tip of the next customer's hat. She keeps an event log to make sure the tallest hat never jumps the queue. Read events in order. A positive number is a new arrival's ticket, joining the back of the line. At each zero, serve the longest-waiting customer and record their ticket number, or record -1 if the hatch opens onto thin air. Return those records in order. Equal ticket numbers belong to separate customers. Anyone still waiting gets a place in tomorrow's log.
[8,3,0,5,0,0]Output[8,3,5]The tickets arrive in the order 8, 3, 5, and the three zeros serve them in that same order.
[0,4,0,0]Output[-1,4,-1]Nobody is waiting at the first zero or at the last one. The middle zero serves ticket 4.
Store the arrivals in an array and keep an index for the front of the line, moving it forward each time you serve. Removing the first element of the array every time would shift everything after it.
/** @param {number[]} events * @returns {number[]} */ function solve(events) { // Return the ticket served by each zero, or -1. }
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.
[8,3,0,5,0,0][8,3,5]