动态规划

63.不同路径 II

https://leetcode-cn.com/problems/unique-paths-ii/description/

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
    
        """
        n = len(obstacleGrid[0])
        m = len(obstacleGrid)
        dp = [[0 for _ in range(n)] for _ in range(m)]
        if obstacleGrid[0][0] or obstacleGrid[-1][-1] == 1:
            return 0
        flag = True  
        for i in range(m):  # 第一列第i行有障碍,则第一列第i行之后的位置可通过的路径数都为0
            if obstacleGrid[i][0] == 1:
                flag = False
            if flag:
                dp[i][0] = 1
            else:
                break
        flag = True
        for j in range(n):  # 第一行第i列有障碍,则第一行第i列之后的位置可通过的路径数都为0
            if obstacleGrid[0][j] == 1:
                flag = False
            if flag:
                dp[0][j] = 1
            else:
                break
                                
        
        for i in range(1,m):
            for j in range(1,n):
                if obstacleGrid[i][j] == 1:
                    dp[i][j] = 0
                else:
                    dp[i][j] = dp[i-1][j]+dp[i][j-1]
        return dp[-1][-1]

 

120. 三角形最小路径和

https://leetcode-cn.com/problems/triangle/description/

class Solution:
    def minimumTotal(self, triangle):
        """
        :type triangle: List[List[int]]
        :rtype: int
        """
        # 从底往上逆推,前一行对应位置的最短路径为其下方相邻位置最短路径与其自身的和,如此递推到最上面
        row = len(triangle)
        for i in range(row-2,-1,-1):
            for j in range(i+1):
                triangle[i][j] = triangle[i][j]+min(triangle[i+1][j],triangle[i+1][j+1])
        return triangle[0][0]

 

121. 买卖股票的最佳时机

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/

方法一:遍历一次,记录当前最小价格,并往后推后面日期与当前最小价格的差值,更新最大差值。


class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if not prices:
            return 0
        min_price = prices[0]
        res = 0
        for i in range(1,len(prices)):
            if prices[i]

方法二:动态规划更新状态

class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if not prices:
            return 0
        res = 0
        profit = [[0 for i in range(3)] for i in range(len(prices))] # 第一列表示没有买股票,第二列表示持股数为1,三列表示持股1且卖出
        profit[0][0],profit[0][1],profit[0][2] = 0,-prices[0],0
        for i in range(1,len(prices)):
            profit[i][0] = profit[i-1][0]  # 没有买卖股票
            profit[i][1] = max(profit[i-1][1],profit[i-1][0]-prices[i]) # 两种情况,一种是前一天就持股一个, 一种是当天买入
            profit[i][2] = profit[i-1][1] + prices[i]  # 之前买入且当天卖出
            res = max(res,profit[i][0],profit[i][1],profit[i][2])
            
            
        return res

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

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/description/

思路:只要后面的价格比前面高就买入前面,并在最近高点卖出。累计即可得到最大值。

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

 

你可能感兴趣的:(算法和数据结构)