题目描述:
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.
题目大意:
一个机器人位于m x n隔板的左上角(在图中标记为“起点”)。
机器人在任意一点只可以向下或者向右移动一步。机器人尝试到达隔板的右下角(图中标记为“终点”)
有多少种可能的路径?
注意:m和n最多为100
解题思路:
解法I:动态规划
状态转移方程:
dp[x][y] = dp[x - 1][y] + dp[x][y - 1]
初始令dp[0][0] = 1
Python代码:
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = [[0] * n for x in range(m)]
dp[0][0] = 1
for x in range(m):
for y in range(n):
if x + 1 < m:
dp[x + 1][y] += dp[x][y]
if y + 1 < n:
dp[x][y + 1] += dp[x][y]
return dp[m - 1][n - 1]
上述解法空间复杂度可以优化至O(n):
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if m < n:
m, n = n, m
dp = [0] * n
dp[0] = 1
for x in range(m):
for y in range(n - 1):
dp[y + 1] += dp[y]
return dp[n - 1]
解法II:排列组合
题目可以转化为下面的问题:
求m - 1个白球,n - 1个黑球的排列方式
公式为:C(m + n - 2, n - 1)
Python代码:
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if m < n:
m, n = n, m
mul = lambda x, y: reduce(operator.mul, range(x, y), 1)
return mul(m, m + n - 1) / mul(1, n)
本文链接:http://bookshadow.com/weblog/2015/10/11/leetcode-unique-paths/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。