moonlightshadow123
5/30/2017 - 3:02 PM

62. Unique Paths

  1. Unique Paths
class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        matrix = [[0 for _ in range(n)]for _ in range(m)]
        for i in range(m):
            for j in range(n):
                if i == 0 or j == 0:
                    matrix[i][j] = 1
                else:
                    matrix[i][j] = matrix[i][j-1] + matrix[i-1][j]
        return matrix[m-1][n-1]
class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        mat = [[0 for _ in range(m+1)] for _ in range(n+1)]
        mat[n-1][m] = 1
        for i in range(n-1,-1,-1):
            for j in range(m-1,-1,-1):
                mat[i][j] = mat[i][j+1] + mat[i+1][j]
        return mat[0][0]

https://leetcode.com/problems/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?