1381. Design a Stack With Increment Operation
Difficulty: Medium
Topics: Array, Stack, Design
Design a stack that supports increment operations on its elements.
Implement the CustomStack class:
Example 1:
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
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 2, 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], Do not add another elements 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 103, stack becomes [201, 202] stk.pop(); // return 202 --> Return top of the stack 202, stack becomes [201] stk.pop(); // return 201 --> Return top of the stack 201, stack becomes [] stk.pop(); // return -1 --> Stack is empty return -1.
Constraints:
Hint:
Solution:
We can follow a typical stack implementation but with an additional method that allows incrementing the bottom k elements by a given value. The increment operation will iterate through the first k elements of the stack and add the value to each.
We'll implement this stack in PHP 5.6, using an array to represent the stack. The core operations are:
Let's implement this solution in PHP: 1381. Design a Stack With Increment Operation
<?php class CustomStack { /** * @var array */ private $stack; /** * @var int */ private $maxSize; /** * Constructor to initialize the stack with a given maxSize * * @param Integer $maxSize */ function __construct($maxSize) { ... ... ... /** * go to ./solution.php */ } /** * Push an element to the stack if it has not reached the maxSize * * @param Integer $x * @return NULL */ function push($x) { ... ... ... /** * go to ./solution.php */ } /** * Pop the top element from the stack and return it, return -1 if the stack is empty * * @return Integer */ function pop() { ... ... ... /** * go to ./solution.php */ } /** * Increment the bottom k elements of the stack by val * * @param Integer $k * @param Integer $val * @return NULL */ function increment($k, $val) { ... ... ... /** * go to ./solution.php */ } } /** * Your CustomStack object will be instantiated and called as such: * $obj = CustomStack($maxSize); * $obj->push($x); * $ret_2 = $obj->pop(); * $obj->increment($k, $val); */ // Example usage $customStack = new CustomStack(3); // Stack is Empty [] $customStack->push(1); // stack becomes [1] $customStack->push(2); // stack becomes [1, 2] echo $customStack->pop() . "\n"; // return 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], maxSize is 3 $customStack->increment(5, 100); // stack becomes [101, 102, 103] $customStack->increment(2, 100); // stack becomes [201, 202, 103] echo $customStack->pop() . "\n"; // return 103, stack becomes [201, 202] echo $customStack->pop() . "\n"; // return 202, stack becomes [201] echo $customStack->pop() . "\n"; // return 201, stack becomes [] echo $customStack->pop() . "\n"; // return -1, stack is empty ?> <h3> Explanation: </h3> <ol> <li> <p><strong>push($x)</strong>:</p> <ul> <li>We use array_push to add elements to the stack. We check if the current size of the stack is less than maxSize. If it is, we push the new element.</li> </ul> </li> <li> <p><strong>pop()</strong>:</p> <ul> <li>We check if the stack is empty using empty($this->stack). If it's not empty, we pop the top element using array_pop and return it. If it's empty, we return -1.</li> </ul> </li> <li> <p><strong>increment($k, $val)</strong>:</p> <ul> <li>We calculate the minimum of k and the current stack size to determine how many elements to increment. We then loop over these elements, adding val to each.</li> </ul> </li> </ol> <h3> Example Execution: </h3> <p>For the input operations:<br> </p> <pre class="brush:php;toolbar:false">["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
The output will be:
[null, null, null, 2, null, null, null, null, null, 103, 202, 201, -1]
This output is based on the following:
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Design a Stack With Increment Operation. For more information, please follow other related articles on the PHP Chinese website!