Advertisement
Guest User

Poisonous Plants

a guest
Jan 19th, 2020
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 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. public class PoisonousPlants {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
  9.  
  10.         Map<Integer, Integer> indexPesticidesCount = new LinkedHashMap<>(Integer.parseInt(consoleReader.readLine()));
  11.         String[] inputTokens = consoleReader.readLine().split(" ");
  12.  
  13.         consoleReader.close();
  14.  
  15.         for (int i = 0; i < inputTokens.length; i++) {
  16.             indexPesticidesCount.put(i, Integer.parseInt(inputTokens[i]));
  17.         }
  18.  
  19.         int daysCounter = 0;
  20.         List<Integer> indicesOfPlantsToBeRemoved = new ArrayList<>();
  21.  
  22.         while (true) {
  23.             Map.Entry<Integer, Integer> previousEntry = null;
  24.  
  25.             for (Map.Entry<Integer, Integer> currentEntry : indexPesticidesCount.entrySet()) {
  26.                 if (previousEntry == null) {
  27.                     previousEntry = currentEntry;
  28.                     continue;
  29.                 }
  30.  
  31.                 if (previousEntry.getValue() < currentEntry.getValue()) { // if current plant has more pesticides than previous one
  32.                     indicesOfPlantsToBeRemoved.add(currentEntry.getKey());
  33.                 }
  34.  
  35.                 previousEntry = currentEntry;
  36.             }
  37.  
  38.             if (indicesOfPlantsToBeRemoved.isEmpty()) {
  39.                 break;
  40.             }
  41.  
  42.             daysCounter++; //increment now since some plants will die
  43.  
  44.             for (int i = 0; i < indicesOfPlantsToBeRemoved.size(); i++) {
  45.                 indexPesticidesCount.remove(indicesOfPlantsToBeRemoved.get(i));
  46.             }
  47.  
  48.             indicesOfPlantsToBeRemoved.clear();
  49.         }
  50.  
  51.         System.out.println(daysCounter);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement