-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.java
39 lines (35 loc) · 1.08 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class CustomStack {
private int maxSize;
private List<Integer> stack;
private List<Integer> inc;
public CustomStack(int maxSize) {
this.maxSize = maxSize;
this.stack = new ArrayList<>();
this.inc = new ArrayList<>();
}
public void push(int x) {
if (stack.size() < maxSize) {
stack.add(x);
inc.add(0); // Initialize increment for this element
}
}
public int pop() {
if (stack.isEmpty()) {
return -1;
}
int idx = stack.size() - 1;
int result = stack.get(idx) + inc.get(idx); // Apply any pending increments
if (idx > 0) {
inc.set(idx - 1, inc.get(idx - 1) + inc.get(idx)); // Propagate increment to the next element
}
stack.remove(idx);
inc.remove(idx);
return result;
}
public void increment(int k, int val) {
int limit = Math.min(k, stack.size()) - 1;
if (limit >= 0) {
inc.set(limit, inc.get(limit) + val); // Add increment to the bottom k-th element
}
}
}