62. 63. Unique Paths 64. Minimum Path Sum
1.
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 uniquePaths(int m, int n) {
vector<vector<int>> v(m, vector<int>(n, ));
int i, j;
for(i = ; i < m; i++)
{
for(j = ; j < n; j++)
{
if( == i || == j)
v[i][j] = ;
else
v[i][j] = v[i-][j] + v[i][j-];
}
}
return v[m-][n-];
}
};
2.
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.
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
if(m <= )
return ;
int n = obstacleGrid[].size();
if(n <= || obstacleGrid[][] == )
return ;
vector<vector<int>> v(m, vector<int>(n, ));
int i, j;
for(i = ; i < m; i++)
{
for(j = ; j < n; j++)
{
if(obstacleGrid[i][j] == )
v[i][j] = ;
else if( == i && j)
v[i][j] = v[][j-];
else if( == j && i)
v[i][j] = v[i-][];
else if(i && j)
v[i][j] = v[i-][j] + v[i][j-];
else
v[i][j] = ;
}
}
return v[m-][n-];
}
};
3.
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.
int minPathSum(vector<vector<int> > &grid) {
if (grid.size()<=){
return ;
}
int i, j;
for(i=; i<grid.size(); i++){
for(j=; j<grid[i].size(); j++){
int top = i-< ? INT_MAX : grid[i-][j] ;
int left = j-< ? INT_MAX : grid[i][j-];
if (top==INT_MAX && left==INT_MAX){
continue;
}
grid[i][j] += (top < left? top: left);
}
}
return grid[grid.size()-][grid[].size()-];
}
62. 63. Unique Paths 64. 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 OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 【一天一道LeetCode】#64. Minimum Path Sum.md
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【leetcode】62.63 Unique Paths
62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...
- 【LeetCode】64. Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- [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 ...
- [LC] 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 ...
随机推荐
- 在ubuntu英文系统下使用中文输入法
How to install and use Chinese Input Method in the English Locale in Ubuntu ?(1) Check if there exis ...
- 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具
转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...
- C++ 表示一个区间值得方法
C++中不允许这样的写法 85<= score <=100;你要想表示85<=score<=100的话只能这么写score>=85&&score<= ...
- Python3基础 list 访问列表中的列表的元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Django组件(四) Django之Auth模块
Auth模块概述 Auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能 ...
- Go第七篇之规范的接口
接口本身是调用方和实现方均需要遵守的一种协议,大家按照统一的方法命名参数类型和数量来协调逻辑处理的过程. Go 语言中使用组合实现对象特性的描述.对象的内部使用结构体内嵌组合对象应该具有的特性,对外通 ...
- PMBOK十大知识领域是什么?
PMBOK十大知识领域是:整合管理.范围管理.时间管理.成本管理.质量管理.人力资源管理.沟通管理.风险管理.采购管理.干系人管理. 各用一句话概括项目管理知识体系十大知识领域: 1.整合管理:其作用 ...
- shell编程学习笔记之特殊变量($0、$1、$2、 $?、 $# 、$@、 $*)
特殊变量($0.$1.$2. $?. $# .$@. $*) shell编程中有一些特殊的变量可以使用.这些变量在脚本中可以作为全局变量来使用. 名称 说明 $0 脚本名称 $1-9 脚本执行时的参数 ...
- linux下命令行工具gcp显示拷贝进度条
1.环境: ubuntu16.04 Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_64 x ...
- BZOJ 1044: [HAOI2008]木棍分割 DP 前缀和优化
题目链接 咳咳咳,第一次没大看题解做DP 以前的我应该是这样的 哇咔咔,这tm咋做,不管了,先看个题解,再写代码 终于看懂了,卧槽咋写啊,算了还是抄吧 第一问类似于noip的那个跳房子,随便做 这里重 ...