【LeetCode练习题】Unique Paths

时间:2022-08-30 16:15:06

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?

【LeetCode练习题】Unique Paths

Above is a 3 x 7 grid. How many possible unique paths are there?

Note:m and n will be at most 100.

计算从起点到终点的最短距离。

(动态规划的题目,没错我开始学DP了……)

解题思路:

因为题目中提到了每一次只能向右或者向下移动,所以以终点为例,记为C(3,7),则能到达终点的点分别是A(3,6)和B(2,7),即从起点到达终点的路径数等于从起点到达A点和B点路径数量的和。C = A + B。

有了这一点,接下来就很好想了。

这一题说起来是动态规划,其实还是算挺简单的,他有好几种做法。

首先我刚刚看到这一题的时候,想法是这样的:

动态创建一个m行n列的数组p,存储每一个位置到起点的路径数量。

因为所有与起点start同一行上的点和同一列上的点到起点的路径只有一条,就是一条直线。所以p[0][0~n-1]和p[0~m-1][0]全部置为1。然后根据每一个位置等于它左边的值与上边的值的和就很好计算了。

比较好的一点是,这种看起来低端不上档次的方法还避免了动态规划方法的重复计算问题。

代码如下:

class Solution {
public:
int uniquePaths(int m, int n) {
if(m == || n == )
return ;
//动态创建二维数组
int **p;
p = new int*[m];
for(int i = ; i < m; i++){
p[i] = new int[n];
}
for(int i = ; i < m; i++)
for(int j = ; j < n;j++)
p[i][j] = ; //初始化第一行为1
for(int i = ; i < n; i++){
p[][i] = ;
}
//初始化第一列为1
for(int i = ; i < m; i++){
p[i][] = ;
}
//从第二行第二列开始计算每一格
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
p[i][j] = p[i][j-] + p[i-][j];
}
} int ret = p[m-][n-]; for(int i = ; i < m; i++)
delete[] p[i];
delete[] p; return ret;
}
};

接下来就是高端大气上档次滴采用递归的动态规划的解法了。或者叫做回溯法。

如果是这样子写:

class Solution {
public:
int uniquePaths(int m, int n) {
if(m == || n == )
return ;
return uniquePaths(m,n-)+uniquePaths(m-,n);
}
};

那么提交上去……

显示 超时 !

原因很简单,因为存在大量的重复计算!

所以我们还是需要维护一个二维数组来避免同一个位置的值重复计算。当某个值已经被计算出来,下次再用到这个值时,直接取就可以不用再去计算了。

所以代码变成了这样子:

const int M_MAX = ;
const int N_MAX = ; class Solution { public:
int uniquePaths(int m, int n) {
int mat[M_MAX+][N_MAX+];
for(int i = ; i < M_MAX+; i++){
for(int j = ; j < N_MAX+; j++){
mat[i][j] = -;
}
}
return backtrack(m,n,mat);
} int backtrack(int m,int n,int mat[][N_MAX+]){
if(m == || n == )
return ;
if(mat[m][n-] == -)
mat[m][n-] = backtrack(m,n-,mat);
if(mat[m-][n] == -)
mat[m-][n] = backtrack(m-,n,mat);
return mat[m][n-] + mat[m-][n];
}
};

我们知道以上的空间复杂度是 O(m * n)。

接下来的这种动态规划的空间复杂度是O(min(m , n))。

非常有意思。

public static int uniquePathsDP(int m, int n){
int x = Math.min(m, n);
int y = Math.max(m, n);
int[] ret = new int[x]; for(int i = ; i < x; i++)
ret[i] = ; for(int i = ; i < y; i++)
for(int j = ; j < x; j++)
{
ret[j] += ret[j - ];
} return ret[x - ];
}

注:

动态创建二维数组的代码,因为我总是记不住,就此记录:

char **a;
a = new char* [m];//分配指针数组
for(int i=; i<m; i++)
{
a[i] = new char[n];//分配每个指针所指向的数组
} printf("%d\n", sizeof(a));//4,指针
printf("%d\n", sizeof(a[]));//4,指针 for(i=; i<m; i++)
delete[] a[i];
delete[] a;

【LeetCode练习题】Unique Paths的更多相关文章

  1. LeetCode 63&period; Unique Paths II不同路径 II &lpar;C&plus;&plus;&sol;Java&rpar;

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  2. &lbrack;LeetCode&rsqb; 62&period; Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  3. LeetCode 62&period; Unique Paths(所有不同的路径)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. &lbrack;LeetCode&rsqb; 63&period; Unique Paths II&lowbar; Medium tag&colon; Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  5. &lbrack;Leetcode Week12&rsqb;Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  6. &lbrack;Leetcode Week12&rsqb;Unique Paths

    Unique Paths 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths/description/ Description A ...

  7. &lbrack;LeetCode&rsqb; 63&period; Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. &lbrack;LeetCode&rsqb; 62&period; 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. leetcode 62&period; Unique Paths 、63&period; Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

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

随机推荐

  1. 【转】JSP中的相对路径和绝对路径

    1.首先明确两个概念: 服务器路径:形如:http://192.168.0.1/的路径 Web应用路径:形如:http://192.168.0.1/yourwebapp的路径 2.关于相对路径与绝对路 ...

  2. 160908、前端开发框架Semantic UI

    简介 网页开发中,CSS控制网页样式.作为测试开发工程师,我个人不太擅长手写CSS.样式微调.兼容浏览器等工作,所以我选择使用成熟的前端框架,可以快速开发出样式美观的网站,也解决了大部分浏览器兼容问题 ...

  3. apecceosummit2016

    https://www.apecceosummit2016.com/program.html Thursday 17 November 2016 9:00am - 9:00pm REGISTRATIO ...

  4. Trapping Rain Water &lbrack;LeetCode&rsqb;

    Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...

  5. 项目管理及自动构建工具Maven

    项目管理及自动构建工具Maven 一.Maven安装.目录结构.cmd命令1.下载安装apache-maven-3.2.3-bin.zip下载:http://maven.apache.org/down ...

  6. Device&period;js——检测设备平台、操作系统的Javascript 库

    http://segmentfault.com/a/1190000000373735 Device.js 是一个可以让你检测设备的平台,操作系统和方向 JavaScript 库,它会自动在 <h ...

  7. 转&colon;简单介绍 P3P 技术

    原文来自于:http://blog.csdn.net/ghj1976/article/details/4889219 以 Internet Explorer 为例,默认情况下,IE的隐私策略如下图所设 ...

  8. Linux下arp用法

    [功能] 管理系统的arp缓存. [描述] 用来管理系统的arp缓存,常用的命令包括: arp: 显示所有的表项. arp  -d  address: 删除一个arp表项. arp  -s addre ...

  9. SpringMVC 配置

    1.在WEB-INF\web.xml中定义前端控制器 <servlet> <servlet-name>springmvc</servlet-name> <se ...

  10. JAVA版本微信管家平台—JeeWx 捷微 4&period;1 微服务版本发布,微信砍价活动闪亮登场!

    捷微 4.1   微服务版本发布,微信砍价活动闪亮登场 ^_^ JEEWX 从4.0版本开始,技术架构全新换代更名 “捷微H5”.这是一款开源免费的微信运营平台,是jeewx的新一代产品,平台涵盖了: ...