Dynamic Programming21 sections · 916 units
Open in Course

Counting Tilings - Walkthrough

Tracing the classic problem

Tile an n×mn \times m grid with 1×21 \times 2 dominoes. How many ways? dp[row][mask]dp[row][mask] = number of ways to fill rows 0..row10..row-1 completely, with current row having profile maskmask. Base: dp[0][0]=1dp[0][0] = 1 (empty grid, nothing filled yet). Transition: for each mask, try all ways to fill the row.

Answer: dp[n][0]dp[n][0] (all rows filled, final profile is empty). Time: O(n2m2m)O(n \cdot 2^m \cdot 2^m)

Time complexity: O(n4m)O(n \cdot 4^m).

Space complexity: O(2m)O(2^m) with rolling array, or O(n2m)O(n \cdot 2^m) without.