-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.java
More file actions
executable file
·52 lines (39 loc) · 927 Bytes
/
MinStack.java
File metadata and controls
executable file
·52 lines (39 loc) · 927 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
46
47
48
49
50
51
52
import java.util.*;
public class MinStack {
Stack<Integer> stack = new Stack();
Stack<Integer> minStack = new Stack();
public void push(int data) {
int min = data;
if(!minStack.isEmpty()){
if(min>minStack.peek())
{
min=minStack.peek();
}
}
stack.push(data);
minStack.push(min);
}
public int pop() {
minStack.pop();
return stack.pop();
}
public int getMin() {
return minStack.pop();
}
public static void main(String[] args) {
int i;
MinStack s=new MinStack();
if(args.length>0)
{
for (i=0;i<args.length;i++ ) {
s.push(Integer.parseInt(args[i]));
}
System.out.println(s.getMin());
}else{
System.out.println("please pass the values");
}
// s.push(Integer.parseInt(args[1]));
// s.push(Integer.parseInt(args[2]));
// s.push(Integer.parseInt(args[3]));
}
}