LeetCode: Unique Paths I & II & Minimum Path Sum
Title:
https://leetcode.com/problems/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?

Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路:直观的思路是使用递归,但是会超时
class Solution{
public:
int m;
int n;
int uniquePaths(int m, int n) {
this->m = m;
this->n = n;
int sum = ;
fun(,,sum);
return sum;
}
void fun(int i,int j,int& sum){
if (i == m && j == n)
sum++;
if (i > m || j > n)
return ;
fun(i+,j,sum);
fun(i,j+,sum);
}
};
int uniquePaths(int m,int n){
if (m == || n == )
return ;
return uniquePaths(m-,n)+uniquePaths(m,n-);
}
一般这种递归都可以使用动态规划来解决
class Solution{
public:
int uniquePaths(int m,int n){
if (m < || n < )
return ;
vector<int> v(n,);
for (int i = ; i < m ; i++)
for (int j = ; j < n;j++){
v[j] += v[j-];
}
return v[n-];
}
};
Unique Path II
https://leetcode.com/problems/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.
Note: m and n will be at most 100.
开始想直接使用I中的,却没有考虑到边界上有障碍的情况
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid){
if (obstacleGrid.empty())
return ;
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
if (m < || n < )
return ;
vector<int> result(n);
result[] = ;
for (int i = ; i < m ; i++){
for (int j = ; j < n ; j++){
if (obstacleGrid[i][j] == )
result[j] = ;
else{
if (j > )
result[j] += result[j-];
}
}
}
return result[n-];
}
Minimun-Path-Sum
Title:
https://leetcode.com/problems/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){
if (grid.empty() || grid.size() == )
return ;
int m = grid.size();
int n = grid[].size();
vector<int> v(n,INT_MAX);
v[] = ;
for (int i = ; i < m; i++){
for (int j = ; j < n; j++){
if (j == ){
v[j] = v[j] + grid[i][j];
}else{
v[j] = min(v[j],v[j-]) + grid[i][j];
}
//cout<<v[j]<<" ";
}
//cout<<endl;
}
//cout<<endl;
return v[n-];
}
};
LeetCode: Unique Paths I & II & Minimum Path Sum的更多相关文章
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
- 【Leetcode】【Medium】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- LeetCode:Unique Paths I II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- 【LeetCode每天一题】Minimum Path Sum(最短路径和)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- LeetCode 113. 路径总和 II(Path Sum II)
题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / ...
- leetcode 64. 最小路径和Minimum Path Sum
很典型的动态规划题目 C++解法一:空间复杂度n2 class Solution { public: int minPathSum(vector<vector<int>>&am ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
随机推荐
- 你真的知道css三种存在样式(外联样式、内部样式、内联样式)的区别吗?
css样式在html中有三种存在形态: 内联样式:<div style="display: none"></div> 内部样式: <style> ...
- uva 307
排序之后再剪枝,有点神 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...
- 《head first java 》读书笔记(二)
Updated 2014/03/27 P402-P454 Updated 2014/04/03 P454- 世界三大首席管理器: border, flow, box borderLayout: 五个区 ...
- hdu 1575 Tr A(矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...
- MVC4.0中下来列表框的,两种使用方法DropDownList
后台控制器代码 public ActionResult Drop() { var list = new List<SchoolInfo>(); list.Add(new SchoolInf ...
- C# 面向对象之概念理解
什么是对象? <韦氏大词典>中对对象定义: (1)某种可为人所感知的物质. (2)思维.感受或动作所作用的物质或精神体. ----说白了万物皆对象 熟悉的对象描述: 对象就是客观世界中的物 ...
- Android 手机震动 设置震动时间
开启震动,单次,5秒: Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); //震动5秒 vibrator.vibra ...
- Git命令参考手册(转)
git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config --glob ...
- IE11兼容IE9问题
注意如果<head>标签里加<meta http-equiv="X-UA-Compatible"content="IE=EmulateIE9" ...
- ios开发跳转
如果我的是A->B->C后,我想直接从 C->A 应该怎么做???这是我的问题 看了这个帖子不太明白 这是10楼的解决办法 , 虽然 写的很清楚 ,但是还是没懂啊 ...