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