java.lang.Object
g1301_1400.s1381_design_a_stack_with_increment_operation.CustomStack

public class CustomStack extends Object
1381 - Design a Stack With Increment Operation.<p>Medium</p> <p>Design a stack which supports the following operations.</p> <p>Implement the <code>CustomStack</code> class:</p> <ul> <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> <li><code>void push(int x)</code> Adds <code>x</code> to the top of the stack if the stack hasn&rsquo;t reached the <code>maxSize</code>.</li> <li><code>int pop()</code> Pops and returns the top of stack or <strong>-1</strong> if the stack is empty.</li> <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> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <p>[&ldquo;CustomStack&rdquo;,&ldquo;push&rdquo;,&ldquo;push&rdquo;,&ldquo;pop&rdquo;,&ldquo;push&rdquo;,&ldquo;push&rdquo;,&ldquo;push&rdquo;,&ldquo;increment&rdquo;,&ldquo;increment&rdquo;,&ldquo;pop&rdquo;,&ldquo;pop&rdquo;,&ldquo;pop&rdquo;,&ldquo;pop&rdquo;]</p> <p>[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]</p> <p><strong>Output:</strong> [null,null,null,2,null,null,null,null,null,103,202,201,-1]</p> <p><strong>Explanation:</strong></p> <pre><code> CustomStack customStack = new CustomStack(3); // Stack is Empty [] customStack.push(1); // stack becomes [1] customStack.push(2); // stack becomes [1, 2] customStack.pop(); // return 2 --> Return top of the stack 2, stack becomes [1] customStack.push(2); // stack becomes [1, 2] customStack.push(3); // stack becomes [1, 2, 3] customStack.push(4); // stack still [1, 2, 3], Don't add another elements as size is 4 customStack.increment(5, 100); // stack becomes [101, 102, 103] customStack.increment(2, 100); // stack becomes [201, 202, 103] customStack.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202] customStack.pop(); // return 202 --> Return top of the stack 102, stack becomes [201] customStack.pop(); // return 201 --> Return top of the stack 101, stack becomes [] customStack.pop(); // return -1 --> Stack is empty return -1. </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= maxSize <= 1000</code></li> <li><code>1 <= x <= 1000</code></li> <li><code>1 <= k <= 1000</code></li> <li><code>0 <= val <= 100</code></li> <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> </ul>
  • Constructor Details

    • CustomStack

      public CustomStack(int maxSize)
  • Method Details

    • push

      public void push(int x)
    • pop

      public int pop()
    • increment

      public void increment(int k, int val)