You have an empty sequence, and you will be given N queries. Each query is one of these three types:
1 x -Push the element x into the stack.
2 -Delete the element present at the top of the stack.
3 -Print the maximum element in the stack.
import java.util.*;
public class Stackers {
public static Stack<Integer> stack;
public static String output = "";
public static void main(String[] args) {
stack = new Stack<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0 ; i < n + 1; i++){
String op = sc.nextLine();
performOperation(op);
}
sc.close();
if(!output.isEmpty()){
System.out.println(output);
}
}
public static void performOperation(String op) {
if (op.startsWith("1")){
String remains = op.split(" ")[1];
int rem = Integer.valueOf(remains);
stack.push(rem);
}else if(op.equalsIgnoreCase("2")){
stack.pop();
}else if(op.equals("3")){
int max = (int) Collections.max(stack);
output += (max + "\n");
}
}
}
You have an empty sequence, and you will be given N queries. Each query is one of these three types:
1 x -Push the element x into the stack.
2 -Delete the element present at the top of the stack.
3 -Print the maximum element in the stack.
import java.util.*;
public class Stackers {
public static Stack<Integer> stack;
public static String output = "";
public static void main(String[] args) {
stack = new Stack<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0 ; i < n + 1; i++){
String op = sc.nextLine();
performOperation(op);
}
sc.close();
if(!output.isEmpty()){
System.out.println(output);
}
}
public static void performOperation(String op) {
if (op.startsWith("1")){
String remains = op.split(" ")[1];
int rem = Integer.valueOf(remains);
stack.push(rem);
}else if(op.equalsIgnoreCase("2")){
stack.pop();
}else if(op.equals("3")){
int max = (int) Collections.max(stack);
output += (max + "\n");
}
}
}
You have an empty sequence, and you will be given N queries. Each query is one of these three types:
1 x -Push the element x into the stack.
2 -Delete the element present at the top of the stack.
3 -Print the maximum element in the stack.
import java.util.*;
public class Stackers {
public static Stack<Integer> stack;
public static String output = "";
public static void main(String[] args) {
stack = new Stack<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0 ; i < n + 1; i++){
String op = sc.nextLine();
performOperation(op);
}
sc.close();
if(!output.isEmpty()){
System.out.println(output);
}
}
public static void performOperation(String op) {
if (op.startsWith("1")){
String remains = op.split(" ")[1];
int rem = Integer.valueOf(remains);
stack.push(rem);
}else if(op.equalsIgnoreCase("2")){
stack.pop();
}else if(op.equals("3")){
int max = (int) Collections.max(stack);
output += (max + "\n");
}
}
}
Finds Finding the Maximummaximum element of a Stack
A little bit about the problem
You have an empty sequence, and you will be given N queries. Each query is one of these three types:
1 x -Push the element x into the stack.
2 -Delete the element present at the top of the stack.
3 -Print the maximum element in the stack.
And my code is here - which passes the first 12 test cases.
And my code is here - which passes the first 12 test cases.
A little bit about the problem
You have an empty sequence, and you will be given N queries. Each query is one of these three types:
1 x -Push the element x into the stack.
2 -Delete the element present at the top of the stack.
3 -Print the maximum element in the stack.
And my code is here - which passes the first 12 test cases.