1381. Design a Stack With Increment Operation - Detailed Explanation
Problem Statement
You need to design a custom stack that supports the following operations:
- 
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 hasn’t reached the maximum size.
- 
int pop(): 
 Pops and returns the top element of the stack. If the stack is empty, returns-1.
- 
void increment(int k, int val): 
 Increments the bottomkelements of the stack byval. If there are fewer thankelements, increment all of them.
Example:
Input: ["CustomStack","push","push","pop","push","push","push","push","increment","increment","pop","pop","pop","pop"] [[3],[1],[2],[],[2],[3],[4],[5],[100,2],[2,100],[],[],[],[]] Output: [null,null,null,2,null,null,null,null,null,null,103,202,201,-1]
Explanation:
- CustomStack(3)→ create a stack of maxSize 3.
- push(1)→ stack becomes [1].
- push(2)→ stack becomes [1, 2].
- pop()→ returns 2, stack becomes [1].
- push(2)→ stack becomes [1, 2].
- push(3)→ stack becomes [1, 2, 3].
- push(4)→ stack is full, so no change.
- push(5)→ still full.
- increment(100, 2)→ add 2 to all bottom 100 elements; effectively, stack becomes [3, 4, 5].
- increment(2, 100)→ add 100 to the bottom 2 elements; stack becomes [103, 104, 5].
- pop()→ returns 5 (top element plus any increment), but because of our lazy propagation (explained below), the effective value becomes 5 + some carried increment; see walkthrough.
- And so on.
Key Insights and Hints
- 
Lazy Increment Technique: 
 Directly updating the bottom ( k ) elements for eachincrementoperation is costly (O(k) per call). Instead, we can use an auxiliary array to store "pending" increments that should be applied when an element is popped.
- 
How It Works: - Maintain two arrays:
- stack[]: Stores the actual values pushed.
- inc[]: An array (of size- maxSize) that tracks the extra increment value that should be added to the element at that index.
 
- For increment(k, val), instead of updating the bottom ( k ) elements immediately, addvaltoinc[min(k, stack.size) - 1].
- When performing a pop(), add the pending increment at that index to the popped value, then propagate the increment to the next element in the stack (i.e.inc[i-1] += inc[i]).
 
- Maintain two arrays:
- 
Avoiding Extra Work: 
 This lazy propagation approach ensures that eachincrementoperation runs in O(1) time, andpop()operations remain O(1) as well.
Step-by-Step Walkthrough
- 
Initialization: - The constructor creates an empty stack and an auxiliary incarray (of the same maximum size) initialized with zeros.
 
- The constructor creates an empty stack and an auxiliary 
- 
Push Operation: - Add an element to the stack only if the current size is less than maxSize.
 
- Add an element to the stack only if the current size is less than 
- 
Pop Operation: - Check if the stack is empty; if yes, return -1.
- Let ibe the current index (i.e.stack.size - 1).
- Before popping, propagate the incvalue at indexito indexi - 1(ifi > 0).
- The result to return is stack[i] + inc[i].
- Reset inc[i]to 0 and remove the element from the stack.
 
- 
Increment Operation: - Determine the index to update: ( i = \min(k, \text{stack.size}) - 1 ).
- If iis valid (i.e. ( \geq 0 )), addvaltoinc[i].
 
Code Implementations
Python Code
Java Code
Complexity Analysis
- 
Time Complexity: - push: O(1)
- pop: O(1) (includes propagating the pending increment to the next element)
- increment: O(1) (only updates a single index in the auxiliary array)
 
- 
Space Complexity: - O(maxSize) is used for storing the stack and the auxiliary incarray.
 
- O(maxSize) is used for storing the stack and the auxiliary 
Common Mistakes and Edge Cases
- 
Double Incrementing a Cell: 
 Be careful not to increment the same cell twice if the same cell is affected by multipleincrementoperations; this is why the lazy propagation method is used.
- 
Stack Full Check: 
 Ensure that you do not push beyond themaxSize.
- 
Empty Stack: 
 When popping from an empty stack, return-1immediately without attempting to access any elements.
- 
Propagation of Increments: 
 When popping, correctly propagate the pending increment from the top element to the next element. Forgetting to do this will result in incorrect values.
Related Problems for Further Practice
- 
Min Stack (LeetCode 155): Design a stack that supports push, pop, and retrieving the minimum element in O(1) time. 
- 
Design Circular Queue: Implement a circular queue with various operations. 
- 
Online Stock Span: A problem that involves efficient updates and queries on a sequence of numbers. 
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78