Stack With a Range-Increment Operation

Implement stackOpsIncrement

Design a stack that supports the usual push and pop, plus increment(k, val), which adds val to each of the bottom min(k, size) elements currently on the stack. Applying an increment to potentially many elements at once seems to demand touching each of them — but the actual values only ever matter once an element is popped and revealed. That gap between "when the increment happens" and "when the value is needed" is exploitable: record each increment's effect in exactly one place (a parallel array, at the position marking how far down it reaches), and let that pending amount quietly cascade downward one step at a time, only when a pop makes it necessary. Every operation — push, pop, and increment alike — becomes a fixed, small amount of work, no matter how many elements an increment conceptually covers.

Example 1:

Input: ops = ["push","push","push","increment","pop","pop","pop"], args = [4,3,2,2,0,0,0], args2 = [0,0,0,10,0,0,0]

Output: [2,13,14]

Example 2:

Input: ops = ["push","push","increment","push","pop","pop","pop"], args = [1,2,100,3,0,0,0], args2 = [0,0,5,0,0,0,0]

Output: [3,7,6]

Example 3:

Input: ops = ["push","push","push","increment","increment","pop","pop","pop"], args = [5,5,5,1,2,0,0,0], args2 = [0,0,0,3,4,0,0,0]

Output: [5,9,12]

+ 2 hidden test cases run on Submit.

Constraints:

  • 1 ≤ ops.length ≤ 20
  • ops[i] is one of "push", "pop", "increment"; args[i] is the pushed value (for push) or k (for increment); args2[i] is val (for increment), 0 otherwise
  • increment(k, val) adds val to each of the bottom min(k, current size) elements
  • "pop" on an empty stack returns -1

ops =

["push", "push", "push", "increment", "pop", "pop", "pop"]

args =

[4, 3, 2, 2, 0, 0, 0]

args2 =

[0, 0, 0, 10, 0, 0, 0]