程式語言 - 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 i = 0;
    int sold[50000] = { 0 };
    int hold[50000] = { 0 };
 
    hold[0] = -prices[0];
    for (i = 1; i < pricesSize; i++) {
        sold[i] = max(sold[i - 1], hold[i - 1] + prices[i] - fee);
        hold[i] = max(hold[i - 1], sold[i - 1] - prices[i]);
    }
 
    return sold[pricesSize - 1];
}