leetcode股票买卖问题系列

121. Best Time to Buy and Sell Stock

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 (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.

 Example 1:
    
    Input: [7,1,5,3,6,4]
    Output: 5
    Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 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
    Explanation: In this case, no transaction is done, i.e. max profit = 0.

只有一次交易,也就是找到差值前后最大的一组数。一次遍历搞定。

class Solution(object):
    def maxProfit(self, prices):
        if prices == []:
            return 0
        min_val = float('inf')
        max_profit = 0
        for i in range(len(prices)):
        # 找到最小的值
            if prices[i] < min_val:
                min_val = prices[i]
            else:
            # 计算计算收益
                max_profit = max(max_profit, prices[i]-min_val)
        return max_profit 

122. Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day 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).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
             Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.
Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

可以交易无数次,所有找到所有的收益加起来。
也就是找到交易中的所有的波峰波谷,差值相加就是总的收益。

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if not prices:
            return 0
        peak = vally = prices[0]
        max_p = 0
        i = 0 
        length = len(prices)
        while i < length-1:
            while i < length-1 and prices[i]>= prices[i+1]:
                i += 1
            valley = prices[i]
            while i < length-1 and prices[i] <= prices[i+1]:
                i+= 1
            peak = prices[i]
            max_p += peak - valley
        return max_p

123. Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day 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).

Example 1:

Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
             Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
             Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
             engaging multiple transactions at the same time. You must sell before buying again.
Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

最多两次交易,可以设置为最多K次,K=2

使用动态规划的思想,(智商不够,硬伤
设置两个数组,
一个全局收益global [i] [ j ] 代表 0-i天 共j次交易的最优收益
一个局部收益loca l[i] [j] 代表 0-i天 共j次交易,且第j次交易是在第i天卖出的条件下的 最优收益

递推公式:
global[i][j] = max(global[i-1][j], local[i][j])
全局最优也就是 比较当前天(第i天的局部收益 和前边i-1天的全局最优收益)
local[i][k] = max(local[i-1][k] + diff, global[i-1][k-1]+max(diff, 0))
局部最优是比较 前i-1天的j-1次交易中的全局最优 加上今天卖出的收益(大于0的收益)
和 前i-1天j次交易的局部收益加上今天卖出的收益(正负都得加上,因为局部最优必须是最后一笔交易是今天卖出),不用j-1 因为i-1天卖出的交易改为今天卖出,交易次数并不会变。

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        k = 3
        length = len(prices) 
        if length == 0:
            return 0
        local = [[0] * k for day in range(length)]
        global_ = [[0] * k for day in range(length)]
        for i in range(1, length):
            diff = prices[i]-prices[i-1]
            for j in range(1, k):
                local[i][j] = max(global_[i-1][j-1] + max(0, diff), local[i-1][j] + diff)
                global_[i][j] = max(global_[i-1][j], local[i][j])
        return global_[length-1][k-1]

188. Best Time to Buy and Sell Stock IV

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

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

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

Example 1:

Input: [2,4,1], k = 2
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:

Input: [3,2,6,5,0,3], k = 2
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
             Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.

这道题跟3 是一样的,只有一个小tric是当k很大时效率会很低。所以当K > 交易天数时直接用2的方法,也就是加上所有的前后差值。

class Solution(object):
    def maxProfit(self, k, prices):

        """
        :type prices: List[int]
        :rtype: int
        """
        length = len(prices) 
        if k<= 0 or length == 0:
            return 0
            
        if k > length:
            return self.helper(prices)
            
        k = k+ 1
        local = [[0] * k for day in range(length)]
        global_ = [[0] * k for day in range(length)]
        for i in range(1, length):
            diff = prices[i]-prices[i-1]
            for j in range(1, k):
                local[i][j] = max(global_[i-1][j-1] + max(0, diff), local[i-1][j] + diff)
                global_[i][j] = max(global_[i-1][j], local[i][j])
        return global_[length-1][k-1]
    def helper(self, prices):
        res = 0
        for i in range(1, len(prices)):
            if prices[i] > prices[i-1]:
                res += prices[i] - prices[i-1]
        return res
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值