Skip to content

Commit 94534d0

Browse files
authored
Merge pull request #75 from lufftw/review/pattern-tree-dp
review: Complete pattern + solutions audit (24/24 patterns)
2 parents 27bda49 + 9122acb commit 94534d0

14 files changed

+1128
-395
lines changed

docs/contracts/solution-contract.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ Constraints:
227227

228228
Each solution class SHOULD be preceded by a block comment explaining the approach.
229229

230+
**Keep it concise**: Block comments should have **3-4 bullet points maximum**. Focus on key algorithmic insights, not exhaustive explanations.
231+
230232
**No blank line** between the comment block and the class definition:
231233

232234
```python
@@ -247,8 +249,9 @@ class SolutionSlidingWindow: # ← No blank line
247249
# ============================================
248250
# Solution {N}: {Approach Name}
249251
# Time: O(?), Space: O(?)
250-
# - {Key insight or implementation detail}
251-
# - {Additional notes}
252+
# - {Key insight 1}
253+
# - {Key insight 2}
254+
# - {Key insight 3 (optional)}
252255
# ============================================
253256
class ClassName: # ← No blank line before class/function
254257
```
@@ -257,9 +260,56 @@ class ClassName: # ← No blank line before class/function
257260
|-----------|----------|-------------|
258261
| Solution number & name || e.g., `Solution 1: Sliding Window` |
259262
| Time/Space complexity || e.g., `Time: O(n), Space: O(n)` |
260-
| Bullet points | Recommended | Key insights, implementation details |
263+
| Bullet points (3-4 max) | | Key insights only, avoid verbose explanations |
261264
| **No blank line** || Comment block directly followed by class/function |
262265

266+
**What NOT to include in block comments:**
267+
268+
- ❌ Lengthy "Algorithm Insight" or "State Definition" sections
269+
- ❌ Detailed step-by-step pseudocode
270+
- ❌ Redundant restating of code logic
271+
- ❌ More than 4 bullet points
272+
273+
#### Internal Function Comments
274+
275+
Internal comments **within methods** are acceptable and encouraged for documenting:
276+
277+
- Key state transitions
278+
- Non-obvious logic or edge case handling
279+
- Invariant maintenance
280+
281+
```python
282+
class Solution:
283+
def maxPathSum(self, root: Optional[TreeNode]) -> int:
284+
self.global_max = float('-inf')
285+
286+
def max_contribution(node: Optional[TreeNode]) -> int:
287+
if not node:
288+
return 0
289+
290+
# Prune negative branches - they can only decrease path sum
291+
left_gain = max(0, max_contribution(node.left))
292+
right_gain = max(0, max_contribution(node.right))
293+
294+
# Path through this node as apex: left → node → right
295+
path_through_here = node.val + left_gain + right_gain
296+
self.global_max = max(self.global_max, path_through_here)
297+
298+
# Return single-branch max to parent (can't fork upward)
299+
return node.val + max(left_gain, right_gain)
300+
301+
max_contribution(root)
302+
return self.global_max
303+
```
304+
305+
**Internal comment guidelines:**
306+
307+
| ✅ Good | ❌ Bad |
308+
|---------|--------|
309+
| Explains WHY (non-obvious reasoning) | Restates WHAT the code does |
310+
| Documents invariant or constraint | Comments obvious operations |
311+
| Marks key algorithmic transitions | Excessive line-by-line comments |
312+
263313
---
264314

265315
## SOLUTIONS Metadata

