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;
}
贪心法-Best Time to Buy and Sell Stock
最新推荐文章于 2019-05-01 17:41:02 发布