程式語言 - LeetCode - C - 714. Best Time to Buy and Sell Stock with Transaction Fee



參考資訊:
https://www.cnblogs.com/grandyang/p/7776979.html

題目:


解答:

int max(int a, int b)
{
    return a > b ? a : b;
}

int maxProfit(int *prices, int pricesSize, int fee)
{
    int cc = 0;
    int sold[50000] = { 0 };
    int hold[50000] = { 0 };

    hold[0] = -prices[0];
    for (cc = 1; cc < pricesSize; cc++) {
        sold[cc] = max(sold[cc - 1], hold[cc - 1] + prices[cc] - fee);
        hold[cc] = max(hold[cc - 1], sold[cc - 1] - prices[cc]);
    }

    return sold[pricesSize - 1];
}