|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../lucky-numbers-in-a-matrix "Lucky Numbers in a Matrix") |
| 9 | + |
| 10 | +[Next >](../balance-a-binary-search-tree "Balance a Binary Search Tree") |
| 11 | + |
| 12 | +## [1381. Design a Stack With Increment Operation (Medium)](https://leetcode.com/problems/design-a-stack-with-increment-operation "设计一个支持增量操作的栈") |
| 13 | + |
| 14 | +<p>Design a stack which supports the following operations.</p> |
| 15 | + |
| 16 | +<p>Implement the <code>CustomStack</code> class:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <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> |
| 20 | + <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> |
| 21 | + <li><code>int pop()</code> Pops and returns the top of stack or <strong>-1</strong> if the stack is empty.</li> |
| 22 | + <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> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong>Example 1:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input</strong> |
| 30 | +["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] |
| 31 | +[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]] |
| 32 | +<strong>Output</strong> |
| 33 | +[null,null,null,2,null,null,null,null,null,103,202,201,-1] |
| 34 | +<strong>Explanation</strong> |
| 35 | +CustomStack customStack = new CustomStack(3); // Stack is Empty [] |
| 36 | +customStack.push(1); // stack becomes [1] |
| 37 | +customStack.push(2); // stack becomes [1, 2] |
| 38 | +customStack.pop(); // return 2 --> Return top of the stack 2, stack becomes [1] |
| 39 | +customStack.push(2); // stack becomes [1, 2] |
| 40 | +customStack.push(3); // stack becomes [1, 2, 3] |
| 41 | +customStack.push(4); // stack still [1, 2, 3], Don't add another elements as size is 4 |
| 42 | +customStack.increment(5, 100); // stack becomes [101, 102, 103] |
| 43 | +customStack.increment(2, 100); // stack becomes [201, 202, 103] |
| 44 | +customStack.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202] |
| 45 | +customStack.pop(); // return 202 --> Return top of the stack 102, stack becomes [201] |
| 46 | +customStack.pop(); // return 201 --> Return top of the stack 101, stack becomes [] |
| 47 | +customStack.pop(); // return -1 --> Stack is empty return -1. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong>Constraints:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>1 <= maxSize <= 1000</code></li> |
| 55 | + <li><code>1 <= x <= 1000</code></li> |
| 56 | + <li><code>1 <= k <= 1000</code></li> |
| 57 | + <li><code>0 <= val <= 100</code></li> |
| 58 | + <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> |
| 59 | +</ul> |
| 60 | + |
| 61 | +### Related Topics |
| 62 | + [[Stack](../../tag/stack/README.md)] |
| 63 | + [[Design](../../tag/design/README.md)] |
| 64 | + |
| 65 | +### Hints |
| 66 | +<details> |
| 67 | +<summary>Hint 1</summary> |
| 68 | +Use an array to represent the stack. Push will add new integer to the array. Pop removes the last element in the array and increment will add val to the first k elements of the array. |
| 69 | +</details> |
| 70 | + |
| 71 | +<details> |
| 72 | +<summary>Hint 2</summary> |
| 73 | +This solution run in O(1) per push and pop and O(k) per increment. |
| 74 | +</details> |
0 commit comments