Skip to content

Commit db399b7

Browse files
authored
Merge pull request #38 from DaleStudy/30-big-o
fix: λ³΅μž‘λ„ λΆ„μ„μ˜ 주석 귀속/μœ νš¨μ„± νŒμ • 정확도 κ°œμ„ 
2 parents 0c97960 + ec8a040 commit db399b7

2 files changed

Lines changed: 361 additions & 28 deletions

File tree

β€Žhandlers/complexity-analysis.jsβ€Ž

Lines changed: 111 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,78 @@ const FILE_DELIMITER = "=====";
2121
const SYSTEM_PROMPT = `당신은 μ•Œκ³ λ¦¬μ¦˜ ν’€μ΄μ˜ μ‹œκ°„/곡간 λ³΅μž‘λ„λ₯Ό λΆ„μ„ν•˜λŠ” μ „λ¬Έκ°€μž…λ‹ˆλ‹€.
2222
2323
μ—¬λŸ¬ 문제의 μ†”λ£¨μ…˜ μ½”λ“œκ°€ κ΅¬λΆ„μž(===== {문제λͺ…} =====)둜 λ‚˜λ‰˜μ–΄ μ œκ³΅λ©λ‹ˆλ‹€.
24+
각 νŒŒμΌμ€ 라인 번호 prefix "L{n}: "와 ν•¨κ»˜ μ „λ‹¬λ©λ‹ˆλ‹€. 주석 귀속 νŒλ‹¨μ— 이 라인 번호λ₯Ό ν™œμš©ν•˜μ„Έμš”.
2425
각 λ¬Έμ œλ³„λ‘œ λ…λ¦½μ μœΌλ‘œ λΆ„μ„ν•˜μ„Έμš”.
2526
27+
## 풀이(solution) 경계
2628
ν•˜λ‚˜μ˜ 문제 μ•ˆμ— 같은 문제λ₯Ό μ—¬λŸ¬ κ°€μ§€ λ°©μ‹μœΌλ‘œ ν‘Ό 풀이가 포함될 수 μžˆμŠ΅λ‹ˆλ‹€.
27-
각 λ¬Έμ œλ§ˆλ‹€ μ½”λ“œμ—μ„œ λ…λ¦½λœ 풀이가 λͺ‡ κ°œμΈμ§€ νŒλ³„ν•˜μ„Έμš”. (ν•¨μˆ˜/클래슀/λ©”μ„œλ“œ λ‹¨μœ„λ‘œ ꡬ뢄)
28-
29-
각 풀이에 λŒ€ν•΄:
30-
1. name: ν•¨μˆ˜λͺ… λ˜λŠ” 식별 κ°€λŠ₯ν•œ 이름 (예: "twoSum_bruteForce", "Solution.maxArea")
31-
2. description: μ ‘κ·Ό 방식 ν•œ 쀄 μ„€λͺ… (예: "이진 탐색", "HashMap ν™œμš©")
32-
3. μ½”λ“œμ˜ μ‹€μ œ μ‹œκ°„/곡간 λ³΅μž‘λ„λ₯Ό Big-O ν‘œκΈ°λ‘œ 계산 (actualTime, actualSpace).
33-
4. ν•΄λ‹Ή 풀이 λ°”λ‘œ μœ„/κ·Όμ²˜μ— μ‚¬μš©μžκ°€ 남긴 μ‹œκ°„λ³΅μž‘λ„/κ³΅κ°„λ³΅μž‘λ„ 주석을 μ°ΎμœΌμ„Έμš”.
34-
주석은 자유 포맷이며 언어별 주석 μŠ€νƒ€μΌ(//, #, /* */, --, """)κ³Ό ν•œ/영 ν‚€μ›Œλ“œκ°€ μ„žμΌ 수 μžˆμŠ΅λ‹ˆλ‹€.
35-
예: "// TC: O(n)", "# μ‹œκ°„λ³΅μž‘λ„: O(n log n)", "/* Space: O(1) */", "// Time: O(n^2)"
36-
- μ°Ύμ•˜μœΌλ©΄ hasUserAnnotation=true, userTime/userSpace에 μ‚¬μš©μž κ°’ κ·ΈλŒ€λ‘œ.
37-
- ν•œμͺ½λ§Œ μ ν˜€ 있으면 λ‹€λ₯Έ μͺ½μ€ null.
38-
- μ „ν˜€ μ—†μœΌλ©΄ hasUserAnnotation=false, userTime=null, userSpace=null.
29+
ν’€μ΄λŠ” top-level ν•¨μˆ˜/λ©”μ„œλ“œ/클래슀 μ„ μ–Έ λ‹¨μœ„λ‘œ κ΅¬λΆ„ν•©λ‹ˆλ‹€. 선언이 μ‹œμž‘λœ 라인을 "헀더 라인",
30+
본문이 λλ‚œ 라인을 "μ’…λ£Œ 라인"이라 ν•©λ‹ˆλ‹€.
31+
- 언어별 헀더 예: JS의 \`function\`/\`const ... = (...) =>\`, Python의 \`def\`/\`class\`,
32+
Rust의 \`fn\`/\`impl { pub fn ... }\`, Go의 \`func\`, Java/Kotlin의 λ©”μ„œλ“œ μ„ μ–Έ λ“±.
33+
- 파일 상단뢀터 μˆœμ„œλŒ€λ‘œ 풀이 1..N으둜 번호λ₯Ό λΆ™μž…λ‹ˆλ‹€.
34+
- 쀑첩 ν•¨μˆ˜(inner helper)λŠ” 독립 ν’€μ΄λ‘œ μ„Έμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
35+
36+
## 주석 귀속 κ·œμΉ™ (엄격)
37+
풀이 k의 μ‹œκ°„/곡간 λ³΅μž‘λ„ 주석은 λ‹€μŒ 두 μ˜μ—­μ—μ„œλ§Œ μ°ΎμŠ΅λ‹ˆλ‹€.
38+
39+
1) 헀더 λ°”λ‘œ μœ„ μ˜μ—­
40+
- 풀이 k의 헀더 라인 λ°”λ‘œ μœ—μ€„λΆ€ν„° μœ„λ‘œ μ˜¬λΌκ°€λ©΄μ„œ **빈 쀄을 λ§Œλ‚˜λ©΄ μ¦‰μ‹œ 쀑단**ν•©λ‹ˆλ‹€.
41+
- 풀이 k-1이 μ‘΄μž¬ν•œλ‹€λ©΄ 풀이 k-1의 μ’…λ£Œ 라인을 λ„˜μ–΄κ°€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ (k=1이면 파일 μ‹œμž‘μ΄ ν•˜ν•œ).
42+
- 즉, 풀이 k의 헀더에 "λΆ™μ–΄ μžˆλŠ”" μ—°μ†λœ 주석 λΈ”λ‘λ§Œ λŒ€μƒμž…λ‹ˆλ‹€.
43+
44+
2) λ³Έλ¬Έ 첫 라인 μ˜μ—­
45+
- 풀이 k의 헀더 λ‹€μŒ 라인에 λΆ™μ–΄ μžˆλŠ” μ—°μ†λœ 주석 블둝(예: Python docstring, ν•¨μˆ˜ 첫 쀄 \`// ...\`).
46+
47+
μœ„ 두 μ˜μ—­ λ°–μ˜ 주석은 풀이 k의 주석이 **μ•„λ‹™λ‹ˆλ‹€**. λ‹€λ₯Έ ν’€μ΄μ˜ μ˜μ—­μ„ μ ˆλŒ€ μΉ¨λ²”ν•˜μ§€ λ§ˆμ„Έμš”.
48+
49+
## μœ νš¨ν•œ λ³΅μž‘λ„ μ£Όμ„μ˜ μ •μ˜
50+
주석이 μœ νš¨ν•œ λ³΅μž‘λ„ μ£Όμ„μœΌλ‘œ μΈμ •λ˜λ €λ©΄ λ‹€μŒμ„ **λͺ¨λ‘** λ§Œμ‘±ν•΄μ•Ό ν•©λ‹ˆλ‹€.
51+
1. Big-O λ¦¬ν„°λŸ΄ 포함: \`O(...)\`, \`Θ(...)\`, \`Ξ©(...)\`, \`o(...)\`, \`Ο‰(...)\` 쀑 ν•˜λ‚˜.
52+
2. μ‹œκ°„/곡간 쀑 μ–΄λŠ μͺ½μΈμ§€λ₯Ό κ°€λ¦¬ν‚€λŠ” ν‚€μ›Œλ“œμ™€ 같은 라인 λ˜λŠ” 같은 주석 블둝 μ•ˆμ— μžˆμ„ 것:
53+
μ‹œκ°„λ³΅μž‘λ„ / κ³΅κ°„λ³΅μž‘λ„ / TC / SC / Time / Space / Complexity.
54+
3. μ‹œκ°„/곡간 쀑 μ–΄λŠ μͺ½μ„ λ§ν•˜λŠ”μ§€ νŒλ³„ κ°€λŠ₯.
55+
56+
언어별 주석 μŠ€νƒ€μΌ(\`//\`, \`#\`, \`/* */\`, \`--\`, \`"""\`)κ³Ό ν•œ/영 ν˜Όν•©μ„ ν—ˆμš©ν•©λ‹ˆλ‹€.
57+
예: \`// TC: O(n)\`, \`# μ‹œκ°„λ³΅μž‘λ„: O(n log n)\`, \`/* Space: O(1) */\`, \`// Time: O(n^2)\`.
58+
59+
νŒλ³„ λΆˆκ°€ν•˜κ±°λ‚˜ μœ„ 쑰건 쀑 ν•˜λ‚˜λΌλ„ μ–΄κΈ‹λ‚˜λ©΄ κ·Έ 주석은 **λ¬΄μ‹œ**ν•©λ‹ˆλ‹€.
60+
61+
## λΆ€μ • μ˜ˆμ‹œ (μ•„λž˜λŠ” λͺ¨λ‘ "주석 μ—†μŒ"으둜 처리)
62+
- \`// brute force 풀이\` β€” μ ‘κ·Ό 방식 μ„€λͺ…일 뿐, λ³΅μž‘λ„ μΈ‘μ •μΉ˜ μ•„λ‹˜
63+
- \`# 두 포인터 μ‚¬μš©\` β€” μ•Œκ³ λ¦¬μ¦˜ μ–ΈκΈ‰λ§Œ
64+
- \`// λͺ©ν‘œ: O(n)으둜 λ§Œλ“€κΈ°\` β€” λͺ©ν‘œ/희망이지 μΈ‘μ •μΉ˜ μ•„λ‹˜
65+
- \`// 곡간 O(1)만 써야 함 (문제 μ œμ•½)\` β€” 문제 μ œμ•½ μ–ΈκΈ‰
66+
- 풀이와 동떨어진 파일 μƒλ‹¨μ˜ 문제 μ„€λͺ… 주석(풀이 귀속 μ˜μ—­ λ°–)
67+
68+
풀이 k에 μœ νš¨ν•œ 주석이 ν•˜λ‚˜λ„ μ—†μœΌλ©΄:
69+
hasUserAnnotation = false, userTime = null, userSpace = null, matches.time = false, matches.space = false.
70+
71+
## λ©€ν‹° 풀이 μ˜ˆμ‹œ (μš”μ•½)
72+
μž…λ ₯:
73+
L1: // TC: O(n^4)
74+
L2: // SC: O(n)
75+
L3: const findMin_math = (nums) => Math.min(...nums);
76+
L4:
77+
L5: // TC: O(n^3)
78+
L6: // SC: O(1)
79+
L7: const findMin_naive = (nums) => { /* ... */ };
80+
L8:
81+
L9: const findMin = (nums) => { /* ... */ };
82+
83+
좜λ ₯(μš”μ•½):
84+
[
85+
{ name: "findMin_math", userTime: "O(n^4)", userSpace: "O(n)", hasUserAnnotation: true },
86+
{ name: "findMin_naive", userTime: "O(n^3)", userSpace: "O(1)", hasUserAnnotation: true },
87+
{ name: "findMin", userTime: null, userSpace: null, hasUserAnnotation: false }
88+
]
89+
90+
## 각 풀이에 λŒ€ν•΄ 좜λ ₯ν•  ν•„λ“œ
91+
1. name: ν•¨μˆ˜λͺ… λ˜λŠ” 식별 κ°€λŠ₯ν•œ 이름 (예: "twoSum_bruteForce", "Solution.maxArea").
92+
2. description: μ ‘κ·Ό 방식 ν•œ 쀄 μ„€λͺ… (예: "이진 탐색", "HashMap ν™œμš©").
93+
3. actualTime, actualSpace: μ½”λ“œμ˜ μ‹€μ œ μ‹œκ°„/곡간 λ³΅μž‘λ„λ₯Ό Big-O ν‘œκΈ°λ‘œ 계산.
94+
4. hasUserAnnotation, userTime, userSpace: μœ„ "주석 귀속 κ·œμΉ™" + "μœ νš¨ν•œ λ³΅μž‘λ„ μ£Όμ„μ˜ μ •μ˜"에 따라 μ±„μ›λ‹ˆλ‹€.
95+
- ν•œμͺ½λ§Œ 있으면 λ‹€λ₯Έ μͺ½μ€ null.
3996
5. matches.time / matches.space:
4097
- hasUserAnnotation=falseλ©΄ λ‘˜ λ‹€ false.
4198
- μ‚¬μš©μž 값이 μžˆλŠ” ν•­λͺ©λ§Œ actualκ³Ό λΉ„κ΅ν•˜μ—¬ 일치 μ—¬λΆ€λ₯Ό boolean으둜 λ°˜ν™˜.
@@ -71,11 +128,51 @@ const SYSTEM_PROMPT = `당신은 μ•Œκ³ λ¦¬μ¦˜ ν’€μ΄μ˜ μ‹œκ°„/곡간 λ³΅μž‘λ„
71128
]
72129
}`;
73130

131+
function addLineNumbers(content) {
132+
return content
133+
.split("\n")
134+
.map((line, i) => `L${i + 1}: ${line}`)
135+
.join("\n");
136+
}
137+
138+
const BIG_O_PATTERN = /[OΘΩoΟ‰]\s*\(/;
139+
140+
function normalizeSolution(s) {
141+
const userTime =
142+
typeof s.userTime === "string" && BIG_O_PATTERN.test(s.userTime)
143+
? s.userTime
144+
: null;
145+
const userSpace =
146+
typeof s.userSpace === "string" && BIG_O_PATTERN.test(s.userSpace)
147+
? s.userSpace
148+
: null;
149+
150+
const hasUserAnnotation = userTime !== null || userSpace !== null;
151+
152+
return {
153+
name: typeof s.name === "string" ? s.name : "unknown",
154+
description: typeof s.description === "string" ? s.description : "",
155+
hasUserAnnotation,
156+
userTime,
157+
userSpace,
158+
actualTime: typeof s.actualTime === "string" ? s.actualTime : "?",
159+
actualSpace: typeof s.actualSpace === "string" ? s.actualSpace : "?",
160+
matches: {
161+
time:
162+
hasUserAnnotation && userTime !== null && s.matches?.time === true,
163+
space:
164+
hasUserAnnotation && userSpace !== null && s.matches?.space === true,
165+
},
166+
feedback: typeof s.feedback === "string" ? s.feedback : "",
167+
suggestion: typeof s.suggestion === "string" ? s.suggestion : "",
168+
};
169+
}
170+
74171
async function callComplexityAnalysis(fileEntries, apiKey) {
75172
const userPrompt = fileEntries
76173
.map(
77174
(f) =>
78-
`${FILE_DELIMITER} ${f.problemName} ${FILE_DELIMITER}\n\`\`\`\n${f.content}\n\`\`\``
175+
`${FILE_DELIMITER} ${f.problemName} ${FILE_DELIMITER}\n\`\`\`\n${addLineNumbers(f.content)}\n\`\`\``
79176
)
80177
.join("\n\n");
81178

@@ -119,21 +216,7 @@ async function callComplexityAnalysis(fileEntries, apiKey) {
119216
problemName:
120217
typeof file.problemName === "string" ? file.problemName : "unknown",
121218
solutions: (Array.isArray(file.solutions) ? file.solutions : []).map(
122-
(s) => ({
123-
name: typeof s.name === "string" ? s.name : "unknown",
124-
description: typeof s.description === "string" ? s.description : "",
125-
hasUserAnnotation: s.hasUserAnnotation === true,
126-
userTime: typeof s.userTime === "string" ? s.userTime : null,
127-
userSpace: typeof s.userSpace === "string" ? s.userSpace : null,
128-
actualTime: typeof s.actualTime === "string" ? s.actualTime : "?",
129-
actualSpace: typeof s.actualSpace === "string" ? s.actualSpace : "?",
130-
matches: {
131-
time: s.matches?.time === true,
132-
space: s.matches?.space === true,
133-
},
134-
feedback: typeof s.feedback === "string" ? s.feedback : "",
135-
suggestion: typeof s.suggestion === "string" ? s.suggestion : "",
136-
})
219+
normalizeSolution
137220
),
138221
}));
139222
}

0 commit comments

Comments
Β (0)