Aliendreamer

plants c# advanced

Sep 19th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. namespace _11.Poisonous_Plants
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             int plantsCount = int.Parse(Console.ReadLine());
  12.             var plants = Console.ReadLine()
  13.                 .Trim()
  14.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  15.                 .Select(int.Parse)      
  16.                 .ToArray();
  17.            
  18.             var daysToDie = new int[plantsCount];
  19.             var plantsLeftSeq = new Stack<int>();
  20.  
  21.             plantsLeftSeq.Push(0);                
  22.  
  23.             for (int i = 1; i < plantsCount; i++)
  24.             {
  25.                 int maxDaysToDie = 0;
  26.  
  27.                 while (plantsLeftSeq.Count != 0 && plants[plantsLeftSeq.Peek()] >= plants[i])
  28.                 {
  29.                     maxDaysToDie = Math.Max(maxDaysToDie, daysToDie[plantsLeftSeq.Pop()]);
  30.                 }
  31.  
  32.                 if (plantsLeftSeq.Count != 0)
  33.                 {
  34.                     daysToDie[i] = maxDaysToDie + 1;
  35.                 }
  36.                 plantsLeftSeq.Push(i);
  37.             }      
  38.             Console.WriteLine(daysToDie.Max());
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment