leetcode Best Time to Buy and Sell Stock专题

最佳时间买卖股票问题在leetcode中出现了很多次,并且这些题目之间也具有相似性,所以我将它们做成一个专题来进行总结。

121. Best Time to Buy and Sell Stock

题目:

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

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

Note that you cannot sell a stock before you buy one.

分析:

这是一道比较简单的编程题,也是这系列题中的一道入门题。题目大意是只准完成一项交易,求利润最大值。我们需要用到两个变量release1和hold1,其中release1代表的是当前利润最大值;hold1代表的是当前股票最低价。这样就可以用动态规划来求解。

代码(c++):

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int hold1 = INT_MIN, release1 = 0;
        for(int i : prices) {
            release1 = max(release1, hold1 + i);
            hold1 = max(hold1, -i);
        }
        return release1;
    }
};

122. Best Time to Buy and Sell Stock II

题目:

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

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

分析:

这道题在第一题的基础上不限制交易次数。没有交易次数的限制后,只要比前一天股票价格高就是盈利。

代码(c++):

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

123. Best Time to Buy and Sell Stock III

题目:

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

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

分析:

这道题也是和第一题的思路类似,区别在于第一题是只能交易一次,这道题是最多交易两次。我们需要用到四个变量release1、release2和hold1、hold1,其中release1、hold1和第一题含义一样;hold2的含义就是在交易一次的基础上再买股票的最佳入手时机、release2则表示交易两次的最大利润。用动态规划来求解。

代码(c++):

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int hold1 = INT_MIN, hold2 = INT_MIN;
        int release1 = 0, release2 = 0;
        for(int i:prices){
            release2 = max(release2, hold2+i);
            hold2    = max(hold2,    release1-i);
            release1 = max(release1, hold1+i);
            hold1    = max(hold1,    -i); 
        }
        return release2;
    }
};

188. Best Time to Buy and Sell Stock IV

题目:

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

Design an algorithm to find the maximum profit. You may complete at most k transactions.

分析:

是第三题的一个推广,但是提交的时候可能会出现一个内存溢出的错误。原因是k值远远大于prices大小,这样用dp就不合理了。所以需要结合第二题的思路来进行解决。

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        if(k == 0) return 0;
        if(k >= prices.size()) {
            int profit = 0;
            for(int i = 1; i < prices.size(); i++) {
                if(prices[i] > prices[i-1]) profit = profit + (prices[i] - prices[i-1]);
            }
            return profit;
        }
        vector<int> hold(k, INT_MIN);
        vector<int> release(k, 0);
        for(int i : prices){
            for(int j = k - 1; j >= 1; j--) {
                release[j] = max(release[j], hold[j] + i);
                hold[j] = max(hold[j], release[j - 1] - i);
            }
            release[0] = max(release[0], hold[0] + i);
            hold[0] = max(hold[0], -i); 
        }
        return release[k-1];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值