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. 【ZOJ】【3329】One Person Game

    概率DP/数学期望 kuangbin总结题目中的第三道 看来还是没有进入状态啊……都说是DP了……当然是要找[状态之间的转移关系]了…… 本题中dp[i]跟 dp[i-(k1+k2+k3)] 到dp[ ...

  2. 【转载】CCombobox使用大全

    一.如何添加/删除Combo Box内容 1. 在Combo Box控件属性的Data标签里面添加,一行表示Combo Box下拉列表中的一行.换行用ctrl+回车. 2. 在程序初始化时动态添加 如 ...

  3. [转载]Winform开发框架之统计图表的实现

    在前面的一些随笔中,介绍了不少我的Winform框架的特性,上篇随笔<Winform开发框架之通用高级查询模块>对其中的通用高级模块进了一个整理说明,本篇继续介绍Winform开发框架重要 ...

  4. 精通ASP.Net MVC 3 框架(第三版)学习笔记

    精通ASP.Net MVC 3 框架(第三版)学习笔记 代码才是王道. http://pan.baidu.com/s/1pJyL1cn

  5. hdu 3807

    很好的思路     枚举有多少人有ipad 判是否满足题目给出的条件 #include <iostream> #include <cstring> #include <c ...

  6. Consumer Client Re-Design (翻译)

    注:0.9版本Kafka的一个重大改变就是consumer和producer API的重新设计. 这篇Kafka的文档大致介绍了对于consumer API重新设计时想要实现的功能.0.9版本的确实现 ...

  7. Dijsktra算法C++实现

    Dijsktra算法解决了有向图G=(V,E)上带权的单源最短路径问题.但要求所有边的权值非负. 思想:Dijkstra算法中设置了一顶点集合S,从源点s到集合中的顶点的最终最短路径的权值均已确定.算 ...

  8. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  9. java基础知识回顾之---java String final类普通方法的应用之“子串在整串中出现的次数”

    /* * 2 一个子串在整串中出现的次数. * "loveerlovetyloveuiloveoplove" * 思路: * 1,要找的子串是否存在,如果存在获取其出现的位置.这个 ...

  10. **CI两种方式查询所返回的结果数量

    区别:第一个是有条件的:第二个没有条件 $this->db->count_all_results(); 允许你获得某个特定的Active Record查询所返回的结果数量.可以使用Acti ...