Files
illustrated-algorithm/jump-game-ii/index.html
T
2026-08-24 04:35:13 +00:00

154 lines
7.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>079. 跳跃游戏 II – 图解</title>
<link rel="stylesheet" href="../shared/style.css">
<style>.vis-area { min-height: 120px; padding: 16px 0; } .code-section { margin-top: 16px; }
.seg{display:inline-flex;align-items:center;justify-content:center;padding:4px 2px;border-radius:6px 6px 0 0;font-size:12px;font-weight:700;color:white;transition:all .25s;min-width:36px;}
</style>
</head>
<body><div class="container">
<h1>🟡 079. 跳跃游戏 II <span class="badge medium">中等</span></h1>
<p class="subtitle">分类:贪心 | LeetCode Hot 100</p>
<div class="controls" id="controls">
<label>输入:</label><input type="text" id="inputArea" value="[2,3,1,1,4]">
<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(){
const examples = [
{input:[2,3,1,1,4], label:'示例1: [2,3,1,1,4]'},
{input:[2,3,0,1,4], label:'示例2: [2,3,0,1,4]'},
{input:[1,2,1,1,1], label:'示例3: [1,2,1,1,1]'},
];
const COLORS = ['#3b82f6','#8b5cf6','#f59e0b','#ef4444','#10b981','#06b6d4'];
let nums, steps, stepCtrl;
function buildSteps(arr) {
nums = [...arr]; steps = [];
let jumps = 0, curEnd = 0, farthest = 0;
const segs = [];
steps.push({stage:'init', msg:`初始化:jumps=0, 当前边界=0, 最远可达=0`, i:0, jumps, curEnd, farthest, segs:[]});
for (let i = 0; i < arr.length - 1; i++) {
farthest = Math.max(farthest, i + arr[i]);
steps.push({stage:'reach', msg:`i=${i}: nums[${i}]=${arr[i]},可达 i+${arr[i]}=${i+arr[i]},最远可达=${farthest}`, i, jumps, curEnd, farthest, segs:JSON.parse(JSON.stringify(segs))});
if (i === curEnd) {
segs.push({start: jumps===0?0:segs[segs.length-1].end+1, end: i, color:COLORS[jumps%COLORS.length]});
jumps++;
curEnd = farthest;
steps.push({stage:'jump', msg:`到达当前边界 i=${i},跳跃!jumps=${jumps},新边界=${curEnd}`, i, jumps, curEnd, farthest, segs:JSON.parse(JSON.stringify(segs))});
}
}
// last segment
if (segs.length===0 || segs[segs.length-1].end < nums.length-1) {
segs.push({start: segs.length?segs[segs.length-1].end+1:0, end:nums.length-1, color:COLORS[jumps%COLORS.length]});
}
steps.push({stage:'done', msg:`完成!最少跳跃次数 = ${jumps}`, i:nums.length-1, jumps, curEnd, farthest, segs});
}
function render(step) {
const s = steps[step];
// segments visualization
let viz = '<div style="margin-bottom:12px;">';
// array with colored segments
viz += '<div class="nums-line">';
nums.forEach((v,i) => {
const seg = s.segs.find(sg => i >= sg.start && i <= sg.end);
let cls = 'default';
if (s.stage !== 'init') {
if (i === s.i) cls = 'active';
}
viz += `<span class="chip ${cls}" style="min-width:38px;${seg?'border-bottom:3px solid '+seg.color+';':''}">${v}</span>`;
});
viz += '</div>';
// jump range bar
if (s.segs.length > 0) {
viz += '<div style="display:flex;height:28px;border-radius:6px;overflow:hidden;margin-top:8px;">';
s.segs.forEach((sg,idx) => {
const pct = ((sg.end - sg.start + 1) / nums.length * 100);
viz += `<div class="seg" style="width:${pct}%;background:${sg.color};">第${idx+1}跳</div>`;
});
const covered = s.segs.reduce((a,s)=>a+s.end-s.start+1, 0);
if (covered < nums.length) {
viz += `<div class="seg" style="width:${(nums.length-covered)/nums.length*100}%;background:#e2e8f0;color:#94a3b8;">待跳</div>`;
}
viz += '</div>';
}
viz += '</div>';
viz += `<div style="margin-top:8px;font-size:14px;color:#475569;">
<b>jumps</b>=${s.jumps} <b>当前边界</b>=${s.curEnd} <b>最远可达</b>=${s.farthest}
</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.jumps}</b></div>
<div class="complexity">时间复杂度 O(n),空间复杂度 O(1)</div>`;
}
$('hintText').textContent = s.msg;
const stages = ['init→初始化','reach→可达范围','jump→跳跃','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>`);
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); }
catch(e) { alert('请输入合法JSON数组'); }
};
$('exampleSelect').onchange = () => {
const e = examples[+$('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 jump(nums):
jumps = cur_end = farthest = 0
for i in range(len(nums) - 1):
farthest = max(farthest, i + nums[i])
if i == cur_end:
jumps += 1
cur_end = farthest
return jumps`, {lang:'Python'});
})();
</script>
</body></html>