Unique Paths II
LeetCode 63 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
Obstacle cell ⇒ dp = 0; otherwise dp = left + up.
ನಾನು Unique Paths II (LeetCode 63) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Grid DP ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Same counting rule as Unique Paths — ways into a cell = ways from the left + ways from above — except a cell with an obstacle contributes zero ways, which naturally blocks every path through it.
Approach
- 1
If the start cell is blocked, there are no paths at all.
- 2
Seed dp[0][0] = 1.
- 3
For each cell: 0 if it's an obstacle, else the sum of the left and upper neighbours (0 when off-grid).
- 4
Return dp[m-1][n-1].
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ
lru_cache dfs(row, col) returning 0 on obstacles.
dp grid with obstacles zeroed.
Recursion + memoPythonO(m·n) time · O(m·n) space
from functools import lru_cache
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
if not obstacleGrid or not obstacleGrid[0]:
return 0
if obstacleGrid[0][0]==1:
return 0
m = len(obstacleGrid)
n = len(obstacleGrid[0])
@lru_cache(maxsize=None)
def dfs(row,col):
if row==0 and col==0:
return 1
if row<0 or col<0:
return 0
if obstacleGrid[row][col]==1:
return 0
option1 = dfs(row,col-1)
option2 = dfs(row-1,col)
return option1+option2
return dfs(m-1,n-1)TabulationPythonO(m·n) time · O(m·n) space
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
if not obstacleGrid or not obstacleGrid[0]:
return 0
if obstacleGrid[0][0]==1:
return 0
m = len(obstacleGrid)
n = len(obstacleGrid[0])
dp = [[0]*n for _ in range(m)]
dp[0][0]=1 if obstacleGrid[0][0]!=1 else 0
for row in range(m):
for col in range(n):
if row==0 and col==0:
continue
if obstacleGrid[row][col]==1:
dp[row][col]=0
else:
option1 = dp[row][col-1] if col>0 else 0
option2 = dp[row-1][col] if row>0 else 0
dp[row][col]=option1+option2
return dp[m-1][n-1]
sol = Solution()
assert sol.uniquePathsWithObstacles([])==0
assert sol.uniquePathsWithObstacles([[]])==0
assert sol.uniquePathsWithObstacles([[1]])==0
assert sol.uniquePathsWithObstacles([[0]])==1Unique Paths II — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Unique Paths II ಹಿಂದಿನ intuition ಏನು?
Same counting rule as Unique Paths — ways into a cell = ways from the left + ways from above — except a cell with an obstacle contributes zero ways, which naturally blocks every path through it.
Unique Paths II (LeetCode 63) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. If the start cell is blocked, there are no paths at all. 2. Seed dp[0][0] = 1. 3. For each cell: 0 if it's an obstacle, else the sum of the left and upper neighbours (0 when off-grid). 4. Return dp[m-1][n-1].
Unique Paths II ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(m · n). Space: O(m · n). (Accepted approach: Tabulation.)
Unique Paths II ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
A blocked start or finish must return 0. Don't blindly set the whole first row to 1 — an obstacle cuts everything after it.
Unique Paths II ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?
Recursion + memo (O(m·n) time · O(m·n) space): lru_cache dfs(row, col) returning 0 on obstacles. Tabulation (O(m·n) time · O(m·n) space): dp grid with obstacles zeroed.