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 ...
随机推荐
- 使用openssl生成SSL证书完全参考手册
一般来说,配置HTTPS/SSL的步骤为: 1.生成足够强度的私钥.需要考虑:算法,广泛采用的一般是RSA.键长度,RSA默认为512,一般应选择2048.密码,虽然私钥不一定要加密存储,但是加密存储 ...
- 20145327《网络对抗》——注入shellcode并执行和Return-to-libc攻击深入
20145327<网络对抗>--注入shellcode并执行 准备一段Shellcode 老师的shellcode:\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68 ...
- Python3基础 try-指定except-as reason 捕获打开一个不存在的文件的时候,会产生OSError异常的示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- ubuntu下如何修改时区和时间
1.修改时区 sudo tzselect (按提示选择即可) sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 修改时间 sudo ...
- Luogu USACO Training 刷水记录
开个坑记录一下刷USACO的Training的记录 可能会随时弃坑 只有代码和做法简述 可能没有做法简述 [USACO1.1]你的飞碟在这儿Your Ride Is He… 模拟,细节已忘 #incl ...
- Java-master(github)教材整理
helloworld class HelloWorld { public static void main(String[] args) { System.out.println("hell ...
- Unity3D学习笔记(二十四):MVC框架
MVC:全名是Model-View-Controller View(视图层 - 顶层) Controller(控制层 - 中层) Model(数据层 - 底层) View(视图层) 说明:展现给玩家的 ...
- Facebook广告API系列 1
Facebook广告API系列 1 前言 最近遇到大坑了,居然要去对接facebook的广告API,之前以为是跟鹅厂一样的API体系,看了半天Facebook的文档,冷汗直冒.... 这得一点一点的讲 ...
- 【译】第2节--- 什么是Code First?
原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF从EF4.1中引入了Code-Firs ...
- POJ 1011 Sticks(dfs+剪枝)
http://poj.org/problem?id=1011 题意:若干个相同长度的棍子被剪成若干长度的小棍,求每根棍子原来的可能最小长度. 思路:很经典的搜索题. 我一开始各种超时,这题需要很多剪枝 ...