Skip to content

Practice Dinner Plate Stacks

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1172
LeetCode Problem

Dinner Plate Stacks

Problem Statement

DinnerPlates(capacity) maintains numbered stacks with equal capacity. push(val) uses the leftmost nonfull stack. pop() removes from the rightmost nonempty stack, returning -1 if all are empty. popAtStack(index) removes that stack's top or returns -1 when empty. Other stacks retain their indices. Practice approach: Track nonfull and nonempty stack indices with ordered sets or heaps and discard stale entries. Every removal creates insertion space, including holes in the middle. Trim empty trailing stacks carefully without shifting interior indices. Test refilling a hole before extending the row.

Source: Dinner Plate Stacks on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["DinnerPlates","push","push","push","popAtStack","push","pop","pop","pop","pop"]
[[2],[4],[7],[9],[0],[6],[],[],[],[]]
Output
[null,null,null,null,7,null,9,6,4,-1]
Explanation

Removing 7 creates room in stack zero; 6 refills it. pop still removes 9 from the rightmost stack first.

Constraints

1 <= capacity, val <= 2 * 10^4
0 <= index <= 10^5
At most 2 * 10^5 total calls.

Related LeetCode Problems

Practice more problems similar to Dinner Plate Stacks.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.