Sub topic 8
Measuring Time and Space
This chapter assumes you know Big-O notation, can read the bottom-up and memoized path-counting code from the earlier chapters, and understand how recursive calls use the call stack.
The Core Formula
Every grid DP solution has the same cost model:
- Time = (number of states) × (work per state)
- Space = (number of stored states) + (any extra memory, such as recursion stack)
A state is one table entry, such as dp[i][j]. The work per state is the number of transitions the recurrence reads, plus the constant cost of combining them. The size of the table usually tells you most of what you need to know.
Applying It to Unique Paths
An m × n grid has m·n states. Each state adds two neighbors, the cell above and the cell to the left. That is O(1) work, so the total time is O(m·n). The full table also takes O(m·n) space.
You can check this by counting how many times the code reads a neighbor:
def unique_paths_counted(m, n):
dp = [[0] * n for _ in range(m)]
reads = 0
for i in range(m):
for j in range(n):
if i == 0 and j == 0:
dp[i][j] = 1
continue
top = dp[i - 1][j] if i > 0 else 0
left = dp[i][j - 1] if j > 0 else 0
reads += 2
dp[i][j] = top + left
return dp[m - 1][n - 1], reads
for size in (10, 20, 40):
paths, reads = unique_paths_counted(size, size)
print(size, reads)
# 10 198
# 20 798
# 40 3198When the side length doubles, the number of reads roughly quadruples. That is the pattern you expect from O(n²) growth on a square grid.
Hidden Costs in the Top-Down Version
Memoized recursion visits the same m·n states, so its time is still O(m·n). It does add two costs that the table version does not have:
- Stack depth. The longest chain of calls runs from
(m-1, n-1)to the base case, which takes O(m + n) frames. Python's default recursion limit is about 1000, so a 600 × 600 grid can raiseRecursionError. - Cache overhead.
lru_cachekeeps a hash entry for every state. The space is still O(m·n), but each entry costs much more memory than a slot in a list.
Comparing Problems in This Tutorial
| Problem | States | Work per state | Time | Space (full table) |
|---|---|---|---|---|
| Unique paths | m·n | 2 reads | O(m·n) | O(m·n) |
| Minimum-cost path | m·n | 2 reads + min | O(m·n) | O(m·n) |
| Maximal square | m·n | 3 reads + min | O(m·n) | O(m·n) |
| Edit distance | (m+1)(n+1) | 3 reads | O(m·n) | O(m·n) |
| Cherry pickup (two walkers) | about n³ | 4 combinations | O(n³) | O(n³) |
The last row shows why the state definition matters most. When the state has to track two walkers, its dimensions multiply, and the cost grows by a whole factor of n.
Where Space Can Be Cut
Time is set by the number of states you must evaluate. Space only has to cover the states that later states still need. If each row reads only the row above it, you never need more than two rows at once. A later chapter turns that observation into an O(n) solution.
Exercise
A variant of unique paths allows three moves into each cell: from above, from the left, and diagonally from the upper-left. What are the time and space complexities of a full-table bottom-up solution on an m × n grid, and what is the recursion depth of a memoized top-down version?
Show answer
There are still m·n states, and each one does 3 reads, which is O(1) work. Time is O(m·n) and space is O(m·n). The deepest recursive chain uses only up or left moves, so the stack depth is O(m + n). Diagonal moves shorten some chains but never make the longest one longer.
Exercise
Doubling both dimensions of a grid multiplies the running time of an O(m·n) DP by what factor?
Show answer
By 4, because (2m)(2n) = 4·m·n.