1. Rolling Arrays
for i in rows:
cur = [...]
for j in cols:
cur[j] = f(prev[j], prev[j-1], cur[j-1])
prev = cur
Catalog of common dynamic programming memory reductions with examples.
for i in rows:
cur = [...]
for j in cols:
cur[j] = f(prev[j], prev[j-1], cur[j-1])
prev = cur
Represent subset / boolean states in integer bits. Iterate masks and submasks efficiently.
mask = (1<<n)-1
sub = mask
while sub:
process(sub)
sub = (sub-1) & mask
Use prefix sums to collapse an extra dimension of range-dependent transitions.
Maintain only k previous rows/columns with deque or circular buffer if transition depends on bounded window.
Some recurrences reduce to math formula (e.g., Fibonacci, sums of arithmetic/geometric progressions) removing DP entirely.