docs/guides/pattern-review.md

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ This guide defines the systematic review process for pattern documentation in th
1616
- [3. Review Scope](#3-review-scope)
1717
- [4. Review Criteria](#4-review-criteria)
1818
- [5. Review Process](#5-review-process)
19-
- [6. Recording Reviews](#6-recording-reviews)
20-
- [7. Branch and Commit Protocol](#7-branch-and-commit-protocol)
21-
- [8. Quality Tiers](#8-quality-tiers)
19+
- [6. Combined Pattern + Solutions Review Workflow](#6-combined-pattern--solutions-review-workflow)
20+
- [7. Recording Reviews](#7-recording-reviews)
21+
- [8. Branch and Commit Protocol](#8-branch-and-commit-protocol)
22+
- [9. Quality Tiers](#9-quality-tiers)
2223

2324
---
2425

@@ -201,17 +202,72 @@ For each review checkpoint:
201202

202203
---
203204

204-
## 6. Recording Reviews
205+
## 6. Combined Pattern + Solutions Review Workflow
205206

206-
### 6.1 Review Log Location
207+
### 6.1 Context-Driven Solutions Audit
208+
209+
When reviewing pattern templates, **simultaneously audit the corresponding solutions**. Reading `templates.md` first provides the algorithmic context needed to improve solution quality.
210+
211+
**Workflow:**
212+
213+
```
214+
1. Read docs/patterns/{pattern}/templates.md completely
215+
2. For each problem referenced in templates.md:
216+
a. Read the corresponding solutions/{problem}.py
217+
b. Apply dual-perspective quality criteria (see 6.2)
218+
c. Improve comments and naming with pattern context
219+
3. Record both template and solution findings in pattern-review-log.md
220+
```
221+
222+
**Why Context Matters:**
223+
224+
| Without Context | With Context |
225+
|-----------------|--------------|
226+
| Generic variable names like `l`, `r`, `res` | Semantic names like `left_boundary`, `right_exclusive`, `max_contribution` |
227+
| Vague comments "loop through array" | Precise comments "extend window until constraint violated" |
228+
| Missing invariant documentation | Explicit invariant: "dp[i] represents optimal cost to reach position i" |
229+
230+
### 6.2 Dual-Perspective Quality Criteria
231+
232+
Solutions should satisfy both **pedagogical clarity** (teaching effectiveness) and **engineering quality** (production maintainability).
233+
234+
| Aspect | Pedagogical (Professor) | Engineering (Tech Lead) |
235+
|--------|------------------------|-------------------------|
236+
| **Comments** | Explain WHY the approach works | Keep concise, avoid redundancy |
237+
| **Naming** | Self-documenting algorithm intent | Consistent, refactorable |
238+
| **Structure** | Progressive logic flow | Minimal coupling, clean interfaces |
239+
| **Complexity** | Explicit analysis with reasoning | Practical performance notes |
240+
241+
**Quality Fusion Guidelines:**
242+
243+
1. **Block comments**: Concise 3-4 bullet points (see Solution Contract)
244+
2. **Internal comments**: Document key transitions and non-obvious logic
245+
3. **Variable naming**: Semantic names reflecting algorithmic role
246+
4. **State documentation**: Explicit invariants where applicable
247+
248+
### 6.3 Solutions Audit Checklist
249+
250+
For each solution file touched during pattern review:
251+
252+
- [ ] Block comment follows concise format (3-4 bullet points)
253+
- [ ] Variable names are semantic and consistent with pattern vocabulary
254+
- [ ] Key algorithmic transitions are documented inline
255+
- [ ] State/invariant is explicit where applicable
256+
- [ ] No redundant comments that merely restate the code
257+
258+
---
259+
260+
## 7. Recording Reviews
261+
262+
### 7.1 Review Log Location
207263

208264
All review findings must be recorded in:
209265

210266
```
211267
docs/reviews/pattern-review-log.md
212268
```
213269

214-
### 6.2 Log Entry Format
270+
### 7.2 Log Entry Format
215271

216272
```markdown
217273
## [Pattern Name] Review - [Date]
@@ -253,24 +309,24 @@ docs/reviews/pattern-review-log.md
253309
- [ ] {Documentation update needed}
254310
```
255311

256-
### 6.3 What NOT to Record
312+
### 7.3 What NOT to Record
257313

258314
- Changes made without issues (silent fixes)
259315
- Personal preferences overridden
260316
- Temporary debugging observations
261317

262318
---
263319

264-
## 7. Branch and Commit Protocol
320+
## 8. Branch and Commit Protocol
265321

266-
### 7.1 Branch Naming
322+
### 8.1 Branch Naming
267323

268324
```
269325
review/pattern-{pattern_name}
270326
review/pattern-quality-audit-{date}
271327
```
272328

273-
### 7.2 Commit Structure
329+
### 8.2 Commit Structure
274330

275331
```bash
276332
# Review log commit
@@ -292,7 +348,7 @@ Impact: {What this fixes}
292348
Refs: docs/reviews/pattern-review-log.md#{pattern}-{date}"
293349
```
294350

295-
### 7.3 PR Requirements
351+
### 8.3 PR Requirements
296352

297353
Each review PR must include:
298354

@@ -306,9 +362,9 @@ Each review PR must include:
306362

307363
---
308364

309-
## 8. Quality Tiers
365+
## 9. Quality Tiers
310366

311-
### 8.1 Tier Definitions
367+
### 9.1 Tier Definitions
312368

313369
| Tier | Criteria | Examples |
314370
|------|----------|----------|
@@ -317,15 +373,15 @@ Each review PR must include:
317373
| **Tier 3 (Bronze)** | Major issues, needs improvement | Newly generated patterns |
318374
| **Tier 4 (Draft)** | Critical issues or incomplete | Work in progress |
319375

320-
### 8.2 Promotion Criteria
376+
### 9.2 Promotion Criteria
321377

322378
```
323379
Tier 4 → Tier 3: All critical issues resolved
324380
Tier 3 → Tier 2: All major issues resolved
325381
Tier 2 → Tier 1: Peer-reviewed, community validated
326382
```
327383

328-
### 8.3 Current Pattern Status
384+
### 9.3 Current Pattern Status
329385

330386
| Pattern | Current Tier | Last Review |
331387
|---------|--------------|-------------|

0 commit comments

Comments
 (0)