-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy path1381.java
More file actions
38 lines (30 loc) · 725 Bytes
/
1381.java
File metadata and controls
38 lines (30 loc) · 725 Bytes
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
class CustomStack {
private int c;
private Stack<Integer> s;
private int[] inc;
public CustomStack(int maxSize) {
c = maxSize;
s = new Stack();
inc = new int[c];
}
public void push(int x) {
if (s.size() < c) {
s.push(x);
}
}
public int pop() {
if (s.isEmpty()) return -1;
int i = s.size() - 1;
if (i > 0) {
inc[i - 1] += inc[i];
}
int res = s.pop() + inc[i];
inc[i] = 0;
return res;
}
public void increment(int k, int val) {
if (!s.isEmpty()) {
inc[Math.min(k, s.size()) - 1] += val;
}
}
}