bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1172. Dinner Plate Stacks

1172. Dinner Plate Stacks

Leetcode Solutions

ArrayList and TreeSet Approach

  1. Initialize an ArrayList to store stacks and a TreeSet to track non-full stacks.
  2. For push(val), find the leftmost stack with available space using the TreeSet. If all stacks are full, create a new stack. Add val to the identified stack and update the TreeSet.
  3. For pop(), find the rightmost non-empty stack. Remove and return the top plate from this stack. Update the ArrayList and TreeSet accordingly.
  4. For popAtStack(index), check if the stack at index is non-empty. If so, remove and return the top plate, updating the TreeSet.
  5. After each operation, clean up the data structures by removing empty stacks from the ArrayList and their indices from the TreeSet.
UML Thumbnail

HashMap and Priority Queue Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...