Tensorum
YouTubeसीखना शुरू करें
LeetCode 62 · Grid DP

Unique Paths

LeetCode 62 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

paths(i, j) = paths(i-1, j) + paths(i, j-1) — or simply C(m+n-2, n-1).

मध्यमMathDynamic ProgrammingCombinatorics

मैंने Unique Paths (LeetCode 62) ऐसे हल किया — Grid DP पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Every cell can be entered from the left or from above, so the number of routes into it is the routes into the left cell plus the routes into the cell above. Cells in the first row or column have exactly one route. There is also a counting view: every path is m-1 downs and n-1 rights in some order.

Approach

  1. 1

    Top-down: dfs(i, j) with first row/column returning 1, cached with lru_cache.

  2. 2

    Bottom-up: set the first row and column to 1, then fill the rest with the sum rule.

  3. 3

    Combinatorics: choose which n-1 of the m+n-2 moves are 'right' — compute nCr incrementally to avoid huge factorials.

मैंने कैसे हल किया · पहले विचार से अंतिम हल तक

1
Recursion + memoO(m·n) time · O(m·n) space

Sum of the two incoming directions, cached.

2
TabulationO(m·n) time · O(m·n) space

Fill the grid from the first row and column.

3
Combinatorics nCrAcceptedO(n) time · O(1) space

Count arrangements of downs and rights.

Recursion + memoPythonO(m·n) time · O(m·n) space
from functools import lru_cache
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        @lru_cache(maxsize=None)
        def dfs(i,j):
            if i==0 or j==0:
                return 1
            if i<0 or j<0:
                return 0
            return dfs(i-1,j)+dfs(i,j-1)
        return dfs(m-1,n-1)
TabulationPythonO(m·n) time · O(m·n) space
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp=[[0]*n for _ in range(m)]
        for i in range(m-1,-1,-1):
            dp[i][0]=1
        for j in range(n-1,-1,-1):
            dp[0][j]=1
        for i in range(1,m):
            for j in range(1,n):
                dp[i][j]=dp[i-1][j]+dp[i][j-1]
        return dp[m-1][n-1]
Combinatorics nCrPythonO(n) time · O(1) space
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        def factorial(n,r):
            ans = 1
            for i in range(r):
                ans=ans*(n-i)
                ans=ans//(i+1)
            return ans
        return factorial(m+n-2,n-1)
स्टेप-थ्रू लोड हो रहा है…

Unique Paths — अक्सर पूछे जाने वाले प्रश्न

Unique Paths के पीछे intuition क्या है?

Every cell can be entered from the left or from above, so the number of routes into it is the routes into the left cell plus the routes into the cell above. Cells in the first row or column have exactly one route. There is also a counting view: every path is m-1 downs and n-1 rights in some order.

Unique Paths (LeetCode 62) कैसे हल करें?

1. Top-down: dfs(i, j) with first row/column returning 1, cached with lru_cache. 2. Bottom-up: set the first row and column to 1, then fill the rest with the sum rule. 3. Combinatorics: choose which n-1 of the m+n-2 moves are 'right' — compute nCr incrementally to avoid huge factorials.

Unique Paths की time और space complexity क्या है?

Time: O(min(m, n)) for the formula · O(m·n) for the DP। Space: O(1) for the formula। (Accepted approach: Combinatorics nCr।)

Unique Paths में आम गलतियाँ क्या हैं?

In the memo version the i == 0 or j == 0 check must come before the negative check (it does). Multiply then integer-divide at each step so the running value stays an exact integer.

क्या Unique Paths को हल करने के एक से ज़्यादा तरीके हैं?

Recursion + memo (O(m·n) time · O(m·n) space): Sum of the two incoming directions, cached. Tabulation (O(m·n) time · O(m·n) space): Fill the grid from the first row and column. Combinatorics nCr (O(n) time · O(1) space): Count arrangements of downs and rights.