Dynamic Programming21 sections · 916 units
Open in Course

Complexity Analysis - Walkthrough

Understanding the bounds

States: O(rows2cols)O(rows \cdot 2^{cols}). Transitions per state: up to 2cols2^{cols} (all ways to fill a row). Total time: O(rows4cols)O(rows \cdot 4^{cols}).

Time complexity: O(rows4cols)O(rows \cdot 4^{cols}).

Space complexity: O(4cols)O(4^{cols}). This is exponential in colscols but linear in rowsrows. improvement: precompute valid transitions. If many transitions are invalid, the actual count is smaller. Matrix exponentiation: if rowsrows is huge, use matrix power in O(4colscolslogrows)O(4^{cols} \cdot cols \cdot \log rows). Matrix exponentiation helps when the number of rows is huge. The matrix size is the number of valid profiles.