用DP[i]表示前i + 1天的最大利润,我们有递推公式:
- localMax = max(max(dp[j - 1] + prices[i] - prices[j] - fee), prices[i] - prices[0] - fee), where 1 <= j < i
- dp[i] = max(dp[i - 1], localMax)
递推公式取dp[j - 1] + prices[i] - prices[j] - fee或者dp[j] + prices[i] - prices[j] - fee都是可以的,不会影响最终的结果。时间复杂度O(n),空间复杂度O(n):
我们可以优化到常数空间,代码如下:
No comments:
Post a Comment