170 lines
7.7 KiB
HTML
170 lines
7.7 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-Hans">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>007. 接雨水 – 图解</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>🔴 007. 接雨水 <span class="badge hard">困难</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: [0,1,0,2,1,0,1,3,2,1,2,1], label: '示例1: [0,1,0,2,1,0,1,3,2,1,2,1]'},
|
||
{input: [4,2,0,3,2,5], label: '示例2: [4,2,0,3,2,5]'},
|
||
];
|
||
let height, steps, stepCtrl;
|
||
|
||
function buildSteps(arr) {
|
||
height = [...arr]; steps = [];
|
||
let left = 0, right = arr.length - 1;
|
||
let leftMax = arr[left], rightMax = arr[right];
|
||
let totalWater = 0;
|
||
const waterAt = new Array(arr.length).fill(0);
|
||
steps.push({stage:'init', msg:`双指针从两端开始,left_max=${leftMax},right_max=${rightMax}`, left, right, leftMax, rightMax, totalWater, waterAt:[...waterAt]});
|
||
while (left < right) {
|
||
if (leftMax <= rightMax) {
|
||
const water = Math.max(0, leftMax - arr[left]);
|
||
waterAt[left] = water; totalWater += water;
|
||
steps.push({stage:'calcL', msg:`左端: min(${leftMax},${rightMax})=${leftMax}, h[${left}]=${arr[left]}, 储水=${water}`, left, right, leftMax, rightMax, totalWater, waterAt:[...waterAt]});
|
||
left++; leftMax = Math.max(leftMax, arr[left]);
|
||
} else {
|
||
const water = Math.max(0, rightMax - arr[right]);
|
||
waterAt[right] = water; totalWater += water;
|
||
steps.push({stage:'calcR', msg:`右端: min(${leftMax},${rightMax})=${rightMax}, h[${right}]=${arr[right]}, 储水=${water}`, left, right, leftMax, rightMax, totalWater, waterAt:[...waterAt]});
|
||
right--; rightMax = Math.max(rightMax, arr[right]);
|
||
}
|
||
}
|
||
steps.push({stage:'done', msg:`总储水量=${totalWater}`, left, right, leftMax, rightMax, totalWater, waterAt:[...waterAt]});
|
||
}
|
||
|
||
function render(step) {
|
||
const s = steps[step];
|
||
const maxH = Math.max(...height);
|
||
const chartH = 160; const unitH = chartH / (maxH||1); const barW = 36;
|
||
let viz = '<div style="position:relative;display:flex;align-items:flex-end;gap:2px;height:' + (chartH+30) + 'px;padding:0 4px;margin-top:8px;">';
|
||
height.forEach((h, i) => {
|
||
const barH = h * unitH; const waterH = s.waterAt[i] * unitH;
|
||
const isL = i===s.left, isR = i===s.right;
|
||
const barBg = isL ? 'var(--blue)' : isR ? 'var(--purple)' : '#94a3b8';
|
||
viz += `<div style="position:relative;width:${barW}px;display:flex;flex-direction:column;align-items:stretch;">`;
|
||
if (waterH > 0) viz += `<div style="height:${waterH}px;background:rgba(59,130,246,0.25);border-radius:2px 2px 0 0;border-top:2px solid rgba(59,130,246,0.5);"></div>`;
|
||
viz += `<div style="height:${barH}px;background:${barBg};border-radius:3px 3px 0 0;display:flex;align-items:flex-start;justify-content:center;font-size:11px;font-weight:700;color:${isL||isR?'white':'#334155'};padding-top:2px;min-height:2px;">${h>0?h:''}</div>`;
|
||
viz += `<div style="text-align:center;font-size:10px;color:var(--text-muted);margin-top:2px;">${i}</div></div>`;
|
||
});
|
||
viz += '</div>';
|
||
viz += `<div style="margin-top:10px;display:flex;gap:16px;"><span style="color:var(--blue);">■ left_max=${s.leftMax}</span><span style="color:var(--purple);">■ right_max=${s.rightMax}</span><span style="color:var(--green);">💧 储水=${s.totalWater}</span></div>`;
|
||
$('vizArea').innerHTML = viz;
|
||
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
|
||
if (s.stage === 'done') $('resultContent').innerHTML = `<div class="final-answer">总储水量 = <b>${s.totalWater}</b></div>`;
|
||
$('hintText').textContent = s.msg;
|
||
const stages = ['init→初始化','calcL→左端','calcR→右端','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 = '[0,1,0,2,1,0,1,3,2,1,2,1]';
|
||
buildSteps(examples[0].input);
|
||
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 = () => {
|
||
try { const arr = JSON.parse($('inputArea').value); buildSteps(arr); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0); $('stepInfo').textContent = '步骤 1 / ' + steps.length; }
|
||
catch(e) { alert('请输入合法 JSON 数组'); }
|
||
};
|
||
$('exampleSelect').onchange = () => {
|
||
const e = examples[parseInt($('exampleSelect').value)];
|
||
$('inputArea').value = JSON.stringify(e.input); buildSteps(e.input); 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 trap(height):
|
||
left, right = 0, len(height) - 1
|
||
left_max, right_max = height[left], height[right]
|
||
water = 0
|
||
while left < right:
|
||
if left_max <= right_max:
|
||
water += left_max - height[left]
|
||
left += 1
|
||
left_max = max(left_max, height[left])
|
||
else:
|
||
water += right_max - height[right]
|
||
right -= 1
|
||
right_max = max(right_max, height[right])
|
||
return water`, {lang:'Python'});
|
||
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html> |