-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0994_rotting_oranges.html
More file actions
376 lines (326 loc) · 14.8 KB
/
0994_rotting_oranges.html
File metadata and controls
376 lines (326 loc) · 14.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rotting Oranges - LeetCode 994</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">#0994</span> Rotting Oranges</h1>
<p><strong>Problem:</strong> Every minute, fresh oranges adjacent to rotten oranges become rotten. Return the minimum minutes until no fresh oranges remain, or -1 if impossible.</p>
<p><strong>Pattern:</strong> Multi-source BFS - Start from all rotten oranges simultaneously</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/0994_rotting_oranges/0994_rotting_oranges.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>
<div class="visualization-section">
<h3>🎬 Step-by-Step Visualization</h3>
<div class="visualization">
<svg id="mainSvg"></svg>
</div>
<div class="controls">
<button id="stepBtn">Step (1 minute)</button>
<button id="autoBtn">Auto Run</button>
<button id="resetBtn">Reset</button>
<div class="speed-control">
<label for="speed">Speed:</label>
<input type="range" id="speed" min="100" max="2000" value="800">
</div>
</div>
<div class="status" id="status">Click "Step" to simulate rotting process (BFS)</div>
<div class="variables">
<div class="var-item">
<span class="var-label">Minutes Elapsed:</span>
<span id="minutesDisplay">0</span>
</div>
<div class="var-item">
<span class="var-label">Fresh Remaining:</span>
<span id="freshDisplay">-</span>
</div>
<div class="var-item">
<span class="var-label">Queue Size:</span>
<span id="queueDisplay">-</span>
</div>
</div>
</div>
<div class="code-section">
<h3>💻 Python Solution</h3>
<div class="code-block">
<pre><span class="keyword">def</span> <span class="function">orangesRotting</span>(grid):
<span class="string">"""
Multi-source BFS from all rotten oranges.
Time: O(m*n), Space: O(m*n)
"""</span>
<span class="keyword">from</span> collections <span class="keyword">import</span> deque
m, n = <span class="function">len</span>(grid), <span class="function">len</span>(grid[<span class="number">0</span>])
queue = <span class="function">deque</span>()
fresh = <span class="number">0</span>
<span class="comment"># Find all rotten oranges and count fresh</span>
<span class="keyword">for</span> i <span class="keyword">in</span> <span class="function">range</span>(m):
<span class="keyword">for</span> j <span class="keyword">in</span> <span class="function">range</span>(n):
<span class="keyword">if</span> grid[i][j] == <span class="number">2</span>:
queue.<span class="function">append</span>((i, j))
<span class="keyword">elif</span> grid[i][j] == <span class="number">1</span>:
fresh += <span class="number">1</span>
minutes = <span class="number">0</span>
directions = [(<span class="number">0</span>, <span class="number">1</span>), (<span class="number">0</span>, <span class="number">-1</span>), (<span class="number">1</span>, <span class="number">0</span>), (<span class="number">-1</span>, <span class="number">0</span>)]
<span class="keyword">while</span> queue <span class="keyword">and</span> fresh > <span class="number">0</span>:
minutes += <span class="number">1</span>
<span class="keyword">for</span> _ <span class="keyword">in</span> <span class="function">range</span>(<span class="function">len</span>(queue)):
r, c = queue.<span class="function">popleft</span>()
<span class="keyword">for</span> dr, dc <span class="keyword">in</span> directions:
nr, nc = r + dr, c + dc
<span class="keyword">if</span> <span class="number">0</span> <= nr < m <span class="keyword">and</span> <span class="number">0</span> <= nc < n <span class="keyword">and</span> grid[nr][nc] == <span class="number">1</span>:
grid[nr][nc] = <span class="number">2</span>
fresh -= <span class="number">1</span>
queue.<span class="function">append</span>((nr, nc))
<span class="keyword">return</span> minutes <span class="keyword">if</span> fresh == <span class="number">0</span> <span class="keyword">else</span> <span class="number">-1</span></pre>
</div>
</div>
</div>
<script>
const originalGrid = [
[2, 1, 1],
[1, 1, 0],
[0, 1, 1]
];
let grid = JSON.parse(JSON.stringify(originalGrid));
const m = grid.length;
const n = grid[0].length;
let queue = [];
let fresh = 0;
let minutes = 0;
let justRotted = [];
let autoRunning = false;
let autoTimer = null;
const width = 700;
const height = 400;
const svg = d3.select("#mainSvg")
.attr("width", width)
.attr("height", height);
const cellSize = 70;
const startX = (width - n * cellSize) / 2;
const startY = 50;
function initializeQueue() {
queue = [];
fresh = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 2) {
queue.push([i, j]);
} else if (grid[i][j] === 1) {
fresh++;
}
}
}
document.getElementById("freshDisplay").textContent = fresh;
document.getElementById("queueDisplay").textContent = queue.length;
}
function draw() {
svg.selectAll("*").remove();
// Legend
const legendY = 15;
const legendItems = [
{ color: "#e0e0e0", text: "Empty (0)" },
{ color: "#ff9800", text: "Fresh (1)" },
{ color: "#795548", text: "Rotten (2)" },
{ color: "#ffeb3b", text: "Just Rotted" }
];
legendItems.forEach((item, idx) => {
svg.append("rect")
.attr("x", 80 + idx * 150)
.attr("y", legendY)
.attr("width", 20)
.attr("height", 20)
.attr("rx", 4)
.attr("fill", item.color)
.attr("stroke", "#333");
svg.append("text")
.attr("x", 105 + idx * 150)
.attr("y", legendY + 15)
.attr("font-size", "12px")
.text(item.text);
});
// Grid
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const x = startX + j * cellSize;
const y = startY + i * cellSize;
const val = grid[i][j];
const isJustRotted = justRotted.some(([r, c]) => r === i && c === j);
let fill, stroke, emoji;
if (val === 0) {
fill = "#e0e0e0";
stroke = "#bdbdbd";
emoji = "";
} else if (val === 1) {
fill = "#ff9800";
stroke = "#f57c00";
emoji = "🍊";
} else {
fill = isJustRotted ? "#ffeb3b" : "#795548";
stroke = isJustRotted ? "#f57c00" : "#5d4037";
emoji = "🤢";
}
svg.append("rect")
.attr("x", x)
.attr("y", y)
.attr("width", cellSize - 4)
.attr("height", cellSize - 4)
.attr("rx", 8)
.attr("fill", fill)
.attr("stroke", stroke)
.attr("stroke-width", isJustRotted ? 4 : 2);
svg.append("text")
.attr("x", x + (cellSize - 4) / 2)
.attr("y", y + (cellSize - 4) / 2 + 8)
.attr("text-anchor", "middle")
.attr("font-size", "28px")
.text(emoji);
}
}
// Timer display
svg.append("rect")
.attr("x", width / 2 - 60)
.attr("y", startY + m * cellSize + 20)
.attr("width", 120)
.attr("height", 50)
.attr("rx", 10)
.attr("fill", fresh === 0 ? "#c8e6c9" : "#e3f2fd")
.attr("stroke", fresh === 0 ? "#4caf50" : "#1976d2")
.attr("stroke-width", 2);
svg.append("text")
.attr("x", width / 2)
.attr("y", startY + m * cellSize + 52)
.attr("text-anchor", "middle")
.attr("font-size", "20px")
.attr("font-weight", "bold")
.text(`⏱️ ${minutes} min`);
// Queue visualization
const queueY = startY + m * cellSize + 90;
svg.append("text")
.attr("x", 50)
.attr("y", queueY)
.attr("font-weight", "bold")
.text("BFS Queue:");
queue.forEach(([r, c], idx) => {
svg.append("rect")
.attr("x", 150 + idx * 50)
.attr("y", queueY - 15)
.attr("width", 45)
.attr("height", 25)
.attr("rx", 4)
.attr("fill", "#795548")
.attr("stroke", "#5d4037");
svg.append("text")
.attr("x", 150 + idx * 50 + 22)
.attr("y", queueY + 3)
.attr("text-anchor", "middle")
.attr("fill", "white")
.attr("font-size", "11px")
.text(`(${r},${c})`);
});
}
function step() {
if (fresh === 0) {
document.getElementById("status").textContent =
`Complete! All oranges rotted in ${minutes} minutes`;
return false;
}
if (queue.length === 0) {
document.getElementById("status").textContent =
`Impossible! ${fresh} fresh oranges cannot be reached`;
return false;
}
minutes++;
justRotted = [];
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
const queueSize = queue.length;
for (let i = 0; i < queueSize; i++) {
const [r, c] = queue.shift();
for (const [dr, dc] of directions) {
const nr = r + dr;
const nc = c + dc;
if (nr >= 0 && nr < m && nc >= 0 && nc < n && grid[nr][nc] === 1) {
grid[nr][nc] = 2;
fresh--;
queue.push([nr, nc]);
justRotted.push([nr, nc]);
}
}
}
document.getElementById("minutesDisplay").textContent = minutes;
document.getElementById("freshDisplay").textContent = fresh;
document.getElementById("queueDisplay").textContent = queue.length;
draw();
if (fresh === 0) {
document.getElementById("status").textContent =
`Complete! All oranges rotted in ${minutes} minutes`;
return false;
} else if (justRotted.length === 0) {
document.getElementById("status").textContent =
`No more spreading possible. ${fresh} fresh remain unreachable.`;
return false;
} else {
document.getElementById("status").textContent =
`Minute ${minutes}: ${justRotted.length} oranges just rotted`;
return true;
}
}
function reset() {
grid = JSON.parse(JSON.stringify(originalGrid));
minutes = 0;
justRotted = [];
autoRunning = false;
if (autoTimer) clearInterval(autoTimer);
initializeQueue();
document.getElementById("minutesDisplay").textContent = "0";
document.getElementById("status").textContent =
'Click "Step" to simulate rotting process (BFS)';
document.getElementById("autoBtn").textContent = "Auto Run";
draw();
}
function autoRun() {
if (autoRunning) {
autoRunning = false;
clearInterval(autoTimer);
document.getElementById("autoBtn").textContent = "Auto Run";
} else {
autoRunning = true;
document.getElementById("autoBtn").textContent = "Pause";
const speed = 2100 - document.getElementById("speed").value;
autoTimer = setInterval(() => {
if (!step()) {
autoRunning = false;
clearInterval(autoTimer);
document.getElementById("autoBtn").textContent = "Auto Run";
}
}, speed);
}
}
document.getElementById("stepBtn").addEventListener("click", step);
document.getElementById("autoBtn").addEventListener("click", autoRun);
document.getElementById("resetBtn").addEventListener("click", reset);
initializeQueue();
draw();
</script>
</body>
</html>