You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
591 B
24 lines
591 B
package class15;
|
|
|
|
//leetcode 123
|
|
public class Code03_BestTimeToBuyAndSellStockIII {
|
|
|
|
public static int maxProfit(int[] prices) {
|
|
if (prices == null || prices.length < 2) {
|
|
return 0;
|
|
}
|
|
int ans = 0;
|
|
int doneOnceMinusBuyMax = -prices[0];
|
|
int doneOnceMax = 0;
|
|
int min = prices[0];
|
|
for (int i = 1; i < prices.length; i++) {
|
|
min = Math.min(min, prices[i]);
|
|
ans = Math.max(ans, doneOnceMinusBuyMax + prices[i]);
|
|
doneOnceMax = Math.max(doneOnceMax, prices[i] - min);
|
|
doneOnceMinusBuyMax = Math.max(doneOnceMinusBuyMax, doneOnceMax - prices[i]);
|
|
}
|
|
return ans;
|
|
}
|
|
|
|
}
|