Skip to content

Instantly share code, notes, and snippets.

@jyhjuzi
Created July 2, 2014 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyhjuzi/60c4cad863717bff7e04 to your computer and use it in GitHub Desktop.
Save jyhjuzi/60c4cad863717bff7e04 to your computer and use it in GitHub Desktop.
import java.util.Stack;
public class Q3_2{
public static void main(String args[]){
StackWithMin s = new StackWithMin();
s.push(5);
s.push(4);
s.push(6);
s.push(7);
s.push(2);
s.push(1);
System.out.println(s.min.size());
s.pop();
System.out.println(s.min());
s.pop();
System.out.println(s.min());
}
}
class StackWithMin{
Stack<Integer> s = new Stack<Integer>();
Stack<Integer> min = new Stack<Integer>();
void push(int value){
s.push(value);
if(value<min()){
min.push(value);
}
}
int pop(){
if(min.peek()== s.peek())
min.pop();
return s.pop();
}
int peek(){
return s.peek();
}
boolean isEmpty(){
return s.isEmpty();
}
int min(){
if(min.isEmpty())
return Integer.MAX_VALUE;
else return min.peek();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment