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.