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) 有关 这种情况下,时间 ...
随机推荐
- 剑指offer--面试题11
题目:求数值的整数次方,不考虑大数值情况 即实现函数:double Power(double base, int exponent) 自己所写代码如下: #include "stdafx.h ...
- MSVC CRT运行库启动代码分析
原文链接:http://www.programlife.net/msvc-crt-startup.html 在程序进入main/WinMain函数之前,需要先进行C运行库的初始化操作,通过在Visua ...
- C#和Javascript中 正则表达式使用的总结
说明:本文并非原创,而是从网站上搜集了一些资料整理的!如有雷同,纯属巧合 1.js中正则表达式的使用 在js中定义正则表达式很简单,有两种方式,一种是通过构造函数,一种是通过//,也就是两个斜杠.例如 ...
- sql 判断两个时间段是否有交集
本文转自CSDN 链接地址:http://blog.csdn.net/dasihg/article/details/8450195 时间段:starttime_1到endtime_1,starttim ...
- POJ 3411 Paid Roads(SPFA || DFS)
题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...
- 李洪强iOS开发之Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...
- 如何配置JAVA的环境变量、Tomcat环境变量
配置JAVA环境变量 1.右击[我的电脑]---[属性]-----[高级]---[环境变量],如图: 2.选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HOME ...
- 创业草堂之六:CEO的财务自修课
创业团队中一个最普遍的缺陷,是团队--尤其是团队的核心人物CEO,缺乏基本的财务知识和技能.一个不懂财务知识的CEO,即使业务能力再强,在投资人的眼里,他/她依然是一个笨拙的CEO.粗糙的CEO.鲁莽 ...
- C++Builder和VC的比较
C++Builder和VC的比较 其实很久以前我就想写这篇文章,其原因一方面是因为笔者深深感觉到C++ Builder的确是一个先进与强大的程式开发工具,但更最重要的一点是,我深信C++ Builde ...
- linux命令-shopt
shopt命令 shopt命令用于显示和设置shell中的行为选项,通过这些选项以增强shell易用性.shopt命令若不带任何参数选项,则可以显示所有可以设置的shell操作选项. 开启与关闭 开启 ...