Skip to content

Commit 4ab526f

Browse files
lufftwclaude
andcommitted
docs(guides): Optimize new-pattern.md based on development learnings
Improvements based on developing GridBFSMultiSource, KWayMerge, and LinkedListInPlaceReversal patterns: - Add "Quick Start: Existing Problems" section for minimal workflow - Add "Common Pitfalls" section with practical tips: - Generator output location quirk - File read-before-edit requirement - Missing api_kernels/patterns in all SOLUTIONS entries - README updates may be optional - Mindmap config is optional - Add "Writing Effective Intuition Guides" section: - Relatable analogies table - Visual trace examples - Common mistakes format - Pattern recognition signals - Structure recommendations - Update Phase 2 with generator output move instruction - Add "Verify Solutions Work" step to Phase 3 - Mark Phase 4 and 5 as optional (skip if tests exist) - Update Phase 8 checklist with required/optional items - Add new example patterns (Multi-Source BFS, K-Way Merge, Linked List Reversal) - Update Example Patterns table with commit counts Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 15c6993 commit 4ab526f

File tree

1 file changed

+224
-21
lines changed

1 file changed

+224
-21
lines changed

docs/guides/new-pattern.md

Lines changed: 224 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ This guide describes the complete workflow for developing a new algorithm patter
1313

1414
- [Overview](#overview)
1515
- [Prerequisites](#prerequisites)
16+
- [Quick Start: Existing Problems](#quick-start-existing-problems)
1617
- [Phase 1: Meta Pattern Sources](#phase-1-meta-pattern-sources)
1718
- [Phase 2: Templates and Intuition](#phase-2-templates-and-intuition)
1819
- [Phase 3: Solutions](#phase-3-solutions)
19-
- [Phase 4: Generators](#phase-4-generators)
20+
- [Phase 4: Generators (Optional)](#phase-4-generators-optional)
2021
- [Phase 5: Test Files](#phase-5-test-files)
2122
- [Phase 6: Problem Metadata](#phase-6-problem-metadata)
2223
- [Phase 7: Ontology and Roadmap](#phase-7-ontology-and-roadmap)
2324
- [Phase 8: Navigation and Integration](#phase-8-navigation-and-integration)
25+
- [Common Pitfalls](#common-pitfalls)
26+
- [Writing Effective Intuition Guides](#writing-effective-intuition-guides)
2427
- [Quick Reference Checklist](#quick-reference-checklist)
2528
- [Example Patterns](#example-patterns)
2629

@@ -123,6 +126,49 @@ Before starting, ensure you have:
123126
| Bitmask DP | LC 78, 847, 1125 | Enumeration → BFS + bitmask → Set cover |
124127
| Tree DP | LC 337, 124, 968 | Include/exclude → Path contribution → Multi-state |
125128
| Line Sweep | LC 253, 1094, 218 | Event counting → Capacity → Height tracking |
129+
| Multi-Source BFS | LC 994, 286, 542 | Propagation timing → Distance fill → Distance field |
130+
| K-Way Merge | LC 23, 21, 88 | Heap-based → Two-pointer → Backward in-place |
131+
| Linked List Reversal | LC 206, 92, 25 | Full → Segment → K-group |
132+
133+
---
134+
135+
## Quick Start: Existing Problems
136+
137+
When creating a pattern from **problems that already have solutions and tests**, you can skip several phases:
138+
139+
### Minimal Workflow (3-4 hours)
140+
141+
```
142+
Phase 1: Meta Sources ✅ Required - Create pattern documentation
143+
Phase 2: Docs ✅ Required - Generate templates, write intuition
144+
Phase 3: Solutions ⚡ Minimal - Just add api_kernels/patterns to SOLUTIONS
145+
Phase 4: Generators ⏭️ Skip - Tests already exist
146+
Phase 5: Test Files ⏭️ Skip - Tests already exist
147+
Phase 6: Problem Metadata ⚡ Minimal - Update roadmaps field, or create if missing
148+
Phase 7: Ontology/Roadmap ✅ Required - Create roadmap file
149+
Phase 8: Navigation ✅ Required - Update mkdocs.yml and docs/patterns/README.md
150+
```
151+
152+
### Example: KWayMerge Pattern
153+
154+
LC 23, 21, 88 already had solutions and tests. The workflow was:
155+
156+
1. **Phase 1-2**: Create meta sources and docs (most time spent here)
157+
2. **Phase 3**: Add metadata to existing SOLUTIONS dicts:
158+
```python
159+
SOLUTIONS = {
160+
"default": {
161+
"class": "SolutionHeap",
162+
"method": "mergeKLists",
163+
"api_kernels": ["KWayMerge"], # Added
164+
"patterns": ["merge_k_sorted_heap"], # Added
165+
},
166+
}
167+
```
168+
3. **Phase 6**: Add `roadmaps = ["k_way_merge_path"]` to existing problem metadata
169+
4. **Phase 7-8**: Create roadmap and update navigation
170+
171+
**Total commits**: 4 (vs 8 for new problems)
126172

127173
---
128174

@@ -368,7 +414,14 @@ mkdir -p docs/patterns/{pattern_name}
368414
### 2.2 Generate templates.md
369415

370416
```bash
371-
PYTHONPATH=. python tools/patterndocs/generate_pattern_docs.py --pattern {pattern_name}
417+
mkdir -p docs/patterns/{pattern_name}
418+
python tools/patterndocs/generate_pattern_docs.py --pattern {pattern_name}
419+
```
420+
421+
**Important**: The generator outputs to `docs/patterns/{pattern_name}.md` (a file), not inside a subdirectory. You need to move it:
422+
423+
```bash
424+
mv docs/patterns/{pattern_name}.md docs/patterns/{pattern_name}/templates.md
372425
```
373426

374427
### 2.3 Create intuition.md
@@ -564,7 +617,17 @@ class Solution{Approach}:
564617
pass
565618
```
566619

567-
### 3.4 Commit Phase 3
620+
### 3.4 Verify Solutions Work
621+
622+
Always run tests after creating/updating solutions:
623+
624+
```bash
625+
python runner/test_runner.py {id1}_{slug1} --all
626+
python runner/test_runner.py {id2}_{slug2} --all
627+
python runner/test_runner.py {id3}_{slug3} --all
628+
```
629+
630+
### 3.5 Commit Phase 3
568631

569632
```bash
570633
git add solutions/{id1}_*.py solutions/{id2}_*.py solutions/{id3}_*.py
@@ -578,10 +641,19 @@ git commit -m "solutions({pattern_name}): Phase 3 - Add/update solutions
578641

579642
---
580643

581-
## Phase 4: Generators
644+
## Phase 4: Generators (Optional)
582645

583646
**Purpose**: Create test generators for each problem.
584647

648+
**Skip this phase if**:
649+
- The problems already have test files in `tests/`
650+
- You're updating an existing pattern with new documentation only
651+
652+
Generators are primarily useful for:
653+
- New problems without existing tests
654+
- Stress testing with random inputs
655+
- Complexity estimation (`--estimate` flag)
656+
585657
### 4.1 Create Generator Files
586658

587659
```python
@@ -1011,6 +1083,130 @@ git branch -d feat/pattern-{pattern_name}
10111083

10121084
---
10131085

1086+
## Common Pitfalls
1087+
1088+
Based on experience developing patterns, avoid these common mistakes:
1089+
1090+
### 1. Generator Output Location
1091+
1092+
The pattern docs generator outputs to `docs/patterns/{pattern}.md`, not inside the subdirectory:
1093+
1094+
```bash
1095+
# Wrong assumption:
1096+
docs/patterns/my_pattern/templates.md # NOT created automatically
1097+
1098+
# Actual output:
1099+
docs/patterns/my_pattern.md # Created here
1100+
1101+
# Fix: Move it manually
1102+
mv docs/patterns/my_pattern.md docs/patterns/my_pattern/templates.md
1103+
```
1104+
1105+
### 2. Forgetting to Read Files Before Editing
1106+
1107+
When updating existing TOML files, always read them first to avoid "File not read" errors:
1108+
1109+
```python
1110+
# Wrong: Trying to edit without reading
1111+
Edit(file_path="meta/problems/0021.toml", old_string=..., new_string=...)
1112+
1113+
# Right: Read first, then edit
1114+
Read(file_path="meta/problems/0021.toml")
1115+
Edit(file_path="meta/problems/0021.toml", old_string=..., new_string=...)
1116+
```
1117+
1118+
### 3. Missing api_kernels/patterns in SOLUTIONS
1119+
1120+
When adding patterns to existing solutions, update ALL solution entries in SOLUTIONS dict:
1121+
1122+
```python
1123+
# Incomplete - only updated "default"
1124+
SOLUTIONS = {
1125+
"default": {
1126+
"api_kernels": ["MyKernel"], # ✅ Added
1127+
"patterns": ["my_pattern"], # ✅ Added
1128+
},
1129+
"variant": {
1130+
# ❌ Missing api_kernels and patterns!
1131+
},
1132+
}
1133+
```
1134+
1135+
### 4. README Updates May Be Optional
1136+
1137+
If the main README uses a "View All Patterns" link to `docs/patterns/README.md`, you only need to update `docs/patterns/README.md`. The main README table shows representative examples, not exhaustive listings.
1138+
1139+
### 5. Mindmap Config Update Is Optional
1140+
1141+
Updating `tools/mindmaps/ai-markmap-agent/config/config.yaml` is only needed if you want the pattern to appear in auto-generated mindmaps. It can be deferred.
1142+
1143+
---
1144+
1145+
## Writing Effective Intuition Guides
1146+
1147+
The `intuition.md` file is the most valuable documentation you'll write. Here's how to make it effective:
1148+
1149+
### Use Relatable Analogies
1150+
1151+
Each pattern benefits from a memorable mental model:
1152+
1153+
| Pattern | Effective Analogy |
1154+
|---------|-------------------|
1155+
| Multi-Source BFS | "Flashlights in a cave" - multiple light sources spreading simultaneously |
1156+
| K-Way Merge | "Racing snails" - each sequence is a snail, heap picks the leader |
1157+
| Linked List Reversal | "Train car couplers" - flip the direction of each coupler |
1158+
| Sliding Window | "Moving spotlight" - illuminate a section, slide to reveal more |
1159+
| Monotonic Stack | "Building heights" - what can you see looking left/right? |
1160+
1161+
### Include Visual Traces
1162+
1163+
ASCII art helps readers follow the algorithm step-by-step:
1164+
1165+
```
1166+
Step 0: prev=None, curr=1
1167+
None <- 1 2 -> 3 -> 4 -> None
1168+
1169+
Step 1: prev=1, curr=2
1170+
None <- 1 <- 2 3 -> 4 -> None
1171+
```
1172+
1173+
### Highlight Common Mistakes
1174+
1175+
Show both wrong and right approaches:
1176+
1177+
```python
1178+
# ❌ WRONG - loses reference to next node
1179+
curr.next = prev
1180+
curr = curr.next # Oops, curr.next is now prev!
1181+
1182+
# ✅ RIGHT - save next before modifying
1183+
next_node = curr.next
1184+
curr.next = prev
1185+
curr = next_node
1186+
```
1187+
1188+
### Add Pattern Recognition Signals
1189+
1190+
Help readers identify when to use the pattern:
1191+
1192+
```markdown
1193+
**Use this pattern when you see:**
1194+
- "Reverse" + "linked list" in problem statement
1195+
- "In-place" or "O(1) space" constraints
1196+
- "Swap adjacent pairs" or "reverse in groups"
1197+
```
1198+
1199+
### Structure Recommendations
1200+
1201+
1. **Mental Model** (1-2 paragraphs with analogy)
1202+
2. **Visual Walkthrough** (ASCII trace of key algorithm)
1203+
3. **Pattern Variants** (when to use each variant)
1204+
4. **Common Pitfalls** (mistakes to avoid)
1205+
5. **Pattern Recognition** (keywords and signals)
1206+
6. **Complexity Summary** (quick reference table)
1207+
1208+
---
1209+
10141210
## Quick Reference Checklist
10151211

10161212
### Phase 1: Meta Pattern Sources
@@ -1034,16 +1230,18 @@ git branch -d feat/pattern-{pattern_name}
10341230
- [ ] Add pattern variants to existing solutions
10351231
- [ ] Commit Phase 3
10361232

1037-
### Phase 4: Generators
1233+
### Phase 4: Generators (Skip if tests exist)
1234+
- [ ] Check if tests already exist: `ls tests/{id}_{slug}_*.in`
10381235
- [ ] Create generator files with `generate()` function
10391236
- [ ] Include edge cases
10401237
- [ ] Add `generate_for_complexity()` (optional)
10411238
- [ ] Commit Phase 4
10421239

1043-
### Phase 5: Test Files
1240+
### Phase 5: Test Files (Skip if tests exist)
1241+
- [ ] Check if tests already exist
10441242
- [ ] Create 5 input files per problem
10451243
- [ ] Generate output files by running solutions
1046-
- [ ] Verify all tests pass
1244+
- [ ] Verify all tests pass: `python runner/test_runner.py {problem} --all`
10471245
- [ ] Commit Phase 5
10481246

10491247
### Phase 6: Problem Metadata
@@ -1060,28 +1258,33 @@ git branch -d feat/pattern-{pattern_name}
10601258
- [ ] Commit Phase 7
10611259

10621260
### Phase 8: Navigation and Integration
1063-
- [ ] Update `mkdocs.yml`
1064-
- [ ] Update `tools/mindmaps/ai-markmap-agent/config/config.yaml`
1065-
- [ ] Update `README.md`
1066-
- [ ] Update `README_zh-TW.md`
1067-
- [ ] Update `docs/patterns/README.md`
1261+
- [ ] Update `mkdocs.yml` (required)
1262+
- [ ] Update `docs/patterns/README.md` (required)
1263+
- [ ] Update `tools/mindmaps/ai-markmap-agent/config/config.yaml` (optional)
1264+
- [ ] Update `README.md` (optional - only if adding to main table)
1265+
- [ ] Update `README_zh-TW.md` (optional - only if adding to main table)
10681266
- [ ] Commit Phase 8
1069-
- [ ] Merge to main
1070-
- [ ] Push to remote
1071-
- [ ] Delete feature branch
1267+
- [ ] Merge to main: `git checkout main && git merge feat/pattern-{pattern_name}`
1268+
- [ ] Push to remote: `git push origin main`
1269+
- [ ] Delete feature branch: `git branch -d feat/pattern-{pattern_name}`
10721270

10731271
---
10741272

10751273
## Example Patterns
10761274

10771275
These patterns serve as implementation references:
10781276

1079-
| Pattern | Problems | Files | Reference |
1080-
|---------|----------|-------|-----------|
1081-
| **Bitmask DP** | LC 78, 847, 1125 | 47 | Subset enumeration, BFS + bitmask, set cover |
1082-
| **Tree DP** | LC 337, 124, 968 | 47 | Include/exclude, path contribution, multi-state |
1083-
| **Line Sweep** | LC 253, 1094, 218 | 47 | Event counting, capacity, height tracking |
1084-
| **Segment Tree/Fenwick** | LC 307, 315, 327 | 47 | Range queries with updates, inversion counting |
1277+
| Pattern | Problems | Commits | Key Techniques |
1278+
|---------|----------|---------|----------------|
1279+
| **Bitmask DP** | LC 78, 847, 1125 | 8 | Subset enumeration, BFS + bitmask, set cover |
1280+
| **Tree DP** | LC 337, 124, 968 | 8 | Include/exclude, path contribution, multi-state |
1281+
| **Line Sweep** | LC 253, 1094, 218 | 8 | Event counting, capacity, height tracking |
1282+
| **Segment Tree/Fenwick** | LC 307, 315, 327 | 8 | Range queries with updates, inversion counting |
1283+
| **Multi-Source BFS** | LC 994, 286, 542 | 8 | Propagation timing, distance fill, distance field |
1284+
| **K-Way Merge** | LC 23, 21, 88 | 4 | Heap-based, two-pointer, backward merge |
1285+
| **Linked List Reversal** | LC 206, 92, 25 | 6 | Full reversal, segment, k-group |
1286+
1287+
**Note**: Patterns using existing solutions/tests (K-Way Merge) require fewer commits since Phases 4-5 are skipped.
10851288

10861289
### Directory Structure Example (Bitmask DP)
10871290

0 commit comments

Comments
 (0)