migrate 392 questions to pattern format
This commit is contained in:
@@ -8,8 +8,10 @@ categories:
|
||||
- sorting
|
||||
- heap
|
||||
patterns:
|
||||
- heap
|
||||
- binary-search
|
||||
- slug: heap
|
||||
is_optimal: true
|
||||
- slug: binary-search
|
||||
is_optimal: false
|
||||
|
||||
function_signature: "def find_kth_largest(nums: list[int], k: int) -> int:"
|
||||
|
||||
@@ -144,6 +146,31 @@ explanation:
|
||||
time_complexity: "O(n log k). We iterate through all `n` elements, and each heap operation (push/pop) takes `O(log k)` time since the heap size is bounded by `k`."
|
||||
space_complexity: "O(k). We maintain a heap containing at most `k` elements."
|
||||
|
||||
pattern_comparison: |
|
||||
**Heap vs Quickselect: Choosing the Right Pattern**
|
||||
|
||||
Both approaches avoid full sorting, but they have different characteristics:
|
||||
|
||||
| Approach | Time (Avg) | Time (Worst) | Space | Modifies Input? |
|
||||
|----------|------------|--------------|-------|-----------------|
|
||||
| **Min Heap** | O(n log k) | O(n log k) | O(k) | No |
|
||||
| **Quickselect** | O(n) | O(n²) | O(log n) | Yes |
|
||||
| **Sorting** | O(n log n) | O(n log n) | O(1)-O(n) | Yes |
|
||||
|
||||
**When to choose Heap:**
|
||||
- You need **guaranteed** performance (no worst-case quadratic time)
|
||||
- The input array shouldn't be modified
|
||||
- `k` is small relative to `n` (the O(log k) factor stays small)
|
||||
- You're working with streaming data
|
||||
|
||||
**When to choose Quickselect:**
|
||||
- You need the **fastest average** performance
|
||||
- Modifying the input array is acceptable
|
||||
- You're comfortable with randomised algorithms
|
||||
- Space is at a premium (O(log n) recursion vs O(k) heap)
|
||||
|
||||
**Interview tip:** Start with Heap for its simplicity and guaranteed bounds, then mention Quickselect as an optimisation if the interviewer asks about O(n) solutions.
|
||||
|
||||
solutions:
|
||||
- approach_name: Min-Heap
|
||||
is_optimal: true
|
||||
|
||||
Reference in New Issue
Block a user