貪心法-Best Time to Buy and Sell Stock

學習不止於前發表於2018-06-08
 public static int MaxProfit(int[] prices)
        {
            if (prices.Length < 2) return 0;
            int profit = 0;
            int cur_min = prices[0];
            for(int i = 1; i < prices.Length; i++)
            {
                profit = Math.Max(profit, prices[i] - cur_min);
                cur_min = Math.Min(cur_min, prices[i]);
            }
            return profit;
        }

相關文章