Advertisement
Guest User

16. **Poisonous Plants

a guest
May 22nd, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 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.Arrays;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) throws IOException {
  10.  
  11. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12. int initialPlantsCount = Integer.parseInt(reader.readLine());
  13.  
  14. int[] plants = Arrays.stream(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  15.  
  16. short[] days = new short[initialPlantsCount];
  17.  
  18. ArrayDeque<Integer> temp = new ArrayDeque<>();
  19. temp.push(0);
  20. short maxDay=0;
  21.  
  22. for (int i = 1; i < initialPlantsCount; i++) {
  23.  
  24. short maxDays = 0;
  25. while (temp.size() > 0 && plants[temp.peek()] >= plants[i]) {
  26. maxDays = (short) Math.max(maxDays, days[temp.pop()]);
  27. }
  28. if (temp.size() > 0) {
  29.  
  30. days[i] = ++maxDays;
  31. if(days[i]>maxDay){
  32. maxDay=days[i];
  33. }
  34. }
  35. temp.push(i);
  36. }
  37. System.out.println(maxDay);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement