class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if (prices.empty()) return 0;
        int n = prices.size(), g[n][3] = {0}, l[n][3] = {0};
        for (int i = 1; i < prices.size(); ++i) {
            int diff = prices[i] - prices[i - 1];
            for (int j = 1; j <= 2; ++j) {
                l[i][j] = max(g[i - 1][j - 1] + max(diff, 0), l[i - 1][j] + diff);
                g[i][j] = max(l[i][j], g[i - 1][j]);
            }
        }
        return g[n - 1][2];
    }
};

相关文章:

  • 2021-08-24
  • 2022-01-08
  • 2021-06-22
  • 2022-02-15
  • 2021-08-24
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-28
  • 2022-12-23
  • 2021-06-15
  • 2021-06-02
相关资源
相似解决方案