> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/design/1381.-design-a-stack-with-increment-operation.md).

# 1381. Design a Stack With Increment Operation

## how do we avoid incrementing k times?

use partial increment array to save increment number!

T: all O(1)

S: O(maxSize)

```java
class CustomStack {

    private int[] increment;
    private LinkedList<Integer> list;
    private int maxSize;
    public CustomStack(int maxSize) {
        this.increment = new int[maxSize];
        this.list = new LinkedList();
        this.maxSize = maxSize;
    }
    
    public void push(int x) {
        if (list.size() >= maxSize) {
            return;
        }
        list.add(x);
    }
    
    public int pop() {
        if (list.isEmpty()) {
            return -1;
        }
        int curIndex = list.size()-1;
        int result = list.pollLast() + increment[curIndex];
        if (curIndex > 0) {
            increment[curIndex-1] += increment[curIndex];
        }
        increment[curIndex] = 0;
        return result;
    }
    
    public void increment(int k, int val) {
        if (list.isEmpty()) {
            return;
        }
        int index = Math.min(k, list.size()) - 1;
        increment[index] += val;
    }
}

/**

inc bottom k elements

O(1) for push & pop
O(1) for increment -> only apply increment for the pop number
but remember to copy this increment value to [index-1] (previous k-1)

 * Your CustomStack object will be instantiated and called as such:
 * CustomStack obj = new CustomStack(maxSize);
 * obj.push(x);
 * int param_2 = obj.pop();
 * obj.increment(k,val);
 */
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/design/1381.-design-a-stack-with-increment-operation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
