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

Leetcode Problem 716. Max Stack

716. Max Stack

Leetcode Solutions

Approach: Two Balanced Trees

  1. Initialize two balanced trees: stack and values, and a counter cnt.
  2. For push(x), insert the element with the current cnt into both stack and values, then increment cnt.
  3. For top(), return the last element's value from stack.
  4. For peekMax(), return the last element's value from values.
  5. For pop(), remove the last element from stack, find and remove the same element in values, and return its value.
  6. For popMax(), remove the last element from values, find and remove the same element in stack, and return its value.
UML Thumbnail

Approach: Heap + Lazy Update

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...