Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package StacksAndQueues1;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.Stack;
- public class PoisonousPlants {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int number = Integer.parseInt(scanner.nextLine());
- int[] plants = Arrays
- .stream(scanner.nextLine().split(" "))
- .mapToInt(Integer::parseInt)
- .toArray();
- int[] days = new int[number];
- Stack<Integer> stack = new Stack<>();
- stack.push(0);
- int max = -1;
- for (int i = 1; i < plants.length; i++) {
- int allDays = 0;
- while (stack.size() > 0 && plants[stack.peek()] >= plants[i]) {
- allDays = Math.max(days[stack.pop()], allDays);
- }
- if (stack.size() > 0) {
- days[i] = allDays + 1;
- if (allDays + 1 > max) {
- max = allDays + 1;
- }
- }
- stack.push(i);
- }
- System.out.println(max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment