Skip to content

Commit 5796f28

Browse files
committed
docs: clarify no blank line between comment block and class/function
- Add explicit "No blank line" rule in A.7.2 Solution Block Comments - Add arrow annotations in examples showing no gap - Add "No blank line" as required component in format table - Update A.7.3 JUDGE_FUNC section with same convention - Update Quick Reference template accordingly
1 parent aefa90b commit 5796f28

File tree

2 files changed

+138
-5
lines changed

2 files changed

+138
-5
lines changed

docs/SOLUTION_CONTRACT.md

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,133 @@ SOLUTIONS = {
176176
}
177177
```
178178

179+
### A.7 Solution Comment Format
180+
181+
Solutions SHOULD include structured comments to explain the algorithm, approach, and key insights. This section defines the standard comment format.
182+
183+
#### A.7.1 File-Level Docstring
184+
185+
Every solution file SHOULD start with a docstring describing the problem:
186+
187+
```python
188+
"""
189+
Problem: Two Sum
190+
Link: https://leetcode.com/problems/two-sum/
191+
192+
Given an array of integers nums and an integer target, return indices
193+
of the two numbers such that they add up to target.
194+
195+
Constraints:
196+
- 2 <= nums.length <= 10^4
197+
- -10^9 <= nums[i] <= 10^9
198+
- -10^9 <= target <= 10^9
199+
- Only one valid answer exists.
200+
"""
201+
```
202+
203+
| Field | Required | Description |
204+
|-------|----------|-------------|
205+
| `Problem` || Problem title |
206+
| `Link` || LeetCode URL |
207+
| Description | Recommended | Brief problem statement |
208+
| `Constraints` | Recommended | Key constraints affecting algorithm choice |
209+
210+
#### A.7.2 Solution Block Comments
211+
212+
Each solution class SHOULD be preceded by a block comment explaining the approach.
213+
214+
**No blank line** between the comment block and the class definition:
215+
216+
```python
217+
# ============================================================================
218+
# Solution 1: Sliding Window (Optimized with Jump)
219+
# Time: O(n), Space: O(min(n, σ))
220+
# - Each character visited at most twice
221+
# - Uses last-seen-index array for O(1) duplicate detection
222+
# - Direct position jumping instead of incremental contraction
223+
# ============================================================================
224+
class SolutionSlidingWindow: # ← 緊接著,無空行
225+
...
226+
```
227+
228+
**Format:**
229+
230+
```
231+
# ============================================
232+
# Solution {N}: {Approach Name}
233+
# Time: O(?), Space: O(?)
234+
# - {Key insight or implementation detail}
235+
# - {Additional notes}
236+
# ============================================
237+
class ClassName: # ← No blank line before class/function
238+
```
239+
240+
| Component | Required | Description |
241+
|-----------|----------|-------------|
242+
| Solution number & name || e.g., `Solution 1: Sliding Window` |
243+
| Time/Space complexity || e.g., `Time: O(n), Space: O(n)` |
244+
| Bullet points | Recommended | Key insights, implementation details |
245+
| **No blank line** || Comment block directly followed by class/function |
246+
247+
**More examples:**
248+
249+
```python
250+
# ============================================
251+
# Solution 1: Single Pass
252+
# Time: O(max(m,n)), Space: O(max(m,n))
253+
# - Single pass through both lists
254+
# - Result list has at most max(m,n) + 1 nodes
255+
# ============================================
256+
class Solution:
257+
...
258+
```
259+
260+
```python
261+
# ============================================================================
262+
# Solution 2: Using Dictionary (More Flexible for Unicode)
263+
# Time: O(n), Space: O(min(n, σ))
264+
# - Same sliding window approach with dictionary instead of array
265+
# - More flexible for Unicode strings but slightly slower
266+
# ============================================================================
267+
class SolutionDict:
268+
...
269+
```
270+
271+
```python
272+
# ============================================================================
273+
# Solution 3: Using Set (Standard While-Loop Pattern)
274+
# Time: O(n), Space: O(min(n, σ))
275+
# - Uses set to track current window characters
276+
# - Demonstrates standard while-loop contraction pattern
277+
# ============================================================================
278+
class SolutionSet:
279+
...
280+
```
281+
282+
#### A.7.3 JUDGE_FUNC Comments (Optional)
283+
284+
When defining a `JUDGE_FUNC`, you MAY include a block comment explaining its purpose and complexity.
285+
286+
Same rule: **no blank line** between comment and function:
287+
288+
```python
289+
# ============================================
290+
# JUDGE_FUNC - Required for generator support
291+
# ============================================
292+
# Uses brute force O(m+n) merge to compute the correct answer,
293+
# then compares with the solution output.
294+
# ============================================
295+
def judge(actual, expected, input_data: str) -> bool: # ← 無空行
296+
...
297+
298+
JUDGE_FUNC = judge
299+
```
300+
301+
This is optional but recommended when:
302+
- The judge uses a different algorithm than the solution
303+
- The judge has notable complexity characteristics
304+
- Generator support requires custom validation
305+
179306
---
180307

181308
## B. SOLUTIONS Metadata Schema
@@ -629,6 +756,12 @@ When adding or modifying a solution, verify:
629756
"""
630757
Problem: {Problem Title}
631758
Link: https://leetcode.com/problems/{slug}/
759+
760+
{Brief problem description}
761+
762+
Constraints:
763+
- {constraint 1}
764+
- {constraint 2}
632765
"""
633766
from typing import List
634767
from _runner import get_solver
@@ -652,9 +785,12 @@ SOLUTIONS = {
652785
}
653786

654787
# ============================================
655-
# Solution class(es)
788+
# Solution 1: {Approach Name}
789+
# Time: O(?), Space: O(?)
790+
# - {Key insight or implementation detail}
791+
# - {Additional notes}
656792
# ============================================
657-
class Solution:
793+
class Solution: # ← No blank line after comment block
658794
def methodName(self, ...):
659795
...
660796

solutions/0003_longest_substring_without_repeating_characters.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def _brute_force(s: str) -> int:
128128
# - Uses last-seen-index array for O(1) duplicate detection
129129
# - Direct position jumping instead of incremental contraction
130130
# ============================================================================
131-
132131
class Solution:
133132
"""
134133
Optimal solution using sliding window with jump optimization.
@@ -180,7 +179,6 @@ def lengthOfLongestSubstring(self, s: str) -> int:
180179
# - Same sliding window approach with dictionary instead of array
181180
# - More flexible for Unicode strings but slightly slower
182181
# ============================================================================
183-
184182
class SolutionDict:
185183
"""
186184
Alternative implementation using dictionary for last-seen index.
@@ -210,7 +208,6 @@ def lengthOfLongestSubstring(self, s: str) -> int:
210208
# - Uses set to track current window characters
211209
# - Demonstrates standard while-loop contraction pattern
212210
# ============================================================================
213-
214211
class SolutionWithSet:
215212
"""
216213
Alternative implementation using a set for the current window.

0 commit comments

Comments
 (0)