14.6 Unique Paths
14.6.1 Problem Metadata
- Platform: LeetCode
- Problem ID: 62
- Difficulty: Medium
- URL: https://leetcode.com/problems/lc-unique-paths/
- Tags: Grind 75, Blind 75, NeetCode 150
- Techniques: Dynamic Programming, Tabulation, Matrix
14.6.2 Description
A robot can move only right or down on an m x n grid from the top-left to the bottom-right. Count the number of unique paths.
14.6.5 Solution - DP Grid
14.6.5.1 Walkthrough
Paths to (i, j) equal the sum of paths to (i-1, j) and (i, j-1) since those are the only ways to enter. Initialize the first row and column to 1 because there is only one way to reach those cells.
14.6.5.2 Analysis
- Time Complexity: O(m * n)
- Space Complexity: O(m * n) (or O(n) with rolling arrays)
14.6.5.3 Implementation Steps
- Fill a DP matrix with
dp[0][*] = dp[*][0] = 1. - For each cell from
(1,1)to(m-1,n-1)computedp[i][j] = dp[i-1][j] + dp[i][j-1]. - Return
dp[m-1][n-1].