Skip to content

Commit f82331d

Browse files
lufftwclaude
andcommitted
docs(contracts): Add DP class-level comment convention
Document the convention for Dynamic Programming solutions: - Block comments: algorithm name, complexity, key insights - Class-level comments: State/Base case/Transition definitions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 5577385 commit f82331d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/contracts/solution-contract.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,37 @@ class ClassName: # ← No blank line before class/function
270270
- ❌ Redundant restating of code logic
271271
- ❌ More than 4 bullet points
272272

273+
#### Dynamic Programming Solutions
274+
275+
For DP solutions, place the **State/Base case/Transition** definitions as **class-level comments** inside the class body, not in the block comment header.
276+
277+
**Format:**
278+
279+
```python
280+
# ============================================================================
281+
# Solution 1: Interval DP (Character Printing)
282+
# Time: O(n³), Space: O(n²)
283+
# - Key insight: when s[k] == s[i], extend first print to cover s[k]
284+
# - Preprocess: remove consecutive duplicates
285+
# ============================================================================
286+
class Solution:
287+
# State: dp[i][j] = minimum turns to print s[i:j+1]
288+
# Base case: dp[i][i] = 1 (single char needs 1 turn)
289+
# Transition: dp[i][j] = min(dp[i+1][j] + 1,
290+
# dp[i+1][k-1] + dp[k][j]) for s[k]==s[i]
291+
292+
def strangePrinter(self, s: str) -> int:
293+
...
294+
```
295+
296+
| Location | Content |
297+
|----------|---------|
298+
| Block comment | Algorithm name, Time/Space complexity, key insights (2-3 bullets) |
299+
| Class-level comment | `# State:`, `# Base case:`, `# Transition:` definitions |
300+
| Inline comments | Brief markers like `# Base case`, `# Transition` near code |
301+
302+
**Rationale:** DP definitions are implementation details that belong close to the code. Block comments should focus on high-level algorithmic insights.
303+
273304
#### Internal Function Comments
274305

275306
Internal comments **within methods** are acceptable and encouraged for documenting:

0 commit comments

Comments
 (0)