Horje
stock span problem c# Code Example
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;
}
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;
    }
}




Csharp

Related
How do I remove all non alphanumeric characters from a string? Code Example How do I remove all non alphanumeric characters from a string? Code Example
avoid autocomplete input text asp.net Code Example avoid autocomplete input text asp.net Code Example
list.max c# Code Example list.max c# Code Example
c# filter non alphanumeric characters Code Example c# filter non alphanumeric characters Code Example
c# thread sleep Code Example c# thread sleep Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
12