Dynamic Programming21 sections · 916 units
Open in Course

Intro

Removing the K dimension

Now I'll show you one of the cleverest DP (dynamic programming) tricks: the Aliens Trick, also called WQS Binary Search (binary search on a penalty parameter) or Lagrangian Relaxation. You'll face problems where you must pick exactly KK items. The obvious DP has a dimension for count: dp[i][k]dp[i][k].

When KK is large, this is too slow. The Aliens Trick eliminates the count dimension entirely. By the end, you'll solve "exactly KK" problems without tracking KK in your DP state (the subproblem parameters). We'll work through four classic problems together.