MediumNo. 1381

Design a Stack With Increment Operation — LeetCode 1381 Python Solution

Pattern
Stack
Topics
Stack, Design, Array
Reading time
6 min

The problem

Design a stack that supports increment operations on its elements. Implement the CustomStack class: CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack.

Example

Input
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
Output
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
Explanation
CustomStack stk = new CustomStack(3); // Stack is Empty []

Python solution

PythonSolution
class CustomStack:
    def __init__(self, maxSize: int):
        self.stk = [0] * maxSize
        self.add = [0] * maxSize
        self.i = 0

    def push(self, x: int) -> None:
        if self.i < len(self.stk):
            self.stk[self.i] = x
            self.i += 1

    def pop(self) -> int:
        if self.i <= 0:
            return -1
        self.i -= 1
        ans = self.stk[self.i] + self.add[self.i]
        if self.i > 0:
            self.add[self.i - 1] += self.add[self.i]
        self.add[self.i] = 0
        return ans

    def increment(self, k: int, val: int) -> None:
        i = min(k, self.i) - 1
        if i >= 0:
            self.add[i] += val


# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)

Complexity

Time
O(1)
Space · auxiliary
O(n)

Pattern: Stack

When the most recent unresolved thing is the one that matters, use a stack. LeetCode 1381. Design a Stack With Increment Operation is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Stack.

The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
1472Design Browser HistoryMedium
0042Trapping Rain WaterHard
0150Evaluate Reverse Polish NotationMedium
0155Min StackMedium
0173Binary Search Tree IteratorMedium
0225Implement Stack using QueuesEasy

Frequently asked questions

How hard is LeetCode 1381. Design a Stack With Increment Operation?

LeetCode 1381. Design a Stack With Increment Operation is rated Medium on LeetCode.

What is the time complexity of LeetCode 1381. Design a Stack With Increment Operation?

The Python solution on this page runs in O(1).

What is the space complexity of LeetCode 1381. Design a Stack With Increment Operation?

The Python solution on this page uses O(n) auxiliary space.

What topics does LeetCode 1381. Design a Stack With Increment Operation cover?

LeetCode 1381. Design a Stack With Increment Operation is tagged Stack, Design and Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.