
Problem
Solution
Submissions
Unique Paths in a Grid
Certification: Intermediate Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C program to find the number of unique paths from the top-left corner to the bottom-right corner of an m×n grid. You can only move either down or right at any point in time.
Example 1
- Input: m = 3, n = 2
- Output: 3
- Explanation:
- Step 1: In a 3×2 grid, we need to go from (0,0) to (2,1).
- Step 2: The possible paths are:
- Right → Right → Down
- Right → Down → Right
- Down → Right → Right
- Step 3: Therefore, there are 3 unique paths.
Example 2
- Input: m = 7, n = 3
- Output: 28
- Explanation:
- Step 1: In a 7×3 grid, we need to go from (0,0) to (6,2).
- Step 2: We need to make exactly 6 right moves and 2 down moves.
- Step 3: The number of ways to arrange these moves is given by the combination formula C(8,2) = 28.
- Step 4: Therefore, there are 28 unique paths.
Constraints
- 1 <= m, n <= 100
- The answer will be less than or equal to 2 * 10^9
- Time Complexity: O(m*n)
- Space Complexity: O(m*n) or O(min(m,n))
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- This is a classic dynamic programming problem.
- Create a 2D array to store the number of ways to reach each cell.
- The number of ways to reach a cell is the sum of ways to reach the cell above it and the cell to its left.
- Base case: There is exactly 1 way to reach any cell in the first row or first column.
- Build the solution bottom-up and return the value at the bottom-right cell.