-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0013_roman_to_integer.html
More file actions
285 lines (245 loc) · 11 KB
/
0013_roman_to_integer.html
File metadata and controls
285 lines (245 loc) · 11 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Roman to Integer - LeetCode 13</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">#13</span> Roman to Integer</h1>
<p>Convert a Roman numeral to an integer.</p>
<div class="problem-meta">
<span class="meta-tag">Math</span>
<span class="meta-tag">String</span>
<span class="meta-tag">Easy</span>
</div>
<div class="file-ref">
📄 Python: <code>python/0013_roman_to_integer/0013_roman_to_integer.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="controls">
<button id="stepBtn">Step</button>
<button id="autoBtn">Auto Run</button>
<button id="resetBtn">Reset</button>
</div>
<svg id="mainSvg" width="800" height="380"></svg>
<div class="status-message" id="status">Click "Step" to convert Roman to integer</div>
</div>
<div class="code-section">
<h3>💻 Python Solution</h3>
<div class="code-block">
<pre><span class="keyword">def</span> <span class="function">romanToInt</span>(s: <span class="class-name">str</span>) -> int:
values = {<span class="string">'I'</span>: <span class="number">1</span>, <span class="string">'V'</span>: <span class="number">5</span>, <span class="string">'X'</span>: <span class="number">10</span>, <span class="string">'L'</span>: <span class="number">50</span>,
<span class="string">'C'</span>: <span class="number">100</span>, <span class="string">'D'</span>: <span class="number">500</span>, <span class="string">'M'</span>: <span class="number">1000</span>}
total = <span class="number">0</span>
prev_value = <span class="number">0</span>
<span class="comment"># Process right to left</span>
<span class="keyword">for</span> char <span class="keyword">in</span> <span class="function">reversed</span>(s):
curr = values[char]
<span class="keyword">if</span> curr >= prev_value:
total += curr
<span class="keyword">else</span>:
total -= curr <span class="comment"># Subtraction case</span>
prev_value = curr
<span class="keyword">return</span> total</pre>
</div>
</div>
</div>
<script>
const romanValues = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000};
const testCases = ["MCMXCIV", "III", "LVIII", "IX"];
let s = "MCMXCIV";
let i;
let total = 0;
let prevValue = 0;
let phase = 'processing';
const width = 800, height = 380;
const svg = d3.select("#mainSvg");
let autoTimer = null;
let autoRunning = false;
function draw() {
svg.selectAll("*").remove();
const cellWidth = 60, startX = 100, startY = 80;
// Title
svg.append("text")
.attr("x", width / 2).attr("y", 30)
.attr("text-anchor", "middle")
.attr("font-weight", "bold")
.text(`Converting "${s}" to Integer (Right to Left)`);
// Draw Roman characters
for (let idx = 0; idx < s.length; idx++) {
const x = startX + idx * cellWidth;
const isProcessed = idx > i;
const isCurrent = idx === i;
svg.append("rect")
.attr("x", x).attr("y", startY)
.attr("width", cellWidth - 5).attr("height", 55)
.attr("rx", 8)
.attr("fill", isCurrent ? "#fef3c7" : (isProcessed ? "#d1fae5" : "#e3f2fd"))
.attr("stroke", isCurrent ? "#f59e0b" : (isProcessed ? "#10b981" : "#1976d2"))
.attr("stroke-width", isCurrent ? 3 : 2);
svg.append("text")
.attr("x", x + (cellWidth - 5) / 2)
.attr("y", startY + 30)
.attr("text-anchor", "middle")
.attr("font-size", "26px")
.attr("font-weight", "bold")
.text(s[idx]);
svg.append("text")
.attr("x", x + (cellWidth - 5) / 2)
.attr("y", startY + 48)
.attr("text-anchor", "middle")
.attr("font-size", "12px")
.attr("fill", "#666")
.text(romanValues[s[idx]]);
}
// Direction arrow
svg.append("text")
.attr("x", startX + s.length * cellWidth + 20)
.attr("y", startY + 30)
.attr("font-size", "24px")
.text("←");
svg.append("text")
.attr("x", startX + s.length * cellWidth + 50)
.attr("y", startY + 35)
.attr("font-size", "12px")
.attr("fill", "#666")
.text("Direction");
// Variables display
const varsY = 180;
const vars = [
{ name: "Current Value", value: i >= 0 ? romanValues[s[i]] : "-" },
{ name: "Previous Value", value: prevValue },
{ name: "Total", value: total }
];
vars.forEach((v, idx) => {
const x = 100 + idx * 200;
svg.append("rect")
.attr("x", x).attr("y", varsY)
.attr("width", 160).attr("height", 60)
.attr("rx", 10)
.attr("fill", idx === 2 ? "#e8f5e9" : "#f5f5f5")
.attr("stroke", idx === 2 ? "#4caf50" : "#ddd");
svg.append("text")
.attr("x", x + 80).attr("y", varsY + 22)
.attr("text-anchor", "middle")
.attr("font-size", "12px")
.attr("fill", "#666")
.text(v.name);
svg.append("text")
.attr("x", x + 80).attr("y", varsY + 48)
.attr("text-anchor", "middle")
.attr("font-size", "22px")
.attr("font-weight", "bold")
.text(v.value);
});
// Logic explanation
if (i >= 0 && phase === 'processing') {
const curr = romanValues[s[i]];
const logicY = 280;
const isSubtraction = curr < prevValue;
svg.append("rect")
.attr("x", 150).attr("y", logicY)
.attr("width", 500).attr("height", 45)
.attr("rx", 8)
.attr("fill", isSubtraction ? "#fee2e2" : "#d1fae5")
.attr("stroke", isSubtraction ? "#ef4444" : "#10b981");
svg.append("text")
.attr("x", 400).attr("y", logicY + 28)
.attr("text-anchor", "middle")
.attr("font-size", "14px")
.text(isSubtraction
? `${curr} < ${prevValue}: SUBTRACT → total = ${total} - ${curr}`
: `${curr} ≥ ${prevValue}: ADD → total = ${total} + ${curr}`);
}
// Final result
if (phase === 'done') {
svg.append("rect")
.attr("x", width / 2 - 120).attr("y", 310)
.attr("width", 240).attr("height", 55)
.attr("rx", 12)
.attr("fill", "#d1fae5").attr("stroke", "#10b981")
.attr("stroke-width", 2);
svg.append("text")
.attr("x", width / 2).attr("y", 345)
.attr("text-anchor", "middle")
.attr("font-size", "24px")
.attr("font-weight", "bold")
.attr("fill", "#10b981")
.text(`${s} = ${total}`);
}
}
function step() {
if (phase === 'done') return false;
if (i < 0) {
phase = 'done';
document.getElementById("status").textContent = `Done! ${s} = ${total}`;
draw();
return false;
}
const curr = romanValues[s[i]];
if (curr >= prevValue) {
total += curr;
document.getElementById("status").textContent =
`'${s[i]}' (${curr}) ≥ prev (${prevValue}): Add ${curr}. Total = ${total}`;
} else {
total -= curr;
document.getElementById("status").textContent =
`'${s[i]}' (${curr}) < prev (${prevValue}): Subtract ${curr}. Total = ${total}`;
}
prevValue = curr;
i--;
draw();
return i >= 0;
}
function reset() {
i = s.length - 1;
total = 0;
prevValue = 0;
phase = 'processing';
if (autoTimer) clearInterval(autoTimer);
autoRunning = false;
document.getElementById("autoBtn").textContent = "Auto Run";
document.getElementById("status").textContent = 'Click "Step" to convert Roman to integer';
draw();
}
function autoRun() {
if (autoRunning) {
clearInterval(autoTimer);
autoRunning = false;
document.getElementById("autoBtn").textContent = "Auto Run";
} else {
autoRunning = true;
document.getElementById("autoBtn").textContent = "Pause";
autoTimer = setInterval(() => {
if (!step()) {
clearInterval(autoTimer);
autoRunning = false;
document.getElementById("autoBtn").textContent = "Auto Run";
}
}, 800);
}
}
document.getElementById("stepBtn").addEventListener("click", step);
document.getElementById("autoBtn").addEventListener("click", autoRun);
document.getElementById("resetBtn").addEventListener("click", reset);
reset();
</script>
</body>
</html>