作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/unique-paths/description/

题目描述:

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?

题目大意

给出了一个m * n的地图,上面有个机器人位于左上角,现在他想到达右下角。它每次只能向右边或者下边走一步,问能到达右下角的方式有多少种。

解题方法

方法一:组合公式

这个题搞明白之后其实就是一个排列组合中的组合类型的题目。

在总数为m + n - 2中的数目中挑选n - 1个位置放竖着的走。也就是我们说的C(m + n - 2)(n -1)的问题。

组合公式的计算方式为:,使用公式计算出结果就行了。

时间复杂度是O(m + n),空间复杂度是O(1)。

class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
total = m + n - 2
v = n - 1
def permutation(m, n):
son = 1
for i in range(m, m - n, -1):
son *= i
mom = 1
for i in range(n, 0, -1):
mom *= i
return son / mom
return permutation(total, min(v, total -v))

方法二:记忆化搜索

到达某个位置的次数怎么计算?可以想到是到达这个位置上面的位置的次数+到达坐标的次数。这里需要说明的是因为两个这个机器人走的方向只能向右或者向下,所以它到达上边位置和左边位置的次数中没有交集,所以可以直接相加。

把问题分解之后,我们就想到了用递归,那么递归的终止条件是什么?明显地机器人到达第一行或者第一列任意位置的可能性方式只有一种!那就是一直向这个方向走!

另外使用了记忆化数组保存已经走过位置的次数,可以加快运算。

时间复杂度是O(m * n),空间复杂度是O(m * n)。超过了99%的提交。

class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
memo = [[0] * n for _ in range(m)]
return self.dfs(m - 1, n - 1, memo) def dfs(self, m, n, memo):
if m == 0 or n == 0:
return 1
if memo[m][n]:
return memo[m][n]
up = self.dfs(m - 1, n, memo)
left = self.dfs(m, n - 1, memo)
memo[m][n] = up + left
return memo[m][n]

方法三:动态规划

看到上面记忆化搜索的方法就知道这个题同样可以使用动态规划解决。第一行第一列的所有方式只有1种,到达其他位置的方式是这个位置上面 + 这个位置左边用DP的话,和上面记忆化搜索差不多。

时间复杂度是O(m * n),空间复杂度是O(m * n)。超过了17%的提交,没有上面搜索快。

class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = [[0] * n for _ in range(m)]
for i in range(m):
dp[i][0] = 1
for i in range(n):
dp[0][i] = 1
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
return dp[m - 1][n - 1]

上面是把dp初始化为0,也可以换初始化为1:

class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = [[1] * n for _ in range(m)]
for i in range(m):
for j in range(n):
if i == 0 or j == 0:
continue
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
return dp[m - 1][n - 1]

使用C++代码如下,这次是把所有的位置都初始化成0,除了机器人刚开始所在的位置[1,1]设置成了1.

class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
dp[1][1] = 1;
for (int i = 1; i < m + 1; ++i) {
for (int j = 1; j < n + 1; ++j) {
if (i == 1 && j == 1) continue;
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m][n];
}
};

日期

2018 年 2 月 19 日
2018 年 10 月 18 日
2018 年 12 月 29 日 —— 2018年剩余电量不足1%

【LeetCode】62. Unique Paths 解题报告(Python & C++)的更多相关文章

  1. [LeetCode] 62. Unique Paths 唯一路径

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

  2. leetcode 62. Unique Paths 、63. Unique Paths II

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

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

  4. [LeetCode] 62. Unique Paths 不同的路径

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

  5. LeetCode 62. Unique Paths(所有不同的路径)

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

  6. leetcode 【 Unique Paths II 】 python 实现

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  7. [leetcode]62. Unique Paths 不同路径

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

  8. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

  9. LeetCode 62. Unique Paths不同路径 (C++/Java)

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

随机推荐

  1. Pyquery解析库的安装和使用

    Pyquery同样是一个强大的网页解析工具,它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便.GitHub:https://github.com/gawel/pyqu ...

  2. 【Python机器学习实战】聚类算法(1)——K-Means聚类

    实战部分主要针对某一具体算法对其原理进行较为详细的介绍,然后进行简单地实现(可能对算法性能考虑欠缺),这一部分主要介绍一些常见的一些聚类算法. K-means聚类算法 0.聚类算法算法简介 聚类算法算 ...

  3. Shell $()、${}、$[]、$(())

    目录 Shell中的 $().${}.$[].$(()) $().${} 替换 ${} 变量内容的替换.删除.取代 数组 $[].$(()) 运算符 Shell中的 $().${}.$[].$(()) ...

  4. HDFS06 DataNode

    DataNode 目录 DataNode DataNode工作机制 数据完整性 DataNode掉线时限参数设置 DataNode工作机制 一个数据块在DataNode上以文字形式存储在磁盘上,包括一 ...

  5. A Child's History of England.35

    The other two clung to the yard for some hours. At length the young noble said faintly, 'I am exhaus ...

  6. A Child's History of England.39

    He had become Chancellor, when the King thought of making him Archbishop. He was clever, gay, well e ...

  7. 【Reverse】DLL注入

    DLL注入就是将dll粘贴到指定的进程空间中,通过dll状态触发目标事件 DLL注入的大概流程 https://uploader.shimo.im/f/CXFwwkEH6FPM0rtT.png!thu ...

  8. 项目cobbler+lamp+vsftp+nfs+数据实时同步(inotify+rsync)

    先配置好epel源 [root@node3 ~]#yum install epel-release -y 关闭防火墙和selinux [root@node3 ~]#iptables -F [root@ ...

  9. 【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现

    NOTE05:PageAbility生命周期的呈现 基本界面设置 创建Slice与对应xml BarAbilitySlice package com.example.myapplication.sli ...

  10. Redis哨兵日常维护

    目录 一.日常操作 指定一个从做新主 添加一个从节点 添加一个Setinel节点 一.日常操作 指定一个从做新主 有时候需要将当前主节点机器下线,并指定一个高一些性能的从节点接替 将其它从节点的sla ...