淘先锋技术网

首页 1 2 3 4 5 6 7

1. Best Time to Buy and Sell Stock 🔗

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # 先暴力

        # summ = 0
        # for i in range(len(prices)-1):
        #     for j in range(i+1, len(prices)):
        #         summ = max(summ, prices[j]-prices[i])
        # return summ

        # 改进版1
        # 维护一个从头开始最小买入值, 记录最大summ
        curMin = prices[0]
        summ = 0
        for i in range(1,len(prices)):
            curMin = min(curMin, prices[i])
            summ = max(summ, prices[i] - curMin)
        return summ

2. Best Time to Buy and Sell Stock II🔗

一下代码运行超时

class Solution:
    # 根据题1进行改编 父问题和子问题相同 采用递归结构 超时
    '''
    def maxProfit(self, prices: List[int]) -> int:
        res = 0
        curMin = prices[0]
        for buy in range(len(prices)):
            curMin = min(curMin, prices[buy])
            res = max(res, self.maxProfit(prices[sell+1:])+prices[sell]-curMin)
        return res
    '''
    # 添加备忘录结构  超时
    def maxProfit(self, prices):
        mome = [-1] * len(prices)
        def dp(start):
            if start >= len(prices): return 0
            if mome[start] != -1: return mome[start]
            # 借鉴题1思路
            res = 0
            curMin = prices[start]
            for sell in range(start+1, len(prices)):
                curMin = min(curMin, prices[sell])
                res = max(res, dp(sell+1)+prices[sell] - curMin)
            mome[start] = res
            return res

        s = dp(0)
        print(mome)
        return s
    
    # 最优算法 贪心
    def maxProfit(self, prices):
        maxProfit = 0
        for i in range(1, len(prices)):
            if prices[i] > prices[i-1]:
                maxProfit += prices[i] - prices[i-1]
        return maxProfit

3. Best Time to Buy and Sell Stock III/IV 🔗

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        memo = dict()
        def dp(start, k): # 加上参数k的限制
            if start >= len(prices): return 0
            if k == 0: return 0
            if (start, k) in memo: return memo[(start,k)]

            res = 0
            curMin = prices[start]

            for sell in range(start+1, len(prices)):
                curMin = min(curMin, prices[sell])
                res = max(res, dp(sell + 1, k - 1) + prices[sell] - curMin)
            memo[(start, k)] = res
            return res
        return dp(0, 2)

4. Best Time to Buy and Sell Stock with Cooldown🔗

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        memo = [-1] * len(prices)
        
        def dp(start):
            if start >= len(prices): return 0
            if memo[start] != -1: return memo[start]
            curMin = prices[start]
            summ = 0
            for sell in range(start+1, len(prices)):
                curMin = min(curMin, prices[sell])
                
                summ = max(summ, dp(start+2)+prices[sell] - curMin)
                
            memo[start] = summ
            
            return summ
        return dp[0]

5. Best Time to Buy and Sell Stock with Transaction 🔗

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        memo = [-1]*len(prices)
        def dp(start):
            if memo[start] !=-1: return memo[start]
            if start >= len(prices): return 0
            
            curMin = prices[start]
            summ = 0
            for sell in range(start, len(prices)):
                curMin = min(curMin, prices[sell])
                summ = max(summ, dp(sell+1)+prices[sell] - curMin - fee)
            memo[start] = curMin
            return curMin
        return dp(0)