Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

stock span problem c#

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