-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetCode_1381.java
More file actions
45 lines (36 loc) · 952 Bytes
/
Copy pathleetCode_1381.java
File metadata and controls
45 lines (36 loc) · 952 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
39
40
41
42
43
44
45
class CustomStack {
int maxsize ;
Stack<Integer> stack = new Stack<>();
int arr[];
public CustomStack(int maxSize) {
maxsize = maxSize;
arr = new int[maxSize];
}
public void push(int x) {
if(stack.size() < maxsize){
stack.push(x);
}
}
public int pop() {
int i = stack.size() - 1;
if(i < 0) return -1;
if(i > 0 ) arr[i-1] += arr[i];
int res = stack.pop() + arr[i];
arr[i]=0;
return res;
}
public void increment(int k, int val) {
if(stack.isEmpty()){
return;
}
int safe = Math.min(k, stack.size()) -1 ;
arr[safe] += val;
}
}
/**
* 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);
*/