Dynamic Programming21 sections · 916 units
Open in Course

Tall Barn - Walkthrough

Tracing the solution

Distribute KK units among NN rooms. Each room ii has value fi(x)f_i(x) for xx units. get the highest total value. Direct DP: O(NK)O(NK) states, O(K)O(K) transition = O(NK2)O(NK^2). Too slow for large KK. Aliens: penalize each unit by λ\lambda. Now the optimal uses some number of units. Binary search on λ\lambda to hit exactly KK. With penalty, each room's optimal allocation is independent. Total time: O(NlogK)O(N \log K)

Time complexity: O(nlogC)O(n \log C). Space complexity: O(n)O(n) per binary search iteration.