leetcode best time to buy and sell stock 股票问题合集

121. 买卖股票的最佳时机

  1. 题意:给定一个数组,只能完成一笔交易,获取最大利润。
  2. 思路:找到每个最小值之后对应的最大值,减掉这个最小值得到的数中最大的即为所求。
  3. 做法:一直记录当前最小值,并不断更新当前值减最小值得到结果。

代码:

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) == 0:return 0
        min_ = prices[0]
        profit = 0
        for i in range(1, len(prices)):
            profit = max(profit, prices[i] - min_)
            min_ = min(prices[i], min_)
        return profit

122. 买卖股票的最佳时机 II

  1. 题意:可以进行无限次交易,获得最大利润。
  2. 思路:得到每个升序子数组。连续上升数组可以通过子数组相加,不变。如[1,2,3]利润为3-1=(2-1)+(3-2)
  3. 做法:每个数组值比前一个大就可以加到利润里。

代码:

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) == 0:
            return 0
        profit = 0
        last = prices[0]
        for i in range(1, len(prices)):
            if prices[i] > last:
                profit += prices[i] - last
            last = prices[i]
        return profit

 

714. 买卖股票的最佳时机含手续费

  1. 题意:还是可以进行无限次交易,但是每笔交易需要手续费,获取最大利润。
  2. 思路:
  • 和II的不同在于,每次交易需要手续费。
  • 动态规划,用两个数组存储当前时刻的两种状态:持有hold/非持有(卖出)sold。
  • hold[i]:当前有股票,要么是i-1就有,要么是i-1给卖出去(非持有)i时刻又买了。hold[i] = max(hold[i-1], sold[i - 1] - price[i]).
  • sold[i]:当前没股票,要么是i-1就没有,要么是i-1有,i时刻给卖了。sold[i] = max(sold[i - 1], hold[i - 1] + price[i] - fee).
  • 我们最后一刻一定是手里没有没卖出去的股票的,不然就亏了,所以只需要关注sold的最终值就是利润。
  • 返回值是sold[-1]。
  • 实际记录的时候只需要记录前面的值,所以不需要用数组来做,存储两个值即可。

代码:

class Solution(object):
    def maxProfit(self, prices, fee):
        """
        :type prices: List[int]
        :type fee: int
        :rtype: int
        """
        if len(prices) < 0:
            return 0
        sold, hold = 0, -prices[0]
        # sold ___no stock : no stock at t - 1 or sell at t
         # hold ___have stock : have stock at t - 1 or buy at t
        for i in range(1, len(prices)):
            sold = max(sold, hold + prices[i] - fee)
            hold = max(hold, sold - prices[i])
        return sold      

188. 买卖股票的最佳时机 IV

  1. 题意:最多可以进行k次交易,获取最大利润。
  2. 思路:
  • 和II的不同在于,不能交易那么多次。
  • 那么当k>len/2时,等同于2。可以遍历整个数组得到最大利润。
  • k<=len/2时,用动态规划的思想。用两个数组存储,和上一个带手续费的类似。
  • 需要考虑每次交易的情况。买一定在卖之前。
  • 这一次存储的是一个交易序列,sold[i]代表第i次交易后非持有的最大利润,hold[i]代表第i次交易后持有第最大利润。
  • i表示数组的索引,j代表交易次数。如,sold[i][j]代表数组中第三个数在第j次交易后非持有的最大利润。
  • 注意,求持有的时候,有种情况是上一次卖出sold这一次买入,j是不同的,应买在卖之后,所以是前一次卖,j-1交易。

代码:

class Solution(object):
    def maxProfit(self, k, prices):
        """
        :type k: int
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) < 2:
            return 0
        if k > len(prices)/ 2:
            profit = 0
            if len(prices) == 0:
                return profit
            for i in range(1, len(prices)):
                if prices[i] - prices[i - 1] > 0:
                    profit += prices[i] - prices[i - 1]
            return profit
        sold, hold = [0], [-2147483648]
        for i in range(len(prices)):
            sold.append(0)
            hold.append(-2147483648)
        for i in range(len(prices)):
            for j in range(1, k + 1):
                sold[j] = max(sold[j], hold[j] + prices[i])
                hold[j] = max(hold[j], sold[j - 1] - prices[i])
        return sold[k]

123. 买卖股票的最佳时机 III

  1. 题意:和IV一样,只允许交易两次。
  2. 思路:其实是IV的特例,可以直接用IV的代码,设置k=2。但是有点大材小用。
  3. 交易序列有着明显的顺序,buy1,sell1,buy2,sell2。
  4. buy1[0] = buy2[0] = -prices[i], sell1[0]=sell2[0]=0。
  5. 这里索引0代表初始值,下同。实际上并不需要数组存储,只需要四个数值。
  6. buy1[i] = max(buy1[i - 1], -prices[i])
  7. sell1[i] = max(sell1[i - 1], buy1[i] + prices[i])
  8. buy2[i] = max(buy2[i - 1], sell1[i]-prices[i]),从第一次卖出后,买入算利润。
  9. sell2[i] = max(sell2[i], buy2[i] + prices[i]), 这里已经进行了买——卖——买,直接算第二次买入后卖出的利润即可。

代码:

class Solution(object):
    def maxProfit(self, prices):
        """
        :type k: int
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) < 2:
            return 0
        sell1, buy1, sell2, buy2 = 0, -prices[0], 0, -prices[0]
        for i in range(1, len(prices)):
            buy1 = max(buy1, -prices[i])
            sell1 = max(sell1, buy1 + prices[i])
            buy2 = max(buy2, sell1 - prices[i])
            sell2 = max(sell2, buy2 + prices[i])
        return max(0, sell2, sell1)

309. 最佳买卖股票时机含冷冻期

  1. 题意:还是可以进行无限次交易,但是每笔交易需要冷却时间,获取最大利润。
  2. 思路:
  • 和带手续费的不同在于,卖出一次需要一次冷却期。
  • 题目要求影响买,不影响卖。
  • 动态规划,用两个数组存储当前时刻的两种状态:持有hold/非持有(卖出)sold和上一回合没有卖出的非持有sold_[i]。
  • hold[i]:当前有股票,要么是i-1就有,否则这一时刻想买必须保证上一回合非持有且没有交易过。hold[i] = max(hold[i-1], sold_[i - 1] - price[i]).
  • sold[i]:当前没股票,要么是i-1就没有,要么是i-1有,i时刻给卖了。sold[i] = max(sold[i - 1], hold[i - 1] + price[i]).
  • 我们最后一刻一定是手里没有没卖出去的股票的,不然就亏了,所以只需要关注sold的最终值就是利润。
  • 返回值是sold[-1]。
  • 实际记录的时候只需要记录前面的值,所以不需要用数组来做,存储三个值即可。

代码:

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) < 2:return 0
        sold_, sold = 0, 0
        hold = -prices[0]
        for i in range(1, len(prices)):
            hold = max(hold, sold_ - prices[i])
            sold_ = sold
            sold = max(sold, hold + prices[i])
            #print sold_, sold, hold
        return sold
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值