LeetCode_62、63、64三题(动态规划)
来源:互联网 发布:无数据库论坛源码 编辑:程序博客网 时间:2024/06/03 14:10
LeetCode_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?
解析:就是问从左下角到右下角有多少条路径。这是一题典型的动态规划问题,考虑终点,到终点的前一步有两种:1、终点左边右移一步。2、终点上面下移一步。
再把最左一排和最上面一排初始化为1条路径(很显然)。
具体代码如下:
class Solution {public: int uniquePaths(int m, int n) { vector<vector <int> > a; a.resize(m); for(int i = 0;i < m;i++){ a[i].resize(n); } for(int i = 0;i < m;i++){ a[i][0] = 1; } for(int i = 0;i < n;i++){ a[0][i] = 1; } for(int i = 1;i < m;i++){ for(int j = 1;j < n;j++){ a[i][j] = a[i-1][j] + a[i][j-1]; } } return a[m-1][n-1]; }};
LeetCode_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
解析:题意和前一题一模一样,只是在中间加了一些障碍物,即表示不能到达障碍物。则根据前一题,多添加一个判断:前一步为障碍物则不能到达。这里需要注意的是左边界和上边界的1,他们分别下面的和右边的是不能达到的。还有就是终点是1的,路径显然就是
具体代码如下:
class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { vector<vector <int> > a; int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); if(obstacleGrid[m-1][n-1] == 1)return 0; a.resize(m); for(int i = 0;i < m;i++){ a[i].resize(n,0); } for(int i = 0;i < m;i++){ if(obstacleGrid[i][0] == 1){ break; } a[i][0] = 1; } for(int i = 0;i < n;i++){ if(obstacleGrid[0][i] == 1){ break; } a[0][i] = 1; } for(int i = 1;i < m;i++){ for(int j = 1;j < n;j++){ if(obstacleGrid[i-1][j] != 1)a[i][j] = a[i-1][j]; if(obstacleGrid[i][j-1] != 1)a[i][j] += a[i][j-1]; } } return a[m-1][n-1]; }};
LeetCode_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.
解析:找到从左下角到右下角的最小和,也是典型的动态规划问题,到达前一步是最小和,则到达下一步也是最小和。
具体代码如下:
class Solution {public: int minPathSum(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].size(); vector<vector<int>> getmin(m, vector<int>(n,INT_MAX)); if(m == 1 && n == 1)return grid[0][0]; for(int i = 0;i < m;i ++){ for(int j = 0;j < n;j ++){ if(i == 0 && j == 0)getmin[i][j] = grid[i][j]; else if(i > 0 && j == 0)getmin[i][j] = getmin[i-1][j] + grid[i][j]; else if(i == 0 && j > 0)getmin[i][j] = getmin[i][j-1] + grid[i][j]; else getmin[i][j] = min(getmin[i][j-1],getmin[i-1][j]) + grid[i][j]; } } return getmin[m-1][n-1]; }};
- LeetCode_62、63、64三题(动态规划)
- 浅谈动态规划(三)
- 动态规划入门(三)
- 算法(三):动态规划
- 网易编程题三,跳石板(动态规划)
- LeetCode_70、300、303三题(动态规划)
- 简单动态规划集(三)
- 动态规划(三)--矩阵链乘法
- 选球博奕与动态规划(三)
- 动态规划 (三) 背包问题
- 动态规划(三)背包问题
- 动态规划的思考(三)
- 第十三周 动态规划(三)
- 算法(三):动态规划法
- 【动态规划(三)】经典背包问题
- 动态规划示例三
- 动态规划三
- 动态规划入门<三>
- 数据访问层(连接数据库)
- 行级元素上下边距 块级元素左右边距
- shell传参注意空格
- Windows环境下IOS APP打包上传AppStore详细流程
- Intellij IDEA设置显示行号
- LeetCode_62、63、64三题(动态规划)
- 字母所有的排列方式
- CCF NOI1069 分解因数
- 自定义一个转换器
- 【其他】论文框架
- 2017.05.06 记雏鹰博客系统开工第4天。
- 外边距失效bug
- ZOJ3944-People Counting
- spring中输入输出json数据