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 :: public static void Load 
Csharp :: jitter on collision for 2 rigid bodies 
Csharp :: making beep voice in c# 
Csharp :: unity particle system playing at the wrong location 
Csharp :: unity rotate vector around point 
Csharp :: c# get vector2 distance 
Csharp :: c# random choice 
Csharp :: how to cast list to observablecollection c# 
Csharp :: unity c# class addition syntax 
Csharp :: c# check if is float 
Csharp :: validate base64 string c# 
Csharp :: action being performed on this control is being called from the wrong thread c# 
Csharp :: c# winforms textbox readonly 
Csharp :: how to unfreeze a rotation in a collider unity 2d 
Csharp :: c# random string 
Csharp :: get web config key value in c# razor view 
Csharp :: string from byte array c# 
Csharp :: how to unescape  in a string java 
Csharp :: unity set text value 
Csharp :: get desktop path c# 
Csharp :: How to add a label programatically in c# 
Csharp :: convert string to array c# 
Csharp :: c# override index operator 
Csharp :: c# switch by type of object 
Csharp :: c# choose first n elements from list 
Csharp :: c# odd even median 
Csharp :: how to play animation with code in unity 
Csharp :: unity get scrollbar value 
Csharp :: c# remove spaces from string 
Csharp :: displayname c# 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =