leetcode - 62,63. Unique Paths(II) & 64.Minimum Path Sum

算法系列博客之Dynamic Programming

本篇博客將運用動態規劃的思想來解決leetcode上264號問題

這三個題目的共同之處在於均是二維矩陣上的規劃問題

問題描述:

62 Unique Paths

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?

由於只能向下或者向右走一步,因而其狀態僅僅依賴於左側和上側,轉化爲數字形式即
假定paths[i][j]表示從(0,0)到(i,j)的總unique paths數目,則:
    paths[i][j] = paths[i-1][j] + paths[i][j-1]
觀察發現,其狀態依賴也僅僅只依賴於上一行和本行前列狀態,因而可以只存儲兩行狀態
特別地,paths[i][0] = paths[0][j] = 1,代碼實現如下

class Solution(object):
    def uniquePaths(self, m, n):
        paths = [[1] * n, [0] * n]
        for i in range(1, m):
            paths[1][0] = 1
            for j in range(1, n):
                paths[1][j] = paths[1][j-1] + paths[0][j]
            paths[0] = paths[1]
        return paths[0][n-1]

時間複雜度分析,兩層無嵌套常數條指令的循環,爲O(m*n)
空間複雜度分析,僅存儲兩行狀態,每行n列,爲O(n)

63 Unique Paths II

Follow up for “Unique Paths”:
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.

此題較上題不同的是,某些點可能無法通過
但仍然是隻能向右或者向下走一步,因而狀態依賴關係依然沒有改變,僅需要添加額外約束
即如果點(i,j)不能通過,則paths[i][j] = 0
如果點(i,j)能通過,則paths[i][j] = paths[i-1][j] + paths[i][j-1]
且初始化狀態只能有一個,即paths[0][0] = 1或0

class Solution(object):
    def uniquePathsWithObstacles(self, obstacleGrid):
        if obstacleGrid[0][0] == 1: return 0
        paths = [[1] * len(obstacleGrid[0])] * 2
        for j in range(1, len(obstacleGrid[0])):
            paths[0][j] = 0 if obstacleGrid[0][j] else paths[0][j-1]
        for i in range(1, len(obstacleGrid)):
            paths[1][0] = 0 if obstacleGrid[i][0] else paths[0][0]
            for j in range(1, len(obstacleGrid[0])):
                paths[1][j] = 0 if obstacleGrid[i][j] else paths[1][j-1] + paths[0][j]
            paths[0] = paths[1]
        return paths[0][len(obstacleGrid[0])-1]

複雜度較之前沒有改變,仍然是時間O(m*n),空間O(n)

64 Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

題目的模型較之前不同的是,每個點都增加了權值grid[i][j]
而目標是要找出權值最小的路徑,但是仍然有隻能向右或者向下的約束
因此仍然可以採用上面的那種策略,僅僅是狀態轉移方程稍微不一樣

假設minpath[i][j]表示從(0,0)到(i,j)的最小權值路徑,則
    minpath[i][j] = grid[i][j] + min(minpath[i][j-1], minpath[i-1][j])
特別地,minpath[0][0] = grid[0][0];
             minpath[0][j] = grid[0][j] + minpath[0][j-1];
             minpath[i][0] = grid[i][0] + minpath[i-1][0]
python代碼實現如下

class Solution(object):
    def minPathSum(self, grid):
        minpath = [[x for x in grid[0]], [0]*len(grid[0])]
        for j in range(1, len(grid[0])):
            minpath[0][j] += minpath[0][j-1]
        for i in range(1, len(grid)):
            minpath[1][0] = grid[i][0] + minpath[0][0]
            for j in range(1, len(grid[0])):
                minpath[1][j] = grid[i][j] + min(minpath[1][j-1], minpath[0][j])
            minpath[0] = minpath[1]
        return minpath[0][len(grid[0])-1]

同樣,複雜度並沒有改變,時間O(m*n),空間O(n)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章