Esempio n. 1
0
        private static void StockBuySell(int[] price)
        {
            List <BuySellInterval> lstIntervals = new List <BuySellInterval>();
            int n = price.Length;
            int i = 0;

            while (i < n)
            {
                // Find Local Minima. Note that the limit is (n-2) as we are
                // comparing present element to the next element.
                while ((i < n - 1) && (price[i + 1] <= price[i]))
                {
                    i++;
                }

                // If we reached the end, break as no further solution possible
                if (i == n - 1)
                {
                    break;
                }

                BuySellInterval e = new BuySellInterval();
                e.Buy = i++;
                // Store the index of minima


                // Find Local Maxima. Note that the limit is (n-1) as we are
                // comparing to previous element
                while ((i < n) && (price[i] >= price[i - 1]))
                {
                    i++;
                }

                // Store the index of maxima
                e.Sell = i - 1;
                lstIntervals.Add(e);
            }

            // print solution
            if (lstIntervals.Count == 0)
            {
                Console.WriteLine("There is no day when buying the stock " + "will make profit");
            }
            else
            {
                foreach (var interval in lstIntervals)
                {
                    Console.WriteLine("Buy on day: " + interval.Buy
                                      + "        " + "Sell on day : " + interval.Sell);
                }
            }
        }
Esempio n. 2
0
        private void StockBuySell(int[] array)
        {
            int length = array.Length;
            int i      = 0;
            List <BuySellInterval> intervals = new List <BuySellInterval>();

            while (i < length - 1)
            {
                if (length <= 1)
                {
                    Console.WriteLine("Invalid array size");
                    return;
                }

                while ((i < length - 1) && (array[i + 1] <= array[i]))
                {
                    i++;
                }

                if (i >= length - 1)
                {
                    break;
                }

                BuySellInterval interval = new BuySellInterval();
                interval.Buy = array[i];

                i++;

                while ((i < length) && (array[i] > array[i - 1]))
                {
                    i++;
                }

                interval.Sell = array[i - 1]; // i-1 is needed as during the final condition, the i is incremented. A in the condition we're checking a[i] with a[i-1].

                intervals.Add(interval);
            }

            foreach (var item in intervals)
            {
                Console.WriteLine($"Buy: {item.Buy}  Sell {item.Sell}");
            }
        }