DSA Patterns (Multi-Lang)•Part 1: Intro, Approach & Complexity Analysis (1 - 5)
Constraint-Based Pattern Recognition.
In Simple Words: Use problem constraints ($N$) to instantly deduce the required time complexity:
- $N \le 20$: Backtracking, Bitmask DP ($O(2^N)$ or $O(N!)$).
- $N \le 10^3$: $O(N^2)$ DP, Nested Loops.
- $N \le 10^5$: $O(N \log N)$ Sorting, Binary Search, Heaps, Divide & Conquer.
- $N \le 10^6$: $O(N)$ Hash Map, Sliding Window, Two Pointers, Monotonic Stack.
- $N \ge 10^9$: $O(\log N)$ Binary Search on Answer, or $O(1)$ Math.
Multi-Language SolutionsPython 3 • JavaScript • TypeScript • Java • C++
Switch tabs or choose All# O(N) Set lookup chosen when N = 10^5 to run within 20ms:
def has_duplicate(nums: list[int]) -> bool:
return len(set(nums)) != len(nums)