← DiffPush

Implement Queue using Array

Baseline Stack and Queues · Learning O(1) per operation · O(n)

The One-Way Conveyor Belt

A factory line moves crates onto a conveyor at one end and unloads them from the other, so the crate that arrived first is always the crate that leaves first. The belt controller accepts two commands: drop a crate onto the loading end, or release whatever is waiting at the unloading end. Your job is to replay a command log and report what each release yields.

Input: A list of operations, each either ["push", value] to enqueue or ["pop"] to dequeue.

Output: Return the list of values produced by every pop operation, in order; a pop on an empty queue yields -1.

Constraints

Examples

Example 1

Input: {"operations":[["push",2],["push",3],["pop"],["push",4],["pop"]]}
Output: [2,3]
First-in-first-out: the 2 leaves before the 3, then the 4 follows.

Example 2

Input: {"operations":[["push",7],["pop"],["pop"]]}
Output: [7,-1]
The single crate leaves first; the next release finds the belt empty.

Solve this in your browser →

Also on LeetCode ↗