desislava_topuzakova

09. Poisonous Plants

Jan 12th, 2022
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package StacksAndQueues;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import java.util.Stack;
  7.  
  8. public class PoisonousPlants_10 {
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner scanner = new Scanner(System.in);
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.         int[] plants = Arrays.stream(scanner.nextLine().split(" "))
  14.                 .mapToInt(Integer::parseInt)
  15.                 .toArray();
  16.  
  17.  
  18.         ArrayDeque<Integer> prevPlants = new ArrayDeque<>();
  19.         int[] days = new int[plants.length];
  20.         prevPlants.push(0);
  21.         for (int x = 1; x < plants.length; x++) {
  22.             int maxDays = 0;
  23.             while (prevPlants.size() > 0 && plants[prevPlants.peek()] >= plants[x]) {
  24.  
  25.                 maxDays = Integer.max(days[prevPlants.pop()], maxDays);
  26.             }
  27.  
  28.             if (prevPlants.size() > 0) {
  29.                 days[x] = maxDays + 1;
  30.             }
  31.  
  32.             prevPlants.push(x);
  33.         }
  34.  
  35.         System.out.printf("%d", Arrays.stream(days).max().getAsInt());
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment