public static List<int> Improved(int[] arr)
{
var list = new List<int>();
var stack = new Stack<int[]>();
for (int i = 0; i < arr.Length; i++)
{
while (stack.Count > 0 && stack.Peek()[0] <= arr[i])
stack.Pop();
if (i == 0)
list.Add(i + 1);
else
{
if (stack.Count == 0)
list.Add(i + 1);
else
list.Add(i - stack.Peek()[1]);
}
stack.Push(new int[] { arr[i], i });
}
return list;
}
public static List<int> StockSpan(int[] arr)
{
var list = new List<int>();
var stack = new Stack<Pair>();
for (int i = 0; i < arr.Length; i++)
{
while (stack.Count > 0 && stack.Peek().Value <= arr[i])
stack.Pop();
if (i==0)
list.Add(i+1);
else
{
if(stack.Count ==0)
list.Add(i + 1);
else
list.Add(i - stack.Peek().Index);
}
stack.Push(new Pair(arr[i], i));
}
return list;
}
public class Pair
{
public int Value;
public int Index;
public Pair(int Value, int Index)
{
this.Index = Index;
this.Value = Value;
}
}