Leetcode 62: Unique Paths

grid47
grid47
Exploring patterns and algorithms
Oct 31, 2024 5 min read

A gentle, glowing path winding through a serene landscape, showing multiple routes.
Solution to LeetCode 62: Unique Paths Problem

A robot starts at the top-left corner of an m x n grid and can only move right or down. Determine the total number of unique paths it can take to reach the bottom-right corner.
Problem
Approach
Steps
Complexity
Input: Two integers, m and n, representing the number of rows and columns of the grid.
Example: Input: m = 4, n = 5
Constraints:
• 1 <= m, n <= 100
Output: Return the total number of unique paths from the top-left to the bottom-right corner of the grid.
Example: Output: 35
Constraints:
• The result will always be less than or equal to 2 * 10^9.
Goal: Calculate the total number of unique paths using dynamic programming or combinatorics.
Steps:
• Define a 2D DP table where dp[i][j] represents the number of ways to reach cell (i, j).
• Initialize dp[0][j] = 1 for all columns and dp[i][0] = 1 for all rows.
• For each cell dp[i][j], calculate the value as dp[i][j] = dp[i-1][j] + dp[i][j-1].
• Return dp[m-1][n-1] as the final answer.
Goal: The grid size and number of paths must adhere to the constraints.
Steps:
• 1 <= m, n <= 100
• The result will always be <= 2 * 10^9.
Assumptions:
• The robot always starts at the top-left corner and ends at the bottom-right corner.
• The robot can only move either down or right.
Input: Input: m = 4, n = 5
Explanation: There are 35 unique paths from the top-left to the bottom-right corner for a 4x5 grid.

Input: Input: m = 2, n = 2
Explanation: There are 2 unique paths for a 2x2 grid: Right -> Down and Down -> Right.

Input: Input: m = 3, n = 4
Explanation: There are 10 unique paths from the top-left to the bottom-right corner for a 3x4 grid.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus