← DiffPush

Implement Stack using Array

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

The Warehouse Pallet Bay

A fulfillment center loads pallets into a single loading bay where only the most recently placed pallet is reachable. Workers can drop a new pallet onto the bay or haul the top pallet away; anything buried underneath is untouchable until everything above it leaves. You are asked to run this bay: accept drop-off commands and retrieval commands, and report what each retrieval yields.

Input: A list of operations, each either ["push", value] to load a pallet or ["pop"] to remove the top pallet.

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

Constraints

Examples

Example 1

Input: {"operations":[["push",2],["push",3],["pop"],["push",4],["pop"]]}
Output: [3,4]
Push 2 then 3; pop returns the last pushed value 3. Push 4; pop returns 4.

Example 2

Input: {"operations":[["push",7],["pop"],["pop"]]}
Output: [7,-1]
The first pop yields 7; the second finds an empty stack and returns -1.

Solve this in your browser →

Also on LeetCode ↗