Top 150 InterviewMedium

Unique Paths

Detailed guide and Python implementation for the 'Unique Paths' problem.

Problem Statement

Medium

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

Write a function uniquePaths(m: int, n: int) -> int.

Constraints
  • 1 <= m, n <= 100

Examples

Example 1
Input
m = 3, n = 7
Output
28
Explanation

There are 28 unique paths to go from top-left to bottom-right.

Example 2
Input
m = 3, n = 2
Output
3
Explanation

From the top-left corner, there are 3 ways: Right -> Down -> Down, Down -> Down -> Right, or Down -> Right -> Down.

Need a Hint?
Consider using 2D DP-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.