62. Unique Paths
Problem 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?
Solution:
Naive solution. Use a 2D array, initialize it with 0th row and 0th column to 1(because only one way to reach).
1 | class Solution { |
time complexity: $O(mn)$
space complexity: $O(mn)$
Solution 2:
Optimized the space usage:
1 | class Solution { |
time complexity: $O(m*n)$
space complexity: $O(n)$
reference: