leetcode
来源:互联网 发布:ubuntu登陆界面鼠标 编辑:程序博客网 时间:2024/06/05 04:07
算法系列博客之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)
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- 对象是否存活
- NumPy的详细教程
- NVIDIA Jetson TX1 系列开发教程之五:CAFFE安装与NVIDIA多媒体例程测试
- 蚂蚁分类信息系统5.8 数据标签调用方法(三)
- 对Java事物的理解
- leetcode
- 希尔排序法
- Qt槽连接
- spring mvc
- Hadoop benchmark测试及调参
- mysql 之: inner join , left join 和 right join 的区别
- 迪杰斯特拉堆优化
- 如何提高深度学习(和机器学习)的性能
- 二叉苹果树