【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛
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++)的更多相关文章
- [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 ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 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 ...
- [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 ...
- 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 ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- [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 ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- 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). ...
随机推荐
- Python time&datetime模块
1.time&datetime模块 time&datetime是时间模块,常用以处理时间相关问题 time.time() #返回当前时间的时间戳timestamp time.sleep ...
- 24-Longest Palindromic Substring-Leetcode
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 源码分析-Consumer
消息消费概述 消息消费以组的模式开展,一个消费组内可以包含多个消费者,每一个消费者组可订阅多个主题,消费组之间有集群模式和广播模式两种消费模式. 集群模式,主题下的同一条消息只允许被其中一个消费者消费 ...
- Largest Rectangle in Histogram及二维解法
昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ...
- Android消除Toast延迟显示
Toast可以用来显示音量改变或者保存更新消息,如果用户一直点击,Toast会排队一个一个的,直到消息队列全部显示完,这样的效果显然是不好的,下面来看解决方法 Toast.makeText(ac ...
- oracle keep
语法: min | max(column1) keep (dense_rank first | last order by column2) over (partion by column3); -- ...
- mysql 报 'Host ‘XXXXXX’ is blocked because of many connection errors'
1. 问题:服务启动时,日志报错,导致启动失败: Caused by: com.mysql.cj.exceptions.CJException: null, message from server: ...
- 1945-祖安 say hello-String
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include<bits/stdc++.h> 3 char str[100][40]; 4 char s[10 ...
- 【经验分享】win10 cmake 构建 Tengine 工程
欢迎关注我的公众号 [极智视界],回复001获取Google编程规范 O_o >_< o_O O_o ~_~ o_O 本教程详细记录了在 win10 环境中 ...
- hooks中,useEffect无限调用问题产生的原因
前言:我在我的另一篇博客中有说道useEffect监听对象或者数组时会导致useEffect无限执行,并给予了解决方案-useEffect无限调用问题 .后来我想从其产生根源去理解并解决这个问题. 原 ...