-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0846_hand_of_straights.html
More file actions
386 lines (341 loc) · 15 KB
/
0846_hand_of_straights.html
File metadata and controls
386 lines (341 loc) · 15 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hand of Straights - LeetCode 846</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">#0846</span> Hand of Straights</h1>
<p><strong>Problem:</strong> Determine if you can rearrange cards into groups of size W, where each group contains W consecutive cards.</p>
<p><strong>Pattern:</strong> Greedy + Hash Map - Start with smallest card, try to form consecutive groups</p>
<div class="problem-meta">
<span class="meta-tag">💰 Greedy</span>
<span class="meta-tag">⏱️ O(n)</span>
</div>
<div class="file-ref">
📄 Python: <code>python/0846_hand_of_straights/0846_hand_of_straights.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</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 form groups</div>
<div class="variables">
<div class="var-item">
<span class="var-label">Hand:</span>
<span id="handDisplay">[1,2,3,6,2,3,4,7,8]</span>
</div>
<div class="var-item">
<span class="var-label">Group Size:</span>
<span id="groupSizeDisplay">3</span>
</div>
<div class="var-item">
<span class="var-label">Groups Formed:</span>
<span id="groupsDisplay">0</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">isNStraightHand</span>(hand, groupSize):
<span class="string">"""
Greedy: form groups starting from smallest.
Time: O(n log n), Space: O(n)
"""</span>
<span class="keyword">if</span> <span class="function">len</span>(hand) % groupSize != <span class="number">0</span>:
<span class="keyword">return</span> <span class="keyword">False</span>
count = <span class="function">Counter</span>(hand)
<span class="keyword">for</span> card <span class="keyword">in</span> <span class="function">sorted</span>(count):
<span class="keyword">if</span> count[card] > <span class="number">0</span>:
<span class="comment"># Try to form groups starting with this card</span>
freq = count[card]
<span class="keyword">for</span> i <span class="keyword">in</span> <span class="function">range</span>(groupSize):
<span class="keyword">if</span> count[card + i] < freq:
<span class="keyword">return</span> <span class="keyword">False</span>
count[card + i] -= freq
<span class="keyword">return</span> <span class="keyword">True</span></pre>
</div>
</div>
</div>
<script>
const hand = [1, 2, 3, 6, 2, 3, 4, 7, 8];
const groupSize = 3;
let count = {};
let sorted = [];
let sortedIdx = 0;
let currentGroup = [];
let groups = [];
let phase = 'init'; // 'init' | 'forming' | 'done'
let cardStates = {}; // card -> count remaining
let canForm = null;
let autoRunning = false;
let autoTimer = null;
const width = 750;
const height = 420;
const svg = d3.select("#mainSvg")
.attr("width", width)
.attr("height", height);
function init() {
count = {};
hand.forEach(card => {
count[card] = (count[card] || 0) + 1;
});
sorted = [...new Set(hand)].sort((a, b) => a - b);
cardStates = {...count};
}
function draw() {
svg.selectAll("*").remove();
svg.append("text")
.attr("x", width / 2).attr("y", 25)
.attr("text-anchor", "middle")
.attr("font-weight", "bold")
.text(`Forming Groups of ${groupSize} Consecutive Cards`);
// Draw card counts
svg.append("text")
.attr("x", 50).attr("y", 60)
.attr("font-weight", "bold")
.text("Card Counts:");
const cardX = 50;
sorted.forEach((card, i) => {
const x = cardX + i * 80;
const remaining = cardStates[card] || 0;
const isActive = phase === 'forming' &&
currentGroup.length < groupSize &&
currentGroup.length > 0 &&
card === currentGroup[0] + currentGroup.length;
// Card
svg.append("rect")
.attr("x", x).attr("y", 75)
.attr("width", 50).attr("height", 70)
.attr("rx", 8)
.attr("fill", remaining === 0 ? "#e0e0e0" : isActive ? "#ffeb3b" : "#fff")
.attr("stroke", isActive ? "#f57c00" : "#1976d2")
.attr("stroke-width", 2);
svg.append("text")
.attr("x", x + 25).attr("y", 110)
.attr("text-anchor", "middle")
.attr("font-size", "24px")
.attr("font-weight", "bold")
.text(card);
// Count badge
svg.append("circle")
.attr("cx", x + 45).attr("cy", 80)
.attr("r", 12)
.attr("fill", remaining > 0 ? "#4caf50" : "#e0e0e0")
.attr("stroke", "#fff");
svg.append("text")
.attr("x", x + 45).attr("y", 85)
.attr("text-anchor", "middle")
.attr("font-size", "12px")
.attr("fill", "#fff")
.attr("font-weight", "bold")
.text(remaining);
});
// Current group being formed
if (currentGroup.length > 0) {
svg.append("text")
.attr("x", 50).attr("y", 180)
.attr("font-weight", "bold")
.text("Forming Group:");
currentGroup.forEach((card, i) => {
svg.append("rect")
.attr("x", 170 + i * 55).attr("y", 165)
.attr("width", 45).attr("height", 60)
.attr("rx", 6)
.attr("fill", "#fff3e0").attr("stroke", "#ff9800")
.attr("stroke-width", 2);
svg.append("text")
.attr("x", 192 + i * 55).attr("y", 200)
.attr("text-anchor", "middle")
.attr("font-size", "20px")
.attr("font-weight", "bold")
.text(card);
});
// Remaining slots
for (let i = currentGroup.length; i < groupSize; i++) {
svg.append("rect")
.attr("x", 170 + i * 55).attr("y", 165)
.attr("width", 45).attr("height", 60)
.attr("rx", 6)
.attr("fill", "none").attr("stroke", "#ddd")
.attr("stroke-width", 2)
.attr("stroke-dasharray", "5,5");
svg.append("text")
.attr("x", 192 + i * 55).attr("y", 200)
.attr("text-anchor", "middle")
.attr("font-size", "16px")
.attr("fill", "#999")
.text("?");
}
}
// Completed groups
svg.append("text")
.attr("x", 50).attr("y", 270)
.attr("font-weight", "bold")
.text("Completed Groups:");
groups.forEach((group, gIdx) => {
group.forEach((card, i) => {
svg.append("rect")
.attr("x", 50 + gIdx * (groupSize * 45 + 30) + i * 40).attr("y", 280)
.attr("width", 35).attr("height", 50)
.attr("rx", 5)
.attr("fill", "#c8e6c9").attr("stroke", "#4caf50");
svg.append("text")
.attr("x", 67 + gIdx * (groupSize * 45 + 30) + i * 40).attr("y", 310)
.attr("text-anchor", "middle")
.attr("font-size", "16px")
.attr("font-weight", "bold")
.text(card);
});
});
// Result
if (canForm !== null) {
svg.append("rect")
.attr("x", width - 180).attr("y", height - 70)
.attr("width", 170).attr("height", 45)
.attr("rx", 10)
.attr("fill", canForm ? "#c8e6c9" : "#ffcdd2")
.attr("stroke", canForm ? "#4caf50" : "#e53935");
svg.append("text")
.attr("x", width - 95).attr("y", height - 40)
.attr("text-anchor", "middle")
.attr("font-size", "16px")
.attr("font-weight", "bold")
.text(canForm ? "✓ Can Form!" : "✗ Cannot Form!");
}
}
function step() {
if (phase === 'init') {
init();
phase = 'forming';
document.getElementById("status").textContent =
"Initialized card counts. Starting to form groups...";
draw();
return true;
}
if (phase === 'forming') {
// Find smallest card with remaining count
let startCard = null;
for (const card of sorted) {
if (cardStates[card] > 0) {
startCard = card;
break;
}
}
if (startCard === null) {
// All cards used
canForm = true;
phase = 'done';
document.getElementById("status").textContent =
`Success! Formed ${groups.length} groups of ${groupSize}.`;
draw();
return false;
}
if (currentGroup.length === 0) {
// Start new group
currentGroup = [startCard];
cardStates[startCard]--;
document.getElementById("status").textContent =
`Starting new group with smallest card: ${startCard}`;
} else {
// Try to add next consecutive card
const nextCard = currentGroup[currentGroup.length - 1] + 1;
if (cardStates[nextCard] > 0) {
currentGroup.push(nextCard);
cardStates[nextCard]--;
document.getElementById("status").textContent =
`Added ${nextCard} to group. Group: [${currentGroup.join(', ')}]`;
} else {
// Cannot form group
canForm = false;
phase = 'done';
document.getElementById("status").textContent =
`Cannot find card ${nextCard}! Cannot form valid groups.`;
draw();
return false;
}
}
// Check if group is complete
if (currentGroup.length === groupSize) {
groups.push([...currentGroup]);
document.getElementById("groupsDisplay").textContent = groups.length;
document.getElementById("status").textContent =
`Completed group: [${currentGroup.join(', ')}]`;
currentGroup = [];
}
draw();
return true;
}
return false;
}
function reset() {
count = {};
sorted = [];
sortedIdx = 0;
currentGroup = [];
groups = [];
phase = 'init';
cardStates = {};
canForm = null;
autoRunning = false;
if (autoTimer) clearInterval(autoTimer);
document.getElementById("groupsDisplay").textContent = "0";
document.getElementById("status").textContent =
'Click "Step" to form groups';
document.getElementById("autoBtn").textContent = "Auto Run";
init();
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);
init();
draw();
</script>
</body>
</html>