Workspace Unique Paths
0d
DSA Problem Sheet

Unique Paths Around Blocks

Count paths from top left to bottom right through a rectangular grid. Move only right or down. A one marks a blocked cell, zero marks an open cell. Target gives column count.

EXECUTION LAB

Count paths from top left to bottom right through a rectangular grid. Move only right or down. A one marks a blocked cell, zero marks an open cell. Target gives column count.

Input and constraints
  • Values must be 0 or 1.
  • Target is the number of columns. Values must fill a nonempty rectangle.
  • Use at most 32 integers. Each numeric input is between -1000 and 1000.

Code inspector

def solve(values, target):
dp = [0] * (len(values))
i = 0
while (i < len(values)):
if (values[i] == 0):
if (i == 0):
dp[i] = 1
else:
if (i >= target):
dp[i] = dp[(i - target)]
if ((i % target) > 0):
dp[i] = (dp[i] + dp[(i - 1)])
i = (i + 1)
return dp[(len(values) - 1)]

Visualizer

Step 1 of 19
Matrix state
0 •00
000
000

A blocked cell receives no routes

A blocked cell receives no routes.

Variable watch

i 0

Step explanation

A blocked cell receives no routes.

comparisons 1reads 0writes 0swaps 0recursiveCalls 0
Educational simulation · O(rows × columns) time · O(rows × columns) space

Worked explanation

Count routes between city intersections while avoiding closed streets.

Guided solution

Each open cell can be reached from its upper and left neighbours; add those counts.

O(rows × columns) time · O(rows × columns) auxiliary space

Examples

Worked example · {"values":[0,0,0,0,1,0,0,0,0],"target":3} ⇒ 2

Edge case 1 · {"values":[1],"target":1} ⇒ 0

Edge case 2 · {"values":[0],"target":1} ⇒ 1

Progressive hints

Hint 1Count routes between city intersections while avoiding closed streets.
Hint 2Each open cell can be reached from its upper and left neighbours; add those counts.
Hint 3Step through the worked example and inspect the highlighted variables before each update.

Common mistake

A blocked starting cell must contribute zero ways.

Interview follow-up

How could you keep only one row of counts?