Dynamic Programming21 sections · 916 units
Open in Course

Grid Paths - Implementation

Complete solution

Read the grid, initialize dp[1][1], then fill row by row. For each cell, check if it's blocked before computing. Edge case: the first row can only come from the left, and the first column can only come from above. Handle boundaries carefully. Time complexity is O(n2)O(n²), space is O(n2)O(n²) but can be improved to O(O(n)) since you only need the previous row. Trace through a small example to verify your understanding before moving on.

Time complexity: O(nm)O(n \cdot m).

Space complexity: O(nm)O(n \cdot m).