Best Time to Buy and Sell Stock系列分析

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int size= prices.size();
        int profit = 0;
        for(int i=0;i<size;i++){
            for(int j=i+1;j<size;j++){
                if((prices[j] - prices[i])>profit){
                    cout<<i<<" "<<j<<endl;
                    profit = prices[j] - prices[i];
                }
            }
        }
        return profit;
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int size= prices.size();
        int profit = 0;
        for(int i=1;i<size;i++){
            if(prices[i-1]<prices[i]){
                cout<< prices[i]<<" "<<prices[i-1]<<endl;
                profit += (prices[i]-prices[i-1]);
            }
        }
        return profit;
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        const int size = prices.size();
        if(size<2){
            return 0;
        }
        int profit1[size+1], profit2[size+1];
        int minprice = prices[0];
        int maxprice = prices[size-1];
        profit1[0] = 0;
        for(int i=1;i<size;i++){
            profit1[i] = max(profit1[i-1], prices[i] -  minprice);
            if(minprice > prices[i]){
                minprice = prices[i];
            }
        }
        profit2[size-1] = 0;
        for(int i=size-2;i>=0;i--){
            profit2[i] = max(profit2[i+1], maxprice - prices[i]);
            if(maxprice < prices[i]){
                maxprice = prices[i];
            }
        }
        int global = 0;
        for(int i=0;i<size;i++){
            global = max(global, profit1[i]+profit2[i]);
        }

        return global;
    }


};
  • Best Time to Buy and Sell Stock IV
    来自 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/discuss/
    题目描述:明确规定最多只能进行k次交易
    将问题拆分成k个子问题,第0天到第cc天,第cc+1天到最后一天
    用global和local来更新,考虑到第j天的交易可能跟第j-1天重复了 本来是1次交易但记录成两次
    另外,如果k很大的时候,退化成第二种问题。注意,当k很大的时候,我们开数组的时候要小心,不然会爆掉,然后Runtime error
    local[j] = max(global[j-1]+max(diff,0), local[j]+diff);
    global[j] = max(local[j], global[j]);
class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        int size = prices.size();
        if(size<2){
            return 0;
        }
        if(k>=size){
            return solveMaxProfit(prices);
        }
        int local[k+1]={0};//k太大的时候 创建数组的时候爆掉了···
        int global[k+1]={0};
        for(int i=0;i<size-1;i++){
            int diff = prices[i+1]-prices[i];
            for(int j=k;j>=1;j--){
                local[j] = max(global[j-1]+max(diff,0), local[j]+diff);
                global[j] = max(local[j], global[j]);
            }
        }
        return global[k];

    }
    int solveMaxProfit(vector<int>& prices) {
        int size = prices.size();
        int result = 0;
        for(int i=1;i<size;i++){
            if(prices[i] - prices[i-1]>0){
                result += prices[i]-prices[i-1];
            }
        }
        return result;
    }
};
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值