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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值