Dynamic Programming21 sections · 916 units
Open in Course

The D&C Recursion

Step by step

Define solve(l,r,optL,optR)solve(l, r, optL, optR): compute dp[l..r]dp[l..r] knowing optimal splits are in [optL,optR][optL, optR].

1.1. Base case: if l>rl > r, return.

2.2. Compute mid=(l+r)/2mid = (l + r) / 2.

3.3. Find dp[mid]dp[mid] by trying all j[optL,min(optR,mid1)]j \in [optL, \min(optR, mid-1)]. Track which jj was best as opt[mid]opt[mid].

4.4. Recurse: solve(l,mid1,optL,opt[mid])solve(l, mid-1, optL, opt[mid]) and solve(mid+1,r,opt[mid],optR)solve(mid+1, r, opt[mid], optR).