-
-
Save JamesJi9277/ee5393fed97f741d9030 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
//using additional stack to keep track of every minimum value | |
public class Solution{ | |
Stack<Integer> s2 = new Stack<Integer>(); | |
public void push(int value){ | |
if (value <= min()) | |
s2.push(value); | |
s1.push(value); | |
} | |
public int pop(){ | |
int value = s1.pop(); | |
if(value == min()) | |
s2.pop(); | |
return value; | |
} | |
public int min(){ | |
if(s2.isEmpty()) | |
return -1; | |
else | |
return s2.pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
u r so smart to use min() directly. zan!