@@ -21,21 +21,78 @@ const FILE_DELIMITER = "=====";
2121const 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.
39965. 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+
74171async 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