Dynamic Programming21 sections · 916 units
Open in Course

D&C Recursion - Walkthrough

Tracing the divide and conquer

Compute dp[i]dp[i] for i[0,7]i \in [0, 7]. Optimal splits are monotonic. Start with range [0,7][0, 7]. Compute dp[3]dp[3] (midpoint). Find opt[3]opt[3] by trying all valid splits. Say opt[3]=5opt[3] = 5. Recurse on [0,2][0, 2] with constraint opt[0,5]opt \in [0, 5]. Recurse on [4,7][4, 7] with constraint opt[5,n]opt \in [5, n].

Each level does O(n)O(n) work total (all ranges at that level partition the search space). O(logn)O(\log n) levels gives O(nlogn)O(n \log n).