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的更多相关文章

  1. [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 ...

  2. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

  3. 【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 ...

  4. 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 ...

  5. 【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 ...

  6. LeetCode 113. 路径总和 II(Path Sum II)

    题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / ...

  7. leetcode 64. 最小路径和Minimum Path Sum

    很典型的动态规划题目 C++解法一:空间复杂度n2 class Solution { public: int minPathSum(vector<vector<int>>&am ...

  8. [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 ...

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

随机推荐

  1. Matlab实现求a到b被c整除的个数

    我先想到的是for循环........ 然后sum(find(mod(a:b,c)==0)),从10到100得到874,为什么不对呢? 比如a = [1 2 3 4  2 3 4 2],find(a= ...

  2. jQuery+css+div--一些细节详解

    (一).首先.让我们认识一下最基本普通的alert()弹出框!(改变alert()提示弹出框的样式) 我们在写html或是jsp页面的时候,谁都不希望自己精心设计,且非常美观的页面颜色布局被破坏掉吧! ...

  3. C# 虚方法 与 隐藏方法(new) 区别

    重写和隐藏的定义: 重写:继承时发生,在子类中重新定义父类中的方法,子类中的方法和父类的方法是一样的          例如:基类方法声明为virtual(虚方法),派生类中使用override申明此 ...

  4. uva 10912

    dp 记忆化搜索 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

  5. poj 3170

    两遍bfs ~ #include <cstdio> #include <cstdlib> //#include <cmath> #include <map&g ...

  6. Ubuntu环境下Nutch+Tomcat 搭建简单的搜索引擎

    简易的搜索引擎搭建 我的配置: Nutch:1.2 Tomcat:7.0.57 1 Nutch设置 修改Nutch配置 1.1 修改conf/nutch-site.xml <?xml versi ...

  7. 看来要学 Asp.Net 了

    C#大部分招聘都要这个:对个人用而言,太庞大了,所以对其的感观一直不咋,也就没想学了.

  8. hdu1022 Train Problem I

    http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...

  9. C程序的内存分配

    一.预备知识-程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)- 由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...

  10. Java开发--操作MongoDB

    http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过前一篇文章我们 ...