Skip to content

Commit 93de734

Browse files
authored
Stacks : Stock span problem
Stacks : Stock span problem
1 parent 849deeb commit 93de734

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

FindSpanUsingStack

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

0 commit comments

Comments
 (0)