Dynamic Programming21 sections · 916 units
Open in Course

Common Mistakes

What to avoid

1.1. Forgetting the double-push. When you pop from the heap, you must push the current element again to maintain slope counts. Without this, your function breaks.

2.2. Using the wrong heap type. Non-decreasing arrays need max-heap. Non-increasing arrays need min-heap. The heap type determines which slope-change points you track.

3.3. Assuming convexity without checking. Slope Trick only works for convex functions. If your cost function isn't convex, the heap approach fails.