Dynamic Programming21 sections · 916 units
Open in Course

Binary Search on Lambda

Finding the right penalty

Define f(λ)f(\lambda) = number of segments in the optimal answer when penalty is λ\lambda. This function is monotonic: larger λ\lambda means fewer segments.

Binary search (halving the search range each step) on λ\lambda to find where f(λ)=Kf(\lambda) = K. At that penalty, the optimal solution happens to use exactly KK segments. The answer is: (penalized cost at optimal λ\lambda) K×λ- K \times \lambda. You subtract the penalty to get the true cost.