Skip to content
Discussion options

You must be logged in to vote

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:

  1. push(x): Adds the element x to the top of the stack, if the stack has not reached its maxSize.
  2. pop(): Removes the top element of the stack and returns it. If the stack is empty, return -1.
  3. increment(k, val): Adds the value val to the first k elements in the stack, or to all the elements if the stack contains fewer than k elements.

Let's impleme…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 30, 2024
Maintainer Author

@kovatz
Comment options

kovatz Sep 30, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty hacktoberfest-accepted hacktoberfest accepted
2 participants