Best Time to Buy and Sell Stock IV

韩峰
2023-12-01

https://gist.github.com/ElninoFong/d08051d98e634991cd93

http://www.cnblogs.com/grandyang/p/4295761.html


#include <vector>
#include <iostream>
#include <cstdio>
#include <climits>
#include <cmath>
using namespace std;

class Solution {
public:
    int maxProfit(int k, vector<int> &prices) {
        if(prices.empty() || k == 0)
          return 0;

        if(k >= prices.size())
          return solveMaxProfit(prices);

        vector<int> global(k + 1, 0);
        vector<int> local(k + 1, 0);

        for(int i = 1; i < prices.size(); i++) {
            int diff = prices[i] - prices[i - 1];
            for(int j = k; j >= 1; j--) {
                local[j] = max(local[j] + diff, global[j - 1] + max(diff, 0));
                global[j] = max(global[j], local[j]);
            }
        }

        return global[k];
    }
private:
    int solveMaxProfit(vector<int> &prices) {
        int res = 0;
        for(int i = 1; i < prices.size(); i++) {
            int diff = prices[i] - prices[i - 1];
            if(diff > 0)
              res += diff;
        }
        return res;
    }
};


 类似资料:

相关阅读

相关文章

相关问答