Design a Stack With Increment Operation - Problem
Design a custom stack data structure that supports traditional stack operations (push and pop) along with a special increment operation that can modify multiple elements at once.
Your task is to implement the CustomStack class with the following methods:
CustomStack(int maxSize): Initialize the stack with a maximum capacityvoid push(int x): Add elementxto the top of the stack (only if capacity allows)int pop(): Remove and return the top element, or-1if emptyvoid inc(int k, int val): Addvalto the bottomkelements of the stack
Challenge: The increment operation should be efficient even when performed frequently on large stacks!
Input & Output
basic_operations.py โ Python
$
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]
๐ก Note:
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, 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], Don't add another element 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, stack becomes [201, 202]
stk.pop(); // return 202 --> Return top of the stack, stack becomes [201]
stk.pop(); // return 201 --> Return top of the stack, stack becomes []
stk.pop(); // return -1 --> Stack is empty return -1.
capacity_limits.py โ Python
$
Input:
["CustomStack","push","push","push","push","increment","pop"]
[[2],[1],[2],[3],[4],[2,10],[]]
โบ
Output:
[null,null,null,null,null,null,12]
๐ก Note:
Stack with capacity 2. Push 1,2 successfully, push 3,4 ignored due to capacity. increment(2,10) adds 10 to bottom 2 elements. Pop returns 2+10=12.
empty_stack_edge_case.py โ Python
$
Input:
["CustomStack","pop","increment","pop"]
[[1],[],[1,100],[]]
โบ
Output:
[null,-1,null,-1]
๐ก Note:
Operations on empty stack. Pop returns -1, increment on empty stack does nothing, pop still returns -1.
Constraints
- 1 โค maxSize โค 1000
- 1 โค x โค 1000
- 1 โค k โค 1000
- 0 โค val โค 100
- At most 1000 calls will be made to each of push, pop and increment
Visualization
Tap to expand
Understanding the Visualization
1
Stack Setup
Initialize empty stack with maximum capacity
2
Push Operations
Add elements to top of stack (like stacking plates)
3
Increment Magic
Mark bottom k elements for future bonus (lazy approach)
4
Pop with Bonus
Remove top element and apply accumulated bonuses
Key Takeaway
๐ฏ Key Insight: Lazy propagation transforms an O(k) increment operation into O(1) by deferring the actual work until elements are popped
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code