leetcode上卖股票系列题
只能交易一次
121. Best Time to Buy and Sell Stock
public int MaxProfit(int[] prices)
{
if (prices == null || prices.Length == 0)
{
return 0;
}
var b = prices[0];
var maxSoFar = 0;
var maxcurr = 0;
foreach (var p in prices)
{
maxcurr += p - b;
maxcurr = Math.Max(maxcurr, 0);
b = p;
maxSoFar = Math.Max(maxcurr, maxSoFar);
}
return maxSoFar;
}
可以交易无限次
122. Best Time to Buy and Sell Stock II
public int MaxProfitII(int[] prices)
{
if (prices == null || prices.Length == 0) return 0;
var first = prices[0];
var res = 0;
for (var i = 1; i