Skip to content

Instantly share code, notes, and snippets.

@Tangrila-BG
Created May 30, 2017 18:58
Show Gist options
  • Save Tangrila-BG/49c0beebfb957e865aa535111b4b07e9 to your computer and use it in GitHub Desktop.
Save Tangrila-BG/49c0beebfb957e865aa535111b4b07e9 to your computer and use it in GitHub Desktop.
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