https://leetcode.com/problems/unique-paths/css
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).ide
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).spa
How many possible unique paths are there?code
Above is a 7 x 3 grid. How many possible unique paths are there?blog
Note: m and n will be at most 100.leetcode
Example 1:get
Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Right -> Down 2. Right -> Down -> Right 3. Down -> Right -> Right
Example 2:it
Input: m = 7, n = 3 Output: 28
1 class Solution: 2 def uniquePaths1(self, m: int, n: int) -> int: 3 dp = [ [ 1 for x in range(n) ] for x in range(m) ] 4 5 for i in range(1, m): 6 for j in range(1, n): 7 dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 8 9 return dp[m - 1][n - 1] 10 11 def uniquePaths2(self, m: int, n: int) -> int: 12 previous, current = [1] * n, [1] * n 13 14 for i in range(1, m): 15 for j in range(1, n): 16 current[j] = previous[j] + current[j - 1] 17 18 current, previous = previous, current 19 20 # note : previous not current 21 return previous[n - 1] 22 23 def uniquePaths(self, m: int, n: int) -> int: 24 current = [1] * n 25 26 for i in range(1, m): 27 for j in range(1, n): 28 current[j] += current[j - 1] 29 30 return current[n - 1]