migrate 392 questions to pattern format

This commit is contained in:
2025-09-10 18:05:55 +01:00
parent 13bab63618
commit c60ae08f56
402 changed files with 1383 additions and 545 deletions

View File

@@ -8,8 +8,10 @@ categories:
- hash-tables
- heap
patterns:
- greedy
- heap
- slug: heap
is_optimal: true
- slug: greedy
is_optimal: false
function_signature: "def reorganize_string(s: str) -> str:"
@@ -133,6 +135,32 @@ explanation:
time_complexity: "O(n log k). We process each of the `n` characters, and each heap operation takes O(log k) where `k` is the number of unique characters (at most 26)."
space_complexity: "O(k). We store at most `k` unique characters in the heap and hash map, where `k <= 26` for lowercase English letters."
pattern_comparison: |
**Heap Greedy vs Odd-Even Placement: Two Valid Approaches**
Both solutions achieve optimal results through different strategies:
| Approach | Time | Space | Key Insight |
|----------|------|-------|-------------|
| **Max Heap** | O(n log k) | O(k) | Always place the most frequent available character |
| **Odd-Even** | O(n + k log k) | O(n) | Separate characters by index parity |
**Heap Greedy (Online):**
- Builds the string character by character
- Naturally handles the "don't repeat" constraint via the hold-back pattern
- More intuitive: "always pick the best available option"
**Odd-Even Placement (Batch):**
- First fills even indices (0, 2, 4...), then odd indices (1, 3, 5...)
- Characters at even indices can never be adjacent to each other
- Exploits the constraint: if max_freq ≤ (n+1)/2, even indices can hold all instances of the most frequent character
**When to prefer each:**
- **Heap**: When you need to explain the greedy logic step-by-step, or for streaming inputs
- **Odd-Even**: When you want simpler code (no heap needed), or when explaining the mathematical feasibility condition
**Both share the same core insight**: Check `max_freq ≤ (n+1)/2` first — this determines whether a solution is even possible.
solutions:
- approach_name: Max Heap (Greedy)
is_optimal: true