Skip to content

Instantly share code, notes, and snippets.

@gregorykelleher
Last active November 13, 2015 17:44
Show Gist options
  • Save gregorykelleher/2a92ec8541ca70bfda6c to your computer and use it in GitHub Desktop.
Save gregorykelleher/2a92ec8541ca70bfda6c to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//System.out.println("Enter the size of the stack: ");
int size = scan.nextInt();
scan.nextLine();
Stack stack = new Stack();
int countSize = size;
int maxSoFar = 0;
while (countSize != 0) {
//System.out.println("Enter the command: ");
String str = scan.nextLine();
int length = str.length();
String NumCmd = "";
for (int i = 0; i < length; i++) {
Character character = str.charAt(i);
if (Character.isDigit(character)) {
NumCmd += character;
}
}
if(NumCmd == "") {
if (str.equals("PEEK")) {
stack.peek();
System.out.println(stack.peek());
}
if (str.contains("POP")) {
stack.pop();
System.out.printf("%s ", stack);
}
}
else {
int x = Integer.parseInt(NumCmd.valueOf(NumCmd));
if (maxSoFar < x) {
maxSoFar = x;
}
if (str.contains("PUSH")) {
stack.push(x);
countSize--;
}
}
}
System.out.println(maxSoFar);
}
}
@gregorykelleher
Copy link
Author

"countSize--" should not be included for POP and PEEK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment