Dynamic Programming21 sections · 916 units
Open in Course

Book Shop - State Design

Max pages for budget

Define dp[j]dp[j] = maximum pages achievable with budget jj. This is the space-improved 1D (one-dimensional) version. For each book with price pp and pages vv: if we buy it, dp[j]dp[j] = max(dp[j]dp[j], dp[jp]dp[j-p] + vv) for all jjpp.

Process books one by one. Iterate budget in reverse (jj from xx down to pp) to avoid using the same book twice. The state tracks both money spent and which books you have considered. Two dimensions are needed.