Continue learning
Practice related DSA questions in the same topic.
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.
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.
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)]| 0 • | 0 | 0 |
| 0 | 0 | 0 |
| 0 | 0 | 0 |
A blocked cell receives no routes.
A blocked cell receives no routes.
Count routes between city intersections while avoiding closed streets.
Each open cell can be reached from its upper and left neighbours; add those counts.
O(rows × columns) time · O(rows × columns) auxiliary space
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
A blocked starting cell must contribute zero ways.
How could you keep only one row of counts?