Data Structures19 sections · 729 units
Open in Course

Lessons from Longest Consecutive

Amortized thinking

What did you learn?

1.1. Hash sets turn O(n)O(n) searches into O(1)O(1) checks.

2.2. Clever iteration order avoids double-counting. Only start from sequence beginnings.

3.3. Amortized analysis: the nested loop looks O(n2)O(n^2), but each element is processed at most twice. Total is O(n)O(n). This problem shows that O(n)O(n) solutions sometimes require creative thinking, not just applying a pattern.