Best Time to Buy and Sell Stock with Transaction Fee

https://www.lintcode.com/problem/best-time-to-buy-and-sell-stock-with-transaction-fee/description

public class Solution {
    /**
     * @param prices: a list of integers
     * @param fee: a integer
     * @return: return a integer
     */
    public int maxProfit(int[] prices, int fee) {
        // write your code here
        int[] buy = new int[prices.length];
        int[] sell = new int[prices.length];
        buy[0] = -prices[0];
        sell[0] = 0;
        for (int i = 1; i < sell.length; i++) {
            buy[i] = Math.max(sell[i - 1] - prices[i], buy[i - 1]);
            sell[i] = Math.max(buy[i - 1] + prices[i] - fee, sell[i - 1]);
        }
        return sell[sell.length - 1];
    }
}

你可能感兴趣的:(Best Time to Buy and Sell Stock with Transaction Fee)