Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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");
 
 }
 
 }
}
edited tags; edited title
Link
Der Kommissar
  • 20.3k
  • 4
  • 70
  • 158

Finds Finding the Maximummaximum element of a Stack

Tweeted twitter.com/StackCodeReview/status/839493848346816512
added 303 characters in body
Source Link

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.

Title and tags...
Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419
Loading
Source Link
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /