Created
July 2, 2014 22:48
-
-
Save jyhjuzi/60c4cad863717bff7e04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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