Dynamic Programming21 sections · 916 units
Open in Course

What Is D&C Optimization?

Core idea

You want to compute dp[1],dp[2],,dp[n]dp[1], dp[2], \ldots, dp[n] where Base case: dp[0]=0dp[0] = 0. For i1i \geq 1: dp[i]=minj<i(dp[j]+cost(j,i))dp[i] = \min_{j < i}(dp[j] + cost(j,i)). Naively, each takes O(n)O(n) time.

D&C improvement computes the middle element first: dp[mid]dp[mid]. Finding opt[mid]opt[mid] takes O(n)O(n) time.

But now you know: for all i<midi < mid, search only jopt[mid]j \leq opt[mid]. For all i>midi > mid, search only jopt[mid]j \geq opt[mid]. Recurse on both halves.

Each level of recursion does O(n)O(n) total work. Picture a binary tree: at the root Compute dp[n/2]dp[n/2], its children compute dp[n/4]dp[n/4] and dp[3n/4]dp[3n/4]. Each level handles O(n)O(n) work total across all nodes. With O(logn)O(\log n) levels, total is O(nlogn)O(n \log n).