Advertisement
Mirineo

10. Poisonous Plants

Jan 18th, 2021
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package StacksAndQueues.Exercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayDeque;
  7.  
  8.  
  9. public class PoisonousPlants {
  10.     public static void main(String[] args) throws IOException {
  11.  
  12.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13.         int numberOfPlants = Integer.parseInt(reader.readLine());
  14.         String[] pesticidePerPlant = reader.readLine().split("\\s+");
  15.  
  16.         int[] days = new int[numberOfPlants];
  17.         ArrayDeque<Integer> indices = new ArrayDeque<>();
  18.         indices.push(0);
  19.         for (int i = 1; i < numberOfPlants; i++) {
  20.             int maxDays = 0;
  21.             while (!indices.isEmpty()) {
  22.                 if (Integer.parseInt(pesticidePerPlant[indices.peek()]) >= Integer.parseInt(pesticidePerPlant[i])) {
  23.                     maxDays = Math.max(maxDays, days[indices.pop()]);
  24.                 } else {
  25.                     days[i] = maxDays + 1;
  26.                     break;
  27.                 }
  28.             }
  29.             indices.push(i);
  30.         }
  31.  
  32.         System.out.println(getMax(days));
  33.     }
  34.  
  35.     public static Integer getMax(int[] array) {
  36.         int max = Integer.MIN_VALUE;
  37.         for (int num : array) {
  38.             if (num > max) {
  39.                 max = num;
  40.             }
  41.         }
  42.         return max;
  43.     }
  44. }
  45.  
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement