Dynamic Programming21 sections · 916 units
Open in Course

Implementation Checklist

Avoiding common bugs

Checklist 11: Verify convexity. Test small cases manually. If gg(kk) isn't convex, the trick fails. Checklist 22: Handle lambda precision. Use integer arithmetic if possible. Binary search on integers is cleaner. Checklist 33: Track count correctly. Pair (cost, count) comparisons must be consistent. Checklist 44: Final answer. After finding optimal lambda, compute actual answer: costKλcost - K \cdot \lambda. Test on small inputs where you can verify the answer manually. Check that the count varies correctly with penalty.

Time complexity: O(nlogC)O(n \log C) where CC is the penalty search range, times the cost of the inner DP.

Space complexity: O(n)O(n) for the DP arrays.