1381. Design a Stack With Increment Operation #631
-
Topics: Design a stack that supports increment operations on its elements. Implement the
Example 1:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We can follow a typical stack implementation but with an additional method that allows incrementing the bottom 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) {
$this->stack = array();
$this->maxSize = $maxSize;
}
/**
* Push an element to the stack if it has not reached the maxSize
*
* @param Integer $x
* @return NULL
*/
function push($x) {
if (count($this->stack) < $this->maxSize) {
array_push($this->stack, $x);
}
}
/**
* Pop the top element from the stack and return it, return -1 if the stack is empty
*
* @return Integer
*/
function pop() {
if (empty($this->stack)) {
return -1;
}
return array_pop($this->stack);
}
/**
* Increment the bottom k elements of the stack by val
*
* @param Integer $k
* @param Integer $val
* @return NULL
*/
function increment($k, $val) {
// We will only iterate through the first `k` elements or the full stack if it's smaller than `k`
$limit = min($k, count($this->stack));
for ($i = 0; $i < $limit; $i++) {
$this->stack[$i] += $val;
}
}
}
/**
* 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
?> Explanation:
Example Execution:For the input operations: ["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:
This output is based on the following:
|
Beta Was this translation helpful? Give feedback.
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 firstk
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:
x
to the top of the stack, if the stack has not reached itsmaxSize
.-1
.val
to the firstk
elements in the stack, or to all the elements if the stack contains fewer thank
elements.Let's impleme…