Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 input = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(input.readLine());
- String[] plants = input.readLine().split(" ");
- ArrayDeque<Integer> indexes = new ArrayDeque<>();
- indexes.push(0);
- int[] daysCount = new int[n];
- for (int a = 0; a < n; a++) {
- int maxDays = 0;
- while (indexes.size() > 0 && Integer.parseInt(plants[indexes.peek()]) >= Integer.parseInt(plants[a])) {
- maxDays = Math.max(maxDays, daysCount[indexes.pop()]);
- }
- if (indexes.size() > 0) {
- daysCount[a] = maxDays + 1;
- }
- indexes.push(a);
- }
- System.out.println(maxCalc(daysCount));
- }
- private static int maxCalc(int[] days) {
- int max = Integer.MIN_VALUE;
- for (int currentDay : days) {
- if (currentDay > max) {
- max = currentDay;
- }
- }
- return max;
- }
- }
Add Comment
Please, Sign In to add comment