Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StacksAndQueues.Exercise;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayDeque;
- public class PoisonousPlants {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int numberOfPlants = Integer.parseInt(reader.readLine());
- String[] pesticidePerPlant = reader.readLine().split("\\s+");
- int[] days = new int[numberOfPlants];
- ArrayDeque<Integer> indices = new ArrayDeque<>();
- indices.push(0);
- for (int i = 1; i < numberOfPlants; i++) {
- int maxDays = 0;
- while (!indices.isEmpty()) {
- if (Integer.parseInt(pesticidePerPlant[indices.peek()]) >= Integer.parseInt(pesticidePerPlant[i])) {
- maxDays = Math.max(maxDays, days[indices.pop()]);
- } else {
- days[i] = maxDays + 1;
- break;
- }
- }
- indices.push(i);
- }
- System.out.println(getMax(days));
- }
- public static Integer getMax(int[] array) {
- int max = Integer.MIN_VALUE;
- for (int num : array) {
- if (num > max) {
- max = num;
- }
- }
- return max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement