migrate 392 questions to pattern format

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

View File

@@ -8,8 +8,10 @@ categories:
- hash-tables
- heap
patterns:
- greedy
- heap
- slug: greedy
is_optimal: true
- slug: heap
is_optimal: false
function_signature: "def least_interval(tasks: list[str], n: int) -> int:"
@@ -153,6 +155,30 @@ explanation:
time_complexity: "O(n). We iterate through the tasks once to count frequencies, then compute the result in constant time."
space_complexity: "O(1). We use a fixed-size counter (at most 26 letters), which is constant regardless of input size."
pattern_comparison: |
**Math Formula vs Heap Simulation: Why Greedy Wins**
Both approaches produce correct answers, but with vastly different performance:
| Approach | Time | Space | Complexity |
|----------|------|-------|------------|
| **Greedy (Math)** | O(n) | O(1) | Simple counting + formula |
| **Heap Simulation** | O(n × m) | O(26) | Simulates actual schedule |
Where `n` = number of tasks and `m` = cooldown period.
**Why the math formula is superior:**
- **Insight over simulation**: The formula captures the *essence* of the problem — the most frequent task dictates the schedule structure
- **Constant-time calculation**: After counting (O(n)), the formula runs in O(1)
- **No edge cases in execution**: The heap simulation has tricky termination conditions
**When Heap Simulation is useful:**
- When you need the **actual schedule** (not just the count)
- For variations where the greedy insight doesn't apply (e.g., task dependencies)
- As a verification tool to validate your math solution
**Interview strategy**: Mention both approaches. The math solution shows algorithmic insight; the heap shows you can handle complex state. Start with heap if unsure, optimise to math if time permits.
solutions:
- approach_name: Greedy (Math Formula)
is_optimal: true