leetcode-714-线性DP-买卖股票的最佳时机含手续费 发表于 2020-04-26 | 分类于 数据结构与算法 题目 解法123456789101112131415161718// 动态规划,优化空间复杂度class Solution {public: int maxProfit(vector<int>& prices, int fee) { int n = prices.size(); if(prices.empty() || n == 1) return 0; int dp_i_0 = 0; int dp_i_1 = -prices[0]; for(int i = 0; i < n; ++i){ int temp = dp_i_0; dp_i_0 = max(dp_i_0, dp_i_1 + prices[i] - fee); dp_i_1 = max(dp_i_1, temp - prices[i]); } return dp_i_0; }};