714. Best Time to Buy and Sell Stock with Transaction Fee
Solution
public int maxProfit(int[] prices, int fee) {
int end_buy = Integer.MIN_VALUE, end_sell = 0;
for(int price : prices){
int old_end_buy = end_buy; //just to avoid affecting second equation
end_buy = Math.max(end_buy, end_sell-price-fee);
end_sell = Math.max(end_sell, old_end_buy + price);
}
return end_sell;
}
Last updated
Was this helpful?