-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0118_pascals_triangle.html
More file actions
428 lines (371 loc) · 16.2 KB
/
0118_pascals_triangle.html
File metadata and controls
428 lines (371 loc) · 16.2 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>118 - Pascal's Triangle</title>
<link rel="stylesheet" href="styles.css">
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<div class="container">
<div class="problem-info">
<h1><span class="problem-number">#118</span> Pascal's Triangle</h1>
<p>
Given an integer numRows, return the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
</p>
<h3>Key Pattern</h3>
<p>
Each row starts and ends with 1. For middle elements:
<strong>triangle[row][col] = triangle[row-1][col-1] + triangle[row-1][col]</strong>
</p>
<div class="problem-meta">
<span class="meta-tag">📝 Algorithm</span>
<span class="meta-tag">⏱️ O(n)</span>
</div>
<div class="file-ref">
📄 Python: <code>python/0118_pascal's_triangle/0118_pascal's_triangle.py</code>
</div>
</div>
<div class="explanation-panel">
<h4>🧠 How It Works (Layman's Terms)</h4>
<p>This algorithm solves the problem <strong>step by step</strong>:</p>
<ul>
<li><strong>Understand:</strong> Parse the input data</li>
<li><strong>Process:</strong> Apply the core logic</li>
<li><strong>Optimize:</strong> Use efficient data structures</li>
<li><strong>Return:</strong> Output the computed result</li>
</ul>
</div>
<section class="visualization-section">
<h3>🎬 Step-by-Step Visualization</h3>
<div class="controls">
<button id="autoRunBtn" class="btn">▶ Auto Run</button>
<button id="stepBtn" class="btn btn-success">Step</button>
<button id="resetBtn" class="btn btn-danger">Reset</button>
<label style="margin-left: 20px;">
Rows: <input type="range" id="rowSlider" min="1" max="10" value="6">
<span id="rowCount">6</span>
</label>
</div>
<div class="status" id="status">Click Auto Run to build Pascal's Triangle row by row</div>
<svg id="visualization"></svg>
</section>
<section class="code-section">
<h3>💻 Python Solution</h3>
<div class="code-block">
<pre>from typing import List, Optional
"""
LeetCode 118. Pascal's Triangle
Problem from LeetCode: https://leetcode.com/problems/pascals-triangle/
Description:
Given an integer numRows, return the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Constraints:
- 1 <= numRows <= 30
"""
class Solution:
def generate(self, numRows):
result = []
if numRows >= 1:
result.append([1])
for row in range(1, numRows):
current_row = [1]
prev_row = result[row - 1]
for j in range(1, row):
current_row.append(prev_row[j - 1] + prev_row[j])
current_row.append(1)
result.append(current_row)
return result
if __name__ == '__main__':
# Example usage based on LeetCode sample
solution = Solution()
# Example 1
numRows = 5
result = solution.generate(numRows)
print(f"Input: numRows = {numRows}")
print(f"Output: {result}")
# Expected output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
# Example 2
numRows = 1
result = solution.generate(numRows)
print(f"Input: numRows = {numRows}")
print(f"Output: {result}")
# Expected output: [[1]]
</pre>
</div>
</section>
</div>
<script>
const width = 900;
const height = 600;
const svg = d3.select("#visualization")
.attr("width", width)
.attr("height", height);
let numRows = 6;
let triangle = [];
let currentRow = 0;
let currentCol = 0;
let phase = "init";
let animationTimer = null;
let highlighting = null;
document.getElementById("rowSlider").addEventListener("input", (e) => {
numRows = parseInt(e.target.value);
document.getElementById("rowCount").textContent = numRows;
reset();
});
function reset() {
triangle = [];
currentRow = 0;
currentCol = 0;
phase = "init";
highlighting = null;
if (animationTimer) clearInterval(animationTimer);
animationTimer = null;
document.getElementById("autoRunBtn").textContent = "▶ Auto Run";
document.getElementById("status").textContent =
`Building Pascal's Triangle with ${numRows} rows`;
render();
}
function render() {
svg.selectAll("*").remove();
const cellSize = 55;
const startY = 50;
const centerX = width / 2;
// Title
svg.append("text")
.attr("x", 30)
.attr("y", 25)
.attr("font-size", "14px")
.attr("font-weight", "bold")
.attr("fill", "#1e293b")
.text(`Pascal's Triangle (${numRows} rows)`);
// Draw the triangle
for (let row = 0; row < triangle.length; row++) {
const rowValues = triangle[row];
const rowWidth = rowValues.length * cellSize;
const startX = centerX - rowWidth / 2;
for (let col = 0; col < rowValues.length; col++) {
const x = startX + col * cellSize;
const y = startY + row * 55;
const value = rowValues[col];
const isCurrentCell = row === currentRow && col === currentCol && phase !== "done";
const isHighlighted = highlighting &&
((row === highlighting.row && col === highlighting.col1) ||
(row === highlighting.row && col === highlighting.col2));
const isNewCell = row === currentRow && phase === "building";
const isJustAdded = row === currentRow && col < currentCol;
// Draw hexagon-like shape for visual appeal
const hexPath = `M${x + cellSize/2},${y}
L${x + cellSize - 5},${y + 15}
L${x + cellSize - 5},${y + 40}
L${x + cellSize/2},${y + 55}
L${x + 5},${y + 40}
L${x + 5},${y + 15} Z`;
svg.append("path")
.attr("d", hexPath)
.attr("fill", () => {
if (isCurrentCell) return "#fef3c7";
if (isHighlighted) return "#dbeafe";
if (isJustAdded) return "#d1fae5";
return "#f0fdf4";
})
.attr("stroke", () => {
if (isCurrentCell) return "#f59e0b";
if (isHighlighted) return "#3b82f6";
if (isJustAdded) return "#10b981";
return "#86efac";
})
.attr("stroke-width", isCurrentCell || isHighlighted ? 3 : 2);
svg.append("text")
.attr("x", x + cellSize / 2)
.attr("y", y + 32)
.attr("text-anchor", "middle")
.attr("font-size", value > 99 ? "14px" : "16px")
.attr("font-weight", "bold")
.attr("fill", "#1e293b")
.text(value);
}
}
// Draw arrows showing current computation
if (highlighting && triangle.length > 1 && currentRow > 0) {
const prevRow = currentRow - 1;
const prevRowValues = triangle[prevRow];
const prevRowWidth = prevRowValues.length * cellSize;
const prevStartX = centerX - prevRowWidth / 2;
const currRowWidth = (triangle[currentRow] ? triangle[currentRow].length : currentCol) * cellSize;
const currStartX = centerX - currRowWidth / 2;
// Arrow from left parent
if (highlighting.col1 >= 0 && highlighting.col1 < prevRowValues.length) {
const fromX = prevStartX + highlighting.col1 * cellSize + cellSize / 2;
const fromY = startY + prevRow * 55 + 55;
const toX = currStartX + currentCol * cellSize + cellSize / 2 - 10;
const toY = startY + currentRow * 55;
svg.append("path")
.attr("d", `M${fromX},${fromY} Q${fromX},${(fromY + toY) / 2} ${toX},${toY}`)
.attr("fill", "none")
.attr("stroke", "#3b82f6")
.attr("stroke-width", 2)
.attr("stroke-dasharray", "5,3");
}
// Arrow from right parent
if (highlighting.col2 >= 0 && highlighting.col2 < prevRowValues.length) {
const fromX = prevStartX + highlighting.col2 * cellSize + cellSize / 2;
const fromY = startY + prevRow * 55 + 55;
const toX = currStartX + currentCol * cellSize + cellSize / 2 + 10;
const toY = startY + currentRow * 55;
svg.append("path")
.attr("d", `M${fromX},${fromY} Q${fromX},${(fromY + toY) / 2} ${toX},${toY}`)
.attr("fill", "none")
.attr("stroke", "#3b82f6")
.attr("stroke-width", 2)
.attr("stroke-dasharray", "5,3");
}
}
// Calculation display
if (highlighting && phase === "building") {
const calcY = height - 130;
svg.append("rect")
.attr("x", 30)
.attr("y", calcY)
.attr("width", 400)
.attr("height", 50)
.attr("rx", 10)
.attr("fill", "#eff6ff")
.attr("stroke", "#3b82f6");
svg.append("text")
.attr("x", 230)
.attr("y", calcY + 32)
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.attr("fill", "#1e293b")
.text(highlighting.text);
}
// Algorithm explanation
const algoY = height - 70;
svg.append("text")
.attr("x", 30)
.attr("y", algoY)
.attr("font-size", "12px")
.attr("fill", "#64748b")
.text("Pattern: Each number = sum of two numbers above it");
svg.append("text")
.attr("x", 30)
.attr("y", algoY + 20)
.attr("font-size", "12px")
.attr("fill", "#64748b")
.text("First and last elements of each row are always 1");
// Completion message
if (phase === "done") {
svg.append("rect")
.attr("x", width / 2 - 200)
.attr("y", height - 70)
.attr("width", 400)
.attr("height", 55)
.attr("rx", 10)
.attr("fill", "#d1fae5")
.attr("stroke", "#10b981")
.attr("stroke-width", 2);
svg.append("text")
.attr("x", width / 2)
.attr("y", height - 35)
.attr("text-anchor", "middle")
.attr("font-size", "16px")
.attr("font-weight", "bold")
.attr("fill", "#10b981")
.text(`✓ Pascal's Triangle with ${numRows} rows complete!`);
}
}
function step() {
if (phase === "done") return;
if (phase === "init") {
// Start with first row
triangle.push([1]);
currentRow = 0;
phase = "row_complete";
document.getElementById("status").textContent = "Row 1: [1] - First row always just contains 1";
render();
return;
}
if (phase === "row_complete") {
currentRow++;
if (currentRow >= numRows) {
phase = "done";
document.getElementById("status").textContent = `✓ Complete! Generated ${numRows} rows.`;
render();
return;
}
currentCol = 0;
triangle.push([]);
phase = "building";
}
if (phase === "building") {
const row = currentRow;
const col = currentCol;
const prevRow = triangle[row - 1];
if (col === 0 || col === row) {
// First or last element is always 1
triangle[row].push(1);
highlighting = {
row: row - 1,
col1: col > 0 ? col - 1 : -1,
col2: col < row ? col : -1,
text: `Position [${row}][${col}]: Edge element = 1`
};
document.getElementById("status").textContent =
`Row ${row + 1}, Position ${col}: Edge element, value = 1`;
} else {
// Middle element = sum of two above
const val = prevRow[col - 1] + prevRow[col];
triangle[row].push(val);
highlighting = {
row: row - 1,
col1: col - 1,
col2: col,
text: `Position [${row}][${col}]: ${prevRow[col-1]} + ${prevRow[col]} = ${val}`
};
document.getElementById("status").textContent =
`Row ${row + 1}, Position ${col}: ${prevRow[col-1]} + ${prevRow[col]} = ${val}`;
}
currentCol++;
if (currentCol > row) {
phase = "row_complete";
highlighting = null;
document.getElementById("status").textContent =
`Row ${row + 1} complete: [${triangle[row].join(', ')}]`;
}
render();
}
}
function autoRun() {
if (animationTimer) {
clearInterval(animationTimer);
animationTimer = null;
document.getElementById("autoRunBtn").textContent = "▶ Auto Run";
return;
}
document.getElementById("autoRunBtn").textContent = "⏸ Pause";
animationTimer = setInterval(() => {
if (phase === "done") {
clearInterval(animationTimer);
animationTimer = null;
document.getElementById("autoRunBtn").textContent = "▶ Auto Run";
return;
}
step();
}, 600);
}
document.getElementById("autoRunBtn").addEventListener("click", autoRun);
document.getElementById("stepBtn").addEventListener("click", step);
document.getElementById("resetBtn").addEventListener("click", reset);
reset();
</script>
</body>
</html>