Design a Stack With Increment Operation

Medium · Stack & Monotonic Stack · about 15 minutes

Asked at Amazon, Google, Microsoft.

Design a stack with a maximum size.

  • CustomStack(maxSize) creates it.
  • push(x) pushes x if the stack has fewer than maxSize elements.
  • pop() pops and returns the top, or -1 if empty.
  • increment(k, val) adds val to the bottom k elements (all of them if fewer).