‹ DS&A interview · Socratic
DSA · Dynamic Programming · #181

Unique Paths II

Module 47 · difficulty 3/5·30:00starts on first keystroke

A robot is located at the top-left corner of an `m x n` grid. It can only move **right** or **down** at any point in time, and is trying to reach the bottom-right corner. Some cells contain an obstacle. Given `obstacleGrid` where `obstacleGrid[i][j]` is `1` if cell `(i, j)` has an obstacle and `0` otherwise, return the number of unique paths from the top-left to the bottom-right corner. The robot cannot enter a cell containing an obstacle. Implement `uniquePathsWithObstacles(obstacleGrid)` returning the integer count of unique paths.

Examples
  • obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] 2The obstacle in the middle forces the two paths around it.
  • obstacleGrid = [[0,1],[0,0]] 1Only one route: down then right.
  • obstacleGrid = [[1]] 0The start cell itself is blocked, so there are no paths.
Constraints
  • · m == obstacleGrid.length
  • · n == obstacleGrid[0].length
  • · 1 <= m, n <= 100
  • · obstacleGrid[i][j] is 0 or 1
  • · The answer fits within a 32-bit signed integer.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.