Dynamic Programming21 sections · 916 units
Open in Course

Why O(n) Total

Amortized analysis

Each line is added exactly once and removed at most once. That's O(n)O(n) add operations, O(n)O(n) remove operations. Adding: You might pop multiple lines when adding one, but total pops across all adds is bounded by total lines ever added. That's O(n)O(n). Querying: Same logic.

Each front-pop happens at most once per line. Total query overhead: O(n)O(n). Combined: O(n)O(n) time for nn states. Your O(n2)O(n^2) DP becomes O(n)O(n) because we do O(n)O(n) work total, not O(n)O(n) work per state. That's the power of CHT.