-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxStack.java
More file actions
40 lines (37 loc) · 1.08 KB
/
MaxStack.java
File metadata and controls
40 lines (37 loc) · 1.08 KB
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
class Result {
static int val;
public static List<Integer> getMax(List<String> operations) {
Stack<Integer> stack=new Stack<>();
//Stack<Integer> mstack=new Stack<>();
List<Integer> arr=new ArrayList<>();
for(int i=0;i<operations.size();i++){
String[] p=operations.get(i).split(" ");
int op=Integer.parseInt(p[0]);
int num=0;
if(op==1){
num=Integer.parseInt(p[1]);
if(stack.isEmpty()){
stack.push(num);
val=num;
}
else if(num>val){
stack.push((2*num)-val);
val=num;
}
else{
stack.push(num);
}
}
else if(op==2){
if(stack.peek()>val){
val=(2*val)-stack.pop();
}
else stack.pop();
}
else if(op==3){
arr.add(val);
}
}
return arr;
}
}