-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidepanel.js
More file actions
265 lines (227 loc) · 8.56 KB
/
Copy pathsidepanel.js
File metadata and controls
265 lines (227 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const titleEl = document.getElementById("problemTitle");
const urlEl = document.getElementById("problemUrl");
const hintBox = document.getElementById("hintBox");
const btnClarify = document.getElementById("btnClarify");
const btnPlan = document.getElementById("btnPlan");
const btnNudge = document.getElementById("btnNudge");
const btnSkeleton = document.getElementById("btnSkeleton");
const btnEdge = document.getElementById("btnEdge");
const statusEl = document.getElementById("textStatus");
function setStatus(text) {
if (statusEl) statusEl.textContent = text;
}
async function loadSavedText() {
const { lastLcProblemText } = await chrome.storage.local.get(["lastLcProblemText"]);
const hasText = (lastLcProblemText?.description || "").length > 50;
setStatus(hasText ? "Problem text: ready" : "Problem text: not detected yet");
}
loadSavedText();
function setProblem(meta) {
titleEl.textContent = meta?.title || "Open a LeetCode problem";
if (meta?.url) {
urlEl.textContent = meta.url;
urlEl.href = meta.url;
} else {
urlEl.textContent = "";
urlEl.href = "#";
}
}
async function loadSaved() {
const { lastLcMeta } = await chrome.storage.local.get(["lastLcMeta"]);
if (lastLcMeta) setProblem(lastLcMeta);
}
loadSaved();
chrome.runtime.onMessage.addListener((msg) => {
if (msg?.type === "LC_META") setProblem(msg.payload);
});
function setHint(text) {
hintBox.textContent = enforceNoSolution(text || "");
}
async function getSavedProblemText() {
const { lastLcProblemText } = await chrome.storage.local.get(["lastLcProblemText"]);
return lastLcProblemText || { description: "", constraints: "" };
}
function inferPatterns(description, constraints) {
const text = (description + " " + constraints).toLowerCase();
const patterns = [];
if (text.includes("sorted") || text.includes("non-decreasing")) patterns.push("two pointers or binary search");
if (text.includes("subarray") || text.includes("substring")) patterns.push("sliding window");
if (text.includes("parentheses") || text.includes("brackets")) patterns.push("stack");
if (text.includes("tree") || text.includes("binary tree")) patterns.push("dfs or bfs");
if (text.includes("graph") || text.includes("connected") || text.includes("edges")) patterns.push("graph traversal (bfs/dfs) or union find");
if (text.includes("minimum") && text.includes("maximum")) patterns.push("greedy or monotonic queue");
if (text.includes("number of ways") || text.includes("count the ways")) patterns.push("dynamic programming");
return [...new Set(patterns)].slice(0, 3);
}
function enforceNoSolution(text) {
// Hard guardrails: keep it short, avoid full functions, force blanks
const maxChars = 900;
let out = (text || "").slice(0, maxChars);
// Ensure blanks exist so user must fill
if (!out.includes("___")) out += "\n\nFill in: ___";
return out;
}
function skeletonFor(pattern, lang = "python") {
// Keep it language-neutral-ish. You can expand per language later.
const common = {
"two pointers or binary search": [
"Skeleton (two pointers / binary search):",
"1) Initialize pointers: left = ___, right = ___",
"2) While left ___ right:",
" - Compute current state: cur = ___",
" - If condition met: ___",
" - Else move pointer: left/right = ___",
"3) Return ___"
].join("\n"),
"sliding window": [
"Skeleton (sliding window):",
"1) left = 0, ans = ___, window = ___",
"2) For right in range(n):",
" - Add a[right] to window: window[___] += 1",
" - While window invalid:",
" - Remove a[left]: window[___] -= 1",
" - left += 1",
" - Update ans: ans = ___",
"3) Return ans"
].join("\n"),
"stack": [
"Skeleton (stack):",
"1) stack = []",
"2) For each char/item x:",
" - If x is opening: push x",
" - Else:",
" - If stack empty: return ___",
" - top = pop",
" - Validate match: ___",
"3) Return ___"
].join("\n"),
"dfs or bfs on trees": [
"Skeleton (DFS/BFS on tree):",
"DFS:",
" def dfs(node):",
" if node is null: return ___",
" left = dfs(node.left)",
" right = dfs(node.right)",
" combine = ___",
" return combine",
"",
"BFS:",
" queue = [root], while queue:",
" level_size = ___",
" for _ in range(level_size):",
" node = pop(0)",
" process node: ___",
" push children: ___"
].join("\n"),
"graph bfs/dfs or union find": [
"Skeleton (graph traversal):",
"1) Build adjacency: adj = ___",
"2) visited = set()",
"3) For each node:",
" - If not visited:",
" - BFS/DFS from node",
" - Update result: ___",
"",
"BFS template:",
" queue = [start], visited add start",
" while queue:",
" u = pop(0)",
" for v in adj[u]:",
" if v not visited: visited add v, push v"
].join("\n"),
"dynamic programming": [
"Skeleton (DP):",
"1) Define state: dp[i] means ___",
"2) Base cases: dp[0] = ___",
"3) Transition:",
" for i in range(1..n):",
" dp[i] = min/max over ___",
"4) Return dp[___]"
].join("\n"),
"heap or quickselect": [
"Skeleton (top K / kth):",
"Heap approach:",
"1) heap = []",
"2) For each item x:",
" - push key(x)",
" - if heap size > K: pop",
"3) Return ___",
"",
"Quickselect approach:",
"1) Choose pivot ___",
"2) Partition into <, =, > groups",
"3) Recurse into the group containing kth"
].join("\n")
};
return common[pattern] || [
"Skeleton:",
"1) Start with brute force: ___",
"2) Identify bottleneck using constraints",
"3) Replace bottleneck with a data structure: ___",
"4) Return ___"
].join("\n");
}
btnClarify?.addEventListener("click", async () => {
const { description, constraints } = await getSavedProblemText();
const snippet = description
? description.slice(0, 500) + (description.length > 500 ? "…" : "")
: "(No description detected yet. Refresh the LeetCode tab and try again.)";
const c = constraints ? `\n\nConstraints:\n${constraints}` : "";
setHint(
`What I see:\n${snippet}${c}\n\nClarify tasks:\n1) Restate the problem in one sentence.\n2) Define inputs and outputs clearly.\n3) Write 2 edge cases you will test.\n4) Decide a target time complexity based on constraints.`
);
});
btnPlan?.addEventListener("click", async () => {
const { description, constraints } = await getSavedProblemText();
const patterns = inferPatterns(description, constraints);
const list = patterns.length ? patterns.map((p) => `- ${p}`).join("\n") : "- Start with brute force, then optimize using constraints.";
setHint(
`Plan:\nLikely patterns:\n${list}\n\nNext: pick one pattern and write the invariant/state before coding.`
);
});
btnNudge?.addEventListener("click", async () => {
setHint(
`Nudge:\nWhat has to remain true after every step (an invariant)?\nIf you can state that clearly, the loops and conditions usually become obvious.`
);
});
btnSkeleton?.addEventListener("click", async () => {
setHint(
`Skeleton:\nWrite the function signature, then add:\n- variable initialization\n- main loop structure\n- update steps (leave blanks)\n- return statement\n\nOnly fill blanks after your invariant/state is written.`
);
});
btnEdge?.addEventListener("click", async () => {
const { description, constraints } = await getSavedProblemText();
const patterns = inferPatterns(description, constraints);
const primary = patterns[0] || "";
const map = {
"sliding window": [
"Edge cases (sliding window):",
"- when window shrinks to empty",
"- duplicates and frequency going to zero",
"- all valid vs none valid",
"- right moves but left must catch up correctly"
].join("\n"),
"stack": [
"Edge cases (stack):",
"- first char is closing",
"- leftover openings at end",
"- nested vs sequential pairs",
"- invalid ordering like (]"
].join("\n"),
"dynamic programming": [
"Edge cases (DP):",
"- base cases for n=0, n=1",
"- unreachable states (use INF or sentinel)",
"- off-by-one in dp indexing",
"- verify transition only uses already computed states"
].join("\n")
};
setHint(map[primary] || [
"Edge cases:",
"- smallest input size",
"- duplicates",
"- negative values if allowed",
"- already optimal case",
"- worst-case boundary values"
].join("\n"));
});