Advertisement
nikeza

Poisonous Plants

Sep 23rd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayDeque;
  5. import java.util.Scanner;
  6.  
  7. public class stack1_Exercises_Poisonous_Plants {
  8.     public static void main(String[] args) throws IOException {
  9.         Scanner scanner = new Scanner(System.in);
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.         ArrayDeque<Integer> plants = new ArrayDeque<>();
  12.         for (int i = 0; i < n; i++) {
  13.             plants.push(scanner.nextInt());
  14.         }
  15.  
  16.         int days = 0;
  17.         while (true) {
  18.             int length = plants.size() - 1;
  19.  
  20.             for (int i = 0; i < length; i++) {
  21.                 int min = 0;
  22.                 min = plants.pop();
  23.                 if (min < plants.peek()) {
  24.                     plants.addLast(min);
  25.                 }
  26.             }
  27.  
  28.             if (plants.size() != length + 1) {
  29.                 days++;
  30.             } else {
  31.                 int result = days - 1;
  32.                 if (result < 0) {
  33.                     result = 0;
  34.                 } else if (result == 0) {
  35.                     result = 1;
  36.                 }
  37.  
  38.                 System.out.println(result);
  39.                 break;
  40.             }
  41.  
  42.  
  43.         }
  44.  
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement