Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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");
- }
- }
- }
- public class ChallengeValue {
- public int max;
- public int value;
- }
- import java.util.*;
- public class Stackers {
- public static Stack<ChallengeValue> 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);
- ChallengeValue newVal = new ChallengeValue();
- newVal.value = rem;
- ChallengeValue last = stack.peek();
- newVal.max = last.max > newVal.value ? last.max : newVal.max;
- stack.push(newVal);
- } else if(op.equalsIgnoreCase("2")) {
- stack.pop();
- } else if(op.equals("3")) {
- int max = stack.peek().max;
- output += (max + "n");
- }
- }
- }
- public class Stacker {
- private final Deque<Integer> stack = new ArrayDeque<>();
- public Optional<Integer> processOperation(String op) {
- char opc = op.charAt(0);
- switch(opc) {
- case '1':
- int previous = stack.isEmpty() ? Integer.MIN_VALUE : stack.peek();
- int max = Math.max(previous, Integer.parseInt(op.substring(1).trim()));
- stack.push(max);
- return Optional.empty();
- case '2':
- if (stack.isEmpty()) {
- throw new IllegalStateException("Cannot pop empty stack");
- }
- stack.pop();
- return Optional.empty();
- case '3':
- if (stack.isEmpty()) {
- throw new IllegalStateException("Cannot max empty stack");
- }
- return Optional.of(stack.peek());
- default:
- throw new IllegalStateException("Unknown operation:" + op);
- }
- }
- }
- Stacker stacker = new Stacker();
- String report = commands.map(stacker::processOperation)
- .filter(Optional::isPresent)
- .map(opt -> String.valueOf(opt.get()))
- .collect(Collectors.joining("n"));
- scanner.useDelimiter("\s*\n");
- int count = Integer.parseInt(scanner.next());
- // convert STDIN lines to a stream
- Stream<String> commands = StreamSupport.stream(Spliterators.spliterator(scanner, count, Spliterator.ORDERED), false);
- import java.io.StringReader;
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Optional;
- import java.util.Scanner;
- import java.util.Spliterator;
- import java.util.Spliterators;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- import java.util.stream.StreamSupport;
- public class Stacker {
- private final Deque<Integer> stack = new ArrayDeque<>();
- public Optional<Integer> processOperation(String op) {
- char opc = op.charAt(0);
- switch(opc) {
- case '1':
- int previous = stack.isEmpty() ? Integer.MIN_VALUE : stack.peek();
- int max = Math.max(previous, Integer.parseInt(op.substring(1).trim()));
- stack.push(max);
- return Optional.empty();
- case '2':
- if (stack.isEmpty()) {
- throw new IllegalStateException("Cannot pop empty stack");
- }
- stack.pop();
- return Optional.empty();
- case '3':
- if (stack.isEmpty()) {
- throw new IllegalStateException("Cannot max empty stack");
- }
- return Optional.of(stack.peek());
- default:
- throw new IllegalStateException("Unknown operation:" + op);
- }
- }
- public static void main(String[] args) {
- String testdata = "10n" +
- "1 97n" +
- "2n" +
- "1 20n" +
- "2n" +
- "1 26n" +
- "1 20n" +
- "2n" +
- "3n" +
- "1 91n" +
- "3";
- try (Scanner scanner = new Scanner(new StringReader(testdata))) {
- scanner.useDelimiter("\s*\n");
- int count = Integer.parseInt(scanner.next());
- // convert STDIN lines to a stream
- Stream<String> commands = StreamSupport.stream(Spliterators.spliterator(scanner, count, Spliterator.ORDERED), false);
- // process the operations:
- Stacker stacker = new Stacker();
- String report = commands.map(stacker::processOperation)
- .filter(Optional::isPresent)
- .map(opt -> String.valueOf(opt.get()))
- .collect(Collectors.joining("n"));
- System.out.println(report);
- }
- }
- }
- public class Solution {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Stack<Integer> stacky = new Stack<Integer>();
- int N = scan.nextInt();
- int ch;
- int x,ele=0;
- while(N-- >0){
- ch = scan.nextInt();
- if(ch==1){x = scan.nextInt();
- if(stacky.isEmpty()){
- stacky.push(x);
- }
- else{
- int y = stacky.peek();
- stacky.push(Math.max(x, y));
- }
- }
- if(ch==2){
- if(!stacky.isEmpty())
- stacky.pop();
- }
- if(ch==3){
- System.out.println(stacky.peek());
- }
- }}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement