Best Time to Buy and Sell Stock l, II, lll

本文探讨了在不同交易限制条件下,如何设计算法以最大化股票交易利润。包括仅能进行一次交易、可进行多次交易及最多允许两次交易的情况,并提供了具体的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0

In this case, no transaction is done, i.e. max profit = 0.

只能买卖一次,那自然是找左边的最大值,和右边的最小值。从左到右扫描数组,算出在每天之前买卖完成的最大利润,扫描结束即可获得最大利润。
记录左边的最小值,今天之前卖出的最大利润为:在最小值买入,如果当前价格卖出获得利润大于昨天之前卖出,那么更新最大利润,否则更早卖出比今天卖出划算。

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int profit = 0;
        if (prices.empty()) {
            return 0;
        }
        int min = prices[0];
        for (int i = 0; i < prices.size(); i++) {
            if (prices[i]-min > profit) {
                profit = prices[i]-min;
            }
            if (prices[i] < min) {
                min = prices[i];
            }
        }
        return profit;
    }
};

现在我们更换题目条件,允许多次买卖,但是在卖出去已持有股票之前,不允许再次买入。

这样一来,在每个极小值买入,在每个最近极大值卖出,可获得最大收益。
但如果不限制买卖次数,在股票价值有上升的时候卖出,其结果并不会改变(相当于把一个上坡分成好几段,eg:假设一号到四号,股票价值都在上升。一号买入,四号卖出的利润=一号买入,二号卖出再买入,三号卖出再买入,四号卖出的利润)

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int profit = 0;
        for (int i = 1; i < prices.size(); i++) {
            if (prices[i-1] < prices[i]) {
                profit += (prices[i]-prices[i-1]);
            }
        }
        return profit;
    }
};

如果限制最多只能买卖两次
那么找出
在每一天之前卖出的最大收益sell[n]
在每一天之后买入,后面能得到的最大收益buy[n+1]
那么买卖两次最大收益:
maxprofit = max{sell[i]+buy[i+1]}
注意:可能只买卖一次且在最后一天卖出,收益更大,因此sell[n]+0也要考虑。

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() <= 1) {
            return 0;
        }
        int n = prices.size();
        vector<int> sell(n,0);
        vector<int> buy(n+1,0);
        int profit = 0;
        int min = prices[0];
        for (int i = 1; i < n; i++) {
            if (prices[i]-min > profit) {
                profit = prices[i]-min;
            }
            if (prices[i] < min) {
                min = prices[i];
            }
            sell[i] = profit;
        }
        int max = prices[n-1];
        profit = 0;
        for (int i = n-1; i > 0; i--) {
            if (max-prices[i] > profit) {
                profit = max-prices[i];
            }
            if (prices[i] > max) {
                max = prices[i];
            }
            buy[i] = profit;
        }
        profit = prices[1]-prices[0];
        for (int i = 0; i < n; i++) {
            if (profit < sell[i]+buy[i+1]) {
                profit = sell[i]+buy[i+1];
            }
        }
        return profit;
    }
};

在LeetCode上更简单的方法

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int hold1 = INT_MIN, hold2 = INT_MIN, release1 = 0, release2 = 0;
        for(auto p : prices){
            release2 = max(release2,hold2+p);
            hold2 = max(hold2,release1-p);
            release1 = max(release1,hold1+p);
            hold1 = max(hold1,-p);
        }
        return max(release1,release2);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值