Design a Stack With Increment Operation Problem
#1381
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 1381.
Design a stack that supports increment operations on its elements.
Implement the CustomStack class:
CustomStack(int maxSize)Initializes the object withmaxSizewhich is the maximum number of elements in the stack.void push(int x)Addsxto the top of the stack if the stack has not reached themaxSize.int pop()Pops and returns the top of the stack or-1if the stack is empty.void inc(int k, int val)Increments the bottomkelements of the stack byval. If there are less thankelements in the stack, increment all the elements in the stack.
Example 1:
Input
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
Output
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
Explanation
CustomStack stk = new CustomStack(3); // Stack is Empty []
stk.push(1); // stack becomes [1]
stk.push(2); // stack becomes [1, 2]
stk.pop(); // return 2 --> Return top of the stack 2, stack becomes [1]
stk.push(2); // stack becomes [1, 2]
stk.push(3); // stack becomes [1, 2, 3]
stk.push(4); // stack still [1, 2, 3], Do not add another elements as size is 4
stk.increment(5, 100); // stack becomes [101, 102, 103]
stk.increment(2, 100); // stack becomes [201, 202, 103]
stk.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202]
stk.pop(); // return 202 --> Return top of the stack 202, stack becomes [201]
stk.pop(); // return 201 --> Return top of the stack 201, stack becomes []
stk.pop(); // return -1 --> Stack is empty return -1.
Constraints:
1 <= maxSize, x, k <= 10000 <= val <= 100- At most
1000calls will be made to each method ofincrement,pushandpopeach separately.
Solution
Approach: Array with Lazy Increments
The simple way stores the stack in an array and, on increment(k, val), adds val to the bottom min(k, size) elements. That is O(k) per call, which is fine for these limits, but every operation can be O(1) by recording increments lazily.
Keep a second array add, where add[i] means "every element at index i or below still has to get this much added":
push(x): if the stack is not full, putxon top.increment(k, val): lettop = min(k, size) - 1. If the stack is not empty, doadd[top] += val. We touch only one entry instead ofkelements.pop(): if the stack is empty, return-1. Otherwise remove the top element at indexiand returnstack[i] + add[i]. The elements below it still owe that increment, so move it down withadd[i - 1] += add[i], then resetadd[i] = 0so a later push to indexistarts clean.
Every element is popped only through the top, so by the time an element is popped, all increments that covered it have been passed down to its index.
Complexity: O(1) time for every operation, and O(maxSize) space.
class CustomStack:
def __init__(self, maxSize: int):
self.stack = []
self.max_size = maxSize
# add[i]: an increment still owed to the elements at indexes 0..i (applied lazily on pop).
self.add = [0] * maxSize
def push(self, x: int) -> None:
if len(self.stack) < self.max_size:
self.stack.append(x)
def pop(self) -> int:
if not self.stack:
return -1
i = len(self.stack) - 1
value = self.stack.pop() + self.add[i]
if i > 0:
self.add[i - 1] += self.add[i] # the elements below still owe this increment
self.add[i] = 0
return value
def increment(self, k: int, val: int) -> None:
top = min(k, len(self.stack)) - 1
if top >= 0:
self.add[top] += val