Exemplo n.º 1
0
        static long minimumLoss(long[] price, long n)
        {
            var MinArray = new Minimum[price.Length];

            for (var i = 0; i < price.Length; i++)
            {
                MinArray[i] = new Minimum
                {
                    Value = price[i],
                    Index = i
                };
            }

            MinArray = MinArray.OrderBy((a) => a.Value).ToArray();

            var currentMin = long.MaxValue;

            for (var i = 0; i < price.Length - 1; i++)
            {
                if (MinArray[i + 1].Value - MinArray[i].Value < currentMin &&
                    MinArray[i].Index - MinArray[i + 1].Index < n &&
                    MinArray[i + 1].Index < MinArray[i].Index)
                {
                    currentMin = MinArray[i + 1].Value - MinArray[i].Value;
                }
            }

            return(currentMin);
        }