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;
- import java.util.Arrays;
- import java.util.stream.Collectors;
- public class PoisonousPlants {
- private static void reverse(ArrayDeque<Integer> queue)
- {
- ArrayDeque<Integer> s = new ArrayDeque(); //create a stack
- //while the queue is not empty
- while(!queue.isEmpty())
- { //add the elements of the queue onto a stack
- s.push(queue.poll());
- }
- //while the stack is not empty
- while(!s.isEmpty())
- { //add the elements in the stack back to the queue
- queue.offer(s.pop());
- }
- }
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int numberOfPlants = Integer.parseInt(reader.readLine());
- ArrayDeque<Integer> pesticidePerPlant = Arrays.stream(reader.readLine()
- .split("\\s+"))
- .map(Integer::parseInt)
- .collect(Collectors.toCollection(ArrayDeque::new));
- reverse(pesticidePerPlant);
- boolean areDying = true;
- int days = 0;
- while (areDying){
- int currentSize = pesticidePerPlant.size();
- for (int i = 0; i < currentSize - 1; i++) {
- int currentIndex = pesticidePerPlant.peek();
- if(pesticidePerPlant.poll() <= pesticidePerPlant.peek()){
- pesticidePerPlant.offer(currentIndex);
- }
- }
- if(pesticidePerPlant.size() > 2)pesticidePerPlant.offer(pesticidePerPlant.poll());
- if(currentSize - pesticidePerPlant.size() == 0) {
- areDying = false;
- }else {
- days++;
- }
- }
- System.out.println(days);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment