Quy luật quen thuộc: muốn nhanh hơn thì thường phải tốn thêm bộ nhớ, và ngược lại.
Ví dụ điển hình là HashMap — đổi O(n) space để biến lookup từ O(n) xuống O(1), giúp Two Sum từ O(n²) còn O(n).
The familiar rule: going faster usually costs more memory, and vice versa.
- A HashMap trades O(n) space to turn O(n) lookups into O(1), cutting Two Sum from O(n²) to O(n).
- Prefix sums spend O(n) memory so each range-sum query is O(1).
- The other way, an O(1)-space requirement forces in-place work or two pointers instead of auxiliary structures.
- State what you trade for what, and why it fits the problem constraints.