Say you have an array for which theithelement is the price of a given stock on dayi.
Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
tag: stock
class Solution {
public int maxProfit(int[] prices) {
int hold1 = Integer.MIN_VALUE, hold2 = Integer.MIN_VALUE;
int release1 = 0, release2 = 0;
for (int i = 0; i < prices.length; i++){
release2 = Math.max(release2, hold2 + prices[i]);
hold2 = Math.max(hold2, release1 - prices[i]);
release1 = Math.max(release1, hold1 + prices[i]);
hold1 = Math.max(hold1, -prices[i]);
}
return release2;
}
}