feat: LeetCode Hot 100 - 100道题完整交互式图解页面
Deploy / deploy (push) Successful in 7s

This commit is contained in:
2026-08-24 04:35:13 +00:00
parent e4fe5afb80
commit 4f830ad352
114 changed files with 32137 additions and 82 deletions
+201
View File
@@ -0,0 +1,201 @@
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>085. 零钱兑换 – 图解</title>
<link rel="stylesheet" href="../shared/style.css">
<style>
/* page-specific overrides */
.vis-area { min-height: 120px; padding: 16px 0; }
.code-section { margin-top: 16px; }
</style>
</head>
<body>
<div class="container">
<h1>🟡 085. 零钱兑换 <span class="badge medium">中等</span></h1>
<p class="subtitle">分类:动态规划 | LeetCode Hot 100</p>
<!-- 控制面板 -->
<div class="controls" id="controls">
<label for="inputArea">输入:</label>
<input type="text" id="inputArea" placeholder="默认示例,可自定义">
<button id="applyBtn" class="primary">生成图解</button>
<select id="exampleSelect"></select>
<span style="flex:1"></span>
<button id="prevBtn">◀ 上一步</button>
<button id="nextBtn">下一步 ▶</button>
<button id="jumpBtn">⏭ 跳到结果</button>
<button id="autoBtn">自动播放</button>
<button id="resetBtn">重置</button>
</div>
<!-- 步骤流水线 -->
<div class="pipeline" id="pipeline"></div>
<!-- 提示条 -->
<div class="hint info" id="hintBox">
<span id="stepInfo"></span><br>
<span id="hintText"></span>
</div>
<!-- 可视化面板 -->
<div class="panels">
<div class="panel" id="mainPanel">
<h3>📊 可视化</h3>
<div class="vis-area" id="vizArea">点击「生成图解」开始</div>
</div>
<div class="panel-grid">
<div class="panel" id="detailPanel">
<h3>📝 当前步骤详情</h3>
<div id="detailContent">等待开始...</div>
</div>
<div class="panel" id="resultPanel">
<h3>✅ 结果</h3>
<div id="resultContent">等待完成...</div>
</div>
</div>
</div>
<!-- 代码 -->
<div class="panel code-section">
<h3>💻 参考代码(Python)</h3>
<div id="codeArea"></div>
</div>
<footer>Powered by QwenPaw · 图解算法 · LeetCode Hot 100</footer>
</div>
<script src="../shared/algo-viz.js"></script>
<script>
"use strict";
(function() {
// ========== Algorithm Logic ==========
const examples = [
{input: [1,2,5], amount: 11, label: '示例1: coins=[1,2,5], amount=11'},
{input: [2], amount: 3, label: '示例2: coins=[2], amount=3'},
{input: [1], amount: 0, label: '示例3: coins=[1], amount=0'},
];
let coins, amount, steps, stepCtrl;
function buildSteps(c, amt) {
coins = [...c]; amount = amt; steps = [];
const dp = new Array(amt + 1).fill(Infinity);
dp[0] = 0;
steps.push({stage:'init', msg:'初始化 dp[0]=0,其余为 ∞', dp:[...dp], current:-1, bestCoin:-1, candidates:[]});
for (let i = 1; i <= amt; i++) {
let bestCoin = -1, bestVal = Infinity;
const candidates = [];
for (const coin of coins) {
if (coin <= i) {
const val = dp[i - coin] + 1;
candidates.push({coin, prev: i - coin, val});
if (val < bestVal) { bestVal = val; bestCoin = coin; }
}
}
steps.push({stage:'try', msg:`计算 dp[${i}]:尝试所有硬币`, dp:[...dp], current:i, bestCoin:-1, candidates});
dp[i] = bestVal;
if (bestCoin > 0) {
steps.push({stage:'fill', msg:`dp[${i}] = dp[${i-bestCoin}]+1 = ${dp[i-bestCoin]}+1 = ${bestVal}(选硬币 ${bestCoin})`, dp:[...dp], current:i, bestCoin, candidates});
} else {
steps.push({stage:'fill', msg:`dp[${i}] = ∞(无法凑出)`, dp:[...dp], current:i, bestCoin:-1, candidates});
}
}
steps.push({stage:'done', msg:dp[amt]===Infinity?`无法凑出金额 ${amt}`:`凑出金额 ${amt} 最少需要 ${dp[amt]} 枚硬币`, dp:[...dp], current:amt, bestCoin:-1, candidates:[]});
}
function render(step) {
const s = steps[step];
let viz = '<div style="margin-top:8px;"><b>金额轴 & dp 表:</b></div>';
viz += '<div style="display:flex;flex-wrap:wrap;gap:4px;margin:8px 0;">';
for (let i = 0; i <= amount; i++) {
const cls = i === s.current ? (s.stage==='try'?'orange':'green') : 'default';
const val = s.dp[i] === Infinity ? '∞' : s.dp[i];
viz += `<span class="chip-group"><span class="chip ${cls}" style="min-width:36px;font-size:13px;">${val}</span><span class="chip-index">${i}</span></span>`;
}
viz += '</div>';
viz += '<div style="margin-top:8px;"><b>硬币面额:</b></div>';
viz += '<div style="display:flex;gap:6px;margin:4px 0;">';
coins.forEach(c => {
const isBest = c === s.bestCoin;
const cls = isBest ? 'green' : 'default';
viz += `<span class="chip ${cls}" style="border-radius:50%;min-width:36px;height:36px;">${c}</span>`;
});
viz += '</div>';
if (s.candidates && s.candidates.length > 0) {
viz += '<div style="margin-top:8px;"><b>候选:</b></div>';
viz += '<div style="display:flex;gap:6px;flex-wrap:wrap;margin:4px 0;">';
s.candidates.forEach(c => {
const isBest = c.coin === s.bestCoin;
const bg = isBest ? '#dcfce7' : '#f0f9ff';
const bd = isBest ? '2px solid #16a34a' : '1px solid #cbd5e1';
viz += `<span style="padding:4px 10px;background:${bg};border:${bd};border-radius:8px;font-size:13px;">用${c.coin} → dp[${c.prev}]+1=${c.val===Infinity?'∞':c.val}</span>`;
});
viz += '</div>';
}
$('vizArea').innerHTML = viz;
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
if (s.current >= 0 && s.dp[s.current] !== Infinity) {
$('detailContent').innerHTML += `<div class="current-answer">dp[${s.current}] = <b>${s.dp[s.current]}</b></div>`;
}
if (s.stage === 'done') {
if (s.dp[amount] === Infinity) {
$('resultContent').innerHTML = '<div class="final-answer" style="border-color:#f87171;background:#fef2f2;">无法凑出该金额,返回 <b>-1</b></div>';
} else {
$('resultContent').innerHTML = `<div class="final-answer">最少需要 <b>${s.dp[amount]}</b> 枚硬币</div>`;
}
}
$('hintText').textContent = s.msg;
const stages = ['init→初始化','try→尝试','fill→填充','done→完成'];
$('pipeline').innerHTML = stages.map(st => {
const [k,l] = st.split('→');
return `<span class="pipe-step ${s.stage===k?'active':''}">${l}</span>`;
}).join('<i>→</i>');
}
function init() {
const sel = $('exampleSelect');
examples.forEach((e,i) => { sel.innerHTML += `<option value="${i}">${e.label}</option>`; });
$('inputArea').value = 'coins=[1,2,5], amount=11';
buildSteps(examples[0].input, examples[0].amount);
stepCtrl = new StepController({onStep: render});
stepCtrl.setSteps(steps.map((_,i)=>i));
$('stepInfo').textContent = '步骤 1 / ' + steps.length;
stepCtrl.onStep = (idx) => { render(idx); $('stepInfo').textContent = `步骤 ${idx+1} / ${steps.length}`; };
$('applyBtn').onclick = () => {
const m = $('inputArea').value.match(/coins=\[([^\]]+)\].*amount=(\d+)/);
if (!m) { alert('格式: coins=[1,2,5], amount=11'); return; }
const c = m[1].split(',').map(Number);
buildSteps(c, parseInt(m[2]));
stepCtrl.setSteps(steps.map((_,i)=>i)); render(0); $('stepInfo').textContent = '步骤 1 / ' + steps.length;
};
$('exampleSelect').onchange = () => {
const e = examples[parseInt($('exampleSelect').value)];
$('inputArea').value = `coins=[${e.input}], amount=${e.amount}`;
buildSteps(e.input, e.amount); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0);
};
$('prevBtn').onclick = () => stepCtrl.prev();
$('nextBtn').onclick = () => stepCtrl.next();
$('jumpBtn').onclick = () => stepCtrl.jumpToEnd();
$('autoBtn').onclick = () => { const on = stepCtrl.toggleAuto(); $('autoBtn').textContent = on ? '暂停' : '自动播放'; };
$('resetBtn').onclick = () => { stepCtrl.reset(); $('autoBtn').textContent = '自动播放'; };
}
init();
$("codeArea").innerHTML = renderCode(
`def coinChange(coins, amount):\n` +
` dp = [float('inf')] * (amount + 1)\n` +
` dp[0] = 0\n` +
` for i in range(1, amount + 1):\n` +
` for coin in coins:\n` +
` if coin <= i:\n` +
` dp[i] = min(dp[i], dp[i - coin] + 1)\n` +
` return dp[amount] if dp[amount] != float('inf') else -1`,
{lang:'Python'}
);
})();
</script>
</body>
</html>