Dynamic Programming21 sections · 916 units
Open in Course

Server Allocation - Implementation

Computing costs efficiently

Precompute prefix sums S[i]=t=0i1wtS[i] = \sum_{t=0}^{i-1} w_t. Cost of [l,r]=(S[r+1]S[l])2[l, r] = (S[r+1] - S[l])^2. D&C recursion: for layer jj, compute all dp[j][i]dp[j][i] using the monotonicity of optimal splits. Base: dp[1][i]=(S[i])2dp[1][i] = (S[i])^2 (all tasks on one server). Fill layers 2 to kk. Final answer: dp[k][n]dp[k][n]. Time: O(knlogn)O(kn \log n). Space: O(n)O(n) if we only keep two layers. Keep only two layers in memory since each layer only depends on the previous. This saves space.

Time complexity: O(knlogn)O(kn \log n).

Space complexity: O(n)O(n) using two-layer rolling array.