Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

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;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: run in new thread C# 
Csharp :: C# Blocks with statements 
Csharp :: go right unity 
Csharp :: unity dropdown 
Csharp :: c# minimise form 
Csharp :: monegame deltatime 
Csharp :: how to query items with any id in a list of ids linq c# 
Csharp :: docker Test a Connection using Curl 
Csharp :: unity draw waypoins path 
Csharp :: translate int to string with x 0 before c# 
Csharp :: C# ValidationAttribute required when 
Csharp :: flat view player movement script 
Csharp :: blazor clientside cookies 
Csharp :: how to check if an integer is in array c# 
Csharp :: Getting the text from a drop-down box 
Csharp :: unity collapse hierarchy script 
Csharp :: .net mvc foreach index 
Csharp :: discord embeds how to separate inline fields 
Csharp :: unity change fixed timestep in code 
Csharp :: example of List c# 
Csharp :: c# if statements 
Csharp :: if statement in razor using "?" and ":" 
Csharp :: usermanager find based on role 
Csharp :: c# insert backslash in string 
Csharp :: unity camera.main.screentoworldpoint(input.mouseposition) not working 
Csharp :: convert json date to datetime c# 
Csharp :: c# HttpResponseMessage postResponse = client.PostAsync 
Csharp :: change a positive number to negative or a negative number to positive 
Csharp :: assert throw 
Csharp :: c# sequential struct with fixed array size 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =