Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

stock span problem c# using class

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;
    }
}
 
PREVIOUS NEXT
Tagged: #stock #span #problem #class
ADD COMMENT
Topic
Name
2+2 =