|
| 1 | +<h2><a href="https://leetcode.com/problems/design-a-stack-with-increment-operation/">1381. Design a Stack With Increment Operation</a></h2><h3>Medium</h3><hr><div><p>Design a stack which supports the following operations.</p> |
| 2 | + |
| 3 | +<p>Implement the <code>CustomStack</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>CustomStack(int maxSize)</code> Initializes the object with <code>maxSize</code> which is the maximum number of elements in the stack or do nothing if the stack reached the <code>maxSize</code>.</li> |
| 7 | + <li><code>void push(int x)</code> Adds <code>x</code> to the top of the stack if the stack hasn't reached the <code>maxSize</code>.</li> |
| 8 | + <li><code>int pop()</code> Pops and returns the top of stack or <strong>-1</strong> if the stack is empty.</li> |
| 9 | + <li><code>void inc(int k, int val)</code> Increments the bottom <code>k</code> elements of the stack by <code>val</code>. If there are less than <code>k</code> elements in the stack, just increment all the elements in the stack.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | +<p><strong>Example 1:</strong></p> |
| 14 | + |
| 15 | +<pre><strong>Input</strong> |
| 16 | +["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] |
| 17 | +[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]] |
| 18 | +<strong>Output</strong> |
| 19 | +[null,null,null,2,null,null,null,null,null,103,202,201,-1] |
| 20 | +<strong>Explanation</strong> |
| 21 | +CustomStack customStack = new CustomStack(3); // Stack is Empty [] |
| 22 | +customStack.push(1); // stack becomes [1] |
| 23 | +customStack.push(2); // stack becomes [1, 2] |
| 24 | +customStack.pop(); // return 2 --> Return top of the stack 2, stack becomes [1] |
| 25 | +customStack.push(2); // stack becomes [1, 2] |
| 26 | +customStack.push(3); // stack becomes [1, 2, 3] |
| 27 | +customStack.push(4); // stack still [1, 2, 3], Don't add another elements as size is 4 |
| 28 | +customStack.increment(5, 100); // stack becomes [101, 102, 103] |
| 29 | +customStack.increment(2, 100); // stack becomes [201, 202, 103] |
| 30 | +customStack.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202] |
| 31 | +customStack.pop(); // return 202 --> Return top of the stack 102, stack becomes [201] |
| 32 | +customStack.pop(); // return 201 --> Return top of the stack 101, stack becomes [] |
| 33 | +customStack.pop(); // return -1 --> Stack is empty return -1. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>1 <= maxSize <= 1000</code></li> |
| 41 | + <li><code>1 <= x <= 1000</code></li> |
| 42 | + <li><code>1 <= k <= 1000</code></li> |
| 43 | + <li><code>0 <= val <= 100</code></li> |
| 44 | + <li>At most <code>1000</code> calls will be made to each method of <code>increment</code>, <code>push</code> and <code>pop</code> each separately.</li> |
| 45 | +</ul> |
| 46 | +</div> |
0 commit comments