Skip to content

Instantly share code, notes, and snippets.

@thmain
Created February 24, 2016 03:41
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 thmain/958f49d6b9dee14f130f to your computer and use it in GitHub Desktop.
Save thmain/958f49d6b9dee14f130f to your computer and use it in GitHub Desktop.
import java.util.Stack;
public class TrackMaxInStack {
// objective here is to keep track of maximum value in a stack of integers
// create another another Stack which will keep track of maximum
Stack<Integer> main = new Stack<>();
Stack<Integer> track = new Stack<>();
public void insert(int x) {
if (main.isEmpty()) { // if stack is empty, insert the number in both
// stacks
main.add(x);
track.add(x);
} else {
// check if number in Stack(track) is bigger than x
// which ever is bigger, insert it into Stack
int a = track.peek();
track.add(Math.max(a, x));
main.add(x); // insert it into main stack.
}
}
public int remove() {
if (!main.isEmpty()) { // pop the top elements
track.pop();
return main.pop();
}
return 0;
}
public int getMax() {
return track.peek();
}
public static void main(String[] args) {
TrackMaxInStack i = new TrackMaxInStack();
i.insert(4);
i.insert(2);
i.insert(14);
i.insert(1);
i.insert(18);
System.out.println("Max Element is " + i.getMax());
System.out.println("Removing Element " + i.remove());
System.out.println("Max Element is " + i.getMax());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment