列表

详情


MT10. 股票交易日

描述

在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于 2 ),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益。请采用时间复杂度低的方法实现。

给定价格序列 prices 及它的长度 n ,请返回最大收益。

数据范围:

示例1

输入:

[10,22,5,75,65,80],6

输出:

87

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 424KB, 提交时间: 2021-09-02

class Stock {
public:
    int maxProfit(vector<int> prices, int n) {
        // write code here
        int sell1 = 0 ,sell2 = 0;
        int buy1 = -prices[0],buy2 = -prices[0];
        for(int i=1;i<n;i++) {
            buy1 = max(buy1,-prices[i]);
            sell1 = max(sell1,buy1+prices[i]);
            buy2 = max(buy2,sell1 - prices[i]);
            sell2 = max(sell2,buy2 + prices[i]);
        }
        return sell2;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 436KB, 提交时间: 2021-09-19

class Stock {
public:
    int maxProfit(vector<int> prices, int n) {
        int pricesLen=n;
        int i,min,max,max1[pricesLen],max2[pricesLen],MAX=0;
    max1[0]=0;max2[pricesLen-1]=0;
    min=prices[0];
    for(i=1;i<pricesLen;i++)
    {
        if(prices[i]<min)
            min=prices[i];
        if(prices[i]-min>max1[i-1])
            max1[i]=prices[i]-min;
        else
            max1[i]=max1[i-1];
    }
    max=prices[pricesLen-1];
    for(i=pricesLen-2;i>=0;i--)
    {
        if(prices[i]>max)
            max=prices[i];
        if(max-prices[i]>max2[i+1])
            max2[i]=max-prices[i];
        else
            max2[i]=max2[i+1];
    }
    for(i=0;i<pricesLen;i++)
    {
        if(max1[i]+max2[i]>MAX)
            MAX=max1[i]+max2[i];
    }
    return MAX;
    }
};

C++ 解法, 执行用时: 2ms, 内存消耗: 476KB, 提交时间: 2020-12-20

class Stock {
public:
    int maxProfit(vector<int> prices, int n) {
        // write code here
        int max=0,min=0x7fffffff;
        int max1=0,max2=0;
        int res=0;
        int q[n],p[n];
        for(int i=0;i<prices.size();i++){
            if(prices[i]<min){
                min=prices[i];
            } 
            if((prices[i]-min)>max1){
                max1=prices[i]-min;
            }
            q[i]=max1;
            if(prices[n-i-1]>max){
                max=prices[n-i-1];
            } 
            if((max-prices[n-i-1])>max2){
                max2=max-prices[n-i-1];
            }
            p[i]=max2;
        }
        for(int i=0;i<prices.size()-1;i++){
            if((q[i]+p[n-i-2])>res)
                res=q[i]+p[n-i-2];
        }
        if(q[n-1]>res)res=q[n-1];
        return res;
    }
};

C++ 解法, 执行用时: 3ms, 内存消耗: 380KB, 提交时间: 2020-08-22

class Stock {
public:
    int maxProfit(vector<int> prices, int n) {
        // write code here
        int k = 2;
        int money = 0;

        vector<int> dp(n, 0);
        for (int i = 1; i <= k; i++) {
            int bestChoose = max(dp[0] - prices[0], dp[1] - prices[1]);
            dp[1] = max(dp[0], bestChoose + prices[1]);
            for (int j = 2; j < n; j++) {
                bestChoose = max(bestChoose, dp[j] - prices[j]);
                dp[j] = max(dp[j - 1], bestChoose + prices[j]);
            }
        }
        return dp[n - 1];
    }
};

C++ 解法, 执行用时: 3ms, 内存消耗: 408KB, 提交时间: 2022-05-23

class Stock {
public:
    int maxProfit(vector<int> prices, int n) {
        // write code here
        int first_buy = INT32_MIN;
        int first_sale = INT32_MIN;
        int second_buy = INT32_MIN;
        int second_sale = INT32_MIN;
        
        for(int i=0;i<n;i++){
            first_buy = max(first_buy, -prices[i]);
            first_sale = max(first_sale, prices[i] + first_buy);
            second_buy = max(first_sale-prices[i], second_buy);
            second_sale = max(second_sale, second_buy + prices[i]);
        }
        return second_sale;
    }
};

上一题