Dynamic Programming21 sections · 916 units
Open in Course

Pattern - Convex Hull Trick

Recognition

Recognize CHT when:

1.1. Your DP has dp[i]=minj(f(j)g(i)+h(j))dp[i] = \min_j(f(j) \cdot g(i) + h(j)) where ff, gg, hh depend only on their argument.

2.2. The cost "mixes" ii and jj through a product term. Look for terms like aibja_i \cdot b_j in the recurrence.

3.3. You can rewrite as minj(mjxi+cj)\min_j(m_j \cdot x_i + c_j). Then check: Slopes mjm_j monotonic? Use deque for inserts. Queries xix_i monotonic? Use deque for queries. Neither sorted? Use Li Chao tree instead.