Created
May 30, 2017 18:58
-
-
Save Tangrila-BG/49c0beebfb957e865aa535111b4b07e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Csharp.Advanced.StacksAndQueues | |
{ | |
public partial class StacksAndQueues | |
{ | |
public static class _11PoisonousPlants | |
{ | |
public static void Solution() | |
{ | |
var plantsAmount = int.Parse(Console.ReadLine()); | |
var plants = Console.ReadLine() | |
.Split() | |
.Select(int.Parse) | |
.ToArray(); | |
var days = new int[plantsAmount]; | |
var proximityStack = new Stack<int>(); | |
proximityStack.Push(0); | |
for (int i = 1; i < plantsAmount; i++) | |
{ | |
int maxDays = 0; | |
while (proximityStack.Count > 0 | |
&& plants[proximityStack.Peek()] >= plants[i]) | |
{ | |
maxDays = Math.Max(maxDays, days[proximityStack.Pop()]); | |
} | |
if (proximityStack.Count > 0) | |
days[i] = maxDays + 1; | |
proximityStack.Push(i); | |
} | |
Console.WriteLine(days.Max()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment