Advertisement
Guest User

Poisonous Plants

a guest
May 24th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. /**
  7.  * Created by IntelliJ IDEA.
  8.  * User: LAPD
  9.  * Date: 22.5.2018 г.
  10.  * Time: 08:59 ч.
  11.  */
  12. public class _16PoisonousPlants {
  13.     public static void main(String[] args) throws IOException {
  14.        
  15.         // not working 100%
  16.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  17.  
  18.         int plantsCount = Integer.parseInt(bufferedReader.readLine());
  19.  
  20.         int[] plantGMO = Arrays
  21.                 .stream(bufferedReader.readLine()
  22.                         .split("\\s+"))
  23.                 .mapToInt(Integer::parseInt)
  24.                 .toArray();
  25.  
  26.         LinkedHashMap<Integer, Boolean> gardenAlive = new LinkedHashMap<>();
  27.  
  28.         for (int plant : plantGMO) {
  29.             gardenAlive.put(plant, true);
  30.         }
  31.  
  32.         boolean hasDied = true;
  33.         int daysCount = 0;
  34.  
  35.         while (hasDied) {
  36.             hasDied = false;
  37.  
  38.             int leftPlant = Integer.MAX_VALUE;
  39.  
  40.             for (Map.Entry<Integer, Boolean> plant : gardenAlive.entrySet()) {
  41.                 if (plant.getKey() > leftPlant) {
  42.                     plant.setValue(false);
  43.                     hasDied = true;
  44.                 }
  45.  
  46.                 leftPlant = plant.getKey();
  47.             }
  48.  
  49.             if (hasDied) {
  50.                 gardenAlive.values().removeIf(val -> !val);
  51.                 daysCount++;
  52.             }
  53.         }
  54.  
  55.         System.out.println(daysCount);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement