62. Unique Paths

A robot is located at the top-left corner of a m × 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 × 7 grid. How many possible unique paths are there?

Note:

m and n will be at most 100.

Solution: DP

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        paths = [1] * n
        for row in xrange(1, m):
            for col in xrange(1, n):
                paths[col] += paths[col - 1]
        return paths[-1]

results matching ""

    No results matching ""