Dynamic Programming21 sections · 916 units
Open in Course

When to Use Aliens Trick

Pattern recognition

Look for these signs that Aliens Trick applies:

1.1. "Exactly KK" constraint in the problem statement (not "at most KK").

2.2. Standard DP would require dp[i][k]dp[i][k] with large kk dimension, making it too slow.

3.3. It's a partition, grouping, or selection problem where you're choosing how to divide things.

4.4. Convex cost structure: adding one more item/segment gives diminishing returns. If you see all four patterns, try the Aliens Trick. It's rare but transforms problems from O(nK)O(n \cdot K) to O(nlogC)O(n \log C), making otherwise impossible problems solvable.