-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindSpanUsingStack
45 lines (38 loc) · 1.12 KB
/
FindSpanUsingStack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
namespace ConsoleApplication32
{
internal static class Program
{
public static void Main(string[] args)
{
var array = new int[] {100,60,70,65,80,85};
var length = array.Length;
var t= GetMaximumArea(array, length);
foreach (var variable in t)
{
Console.WriteLine(variable);
}
}
private static IEnumerable<int> GetMaximumArea(IReadOnlyList<int> array, int length)
{
var stack = new Stack<int>();
var span = new int[length];
span[0] = 1;
stack.Push(0);
for (var i = 1; i <length; i++)
{
while (stack.Count != 0 && array[i] > array[stack.Peek()])
stack.Pop();
if (stack.Count == 0)
span[i] = i + 1;
else
{
span[i] = i - stack.Peek();
}
stack.Push(i);
}
return span;
}
}
}