mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-29 07:44:46 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -47,7 +47,7 @@
|
||||
set(key, value, storage, key === "__palette" ? shared : scope);
|
||||
})();
|
||||
</script>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css" rel="stylesheet"/></head>
|
||||
<link href="/assets/figure-svg/css/ca08fd1c3a121ac517cf0998.css" rel="stylesheet"/><link href="/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css" rel="stylesheet"/></head>
|
||||
<body data-md-color-accent="teal" data-md-color-primary="white" data-md-color-scheme="default" dir="ltr">
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
|
||||
<input autocomplete="off" class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
|
||||
@@ -1983,8 +1983,8 @@
|
||||
<p>给定一个共有 <span class="arithmatex">\(n\)</span> 阶的楼梯,你每步可以上 <span class="arithmatex">\(1\)</span> 阶或者 <span class="arithmatex">\(2\)</span> 阶,请问有多少种方案可以爬到楼顶?</p>
|
||||
</div>
|
||||
<p>如图 14-1 所示,对于一个 <span class="arithmatex">\(3\)</span> 阶楼梯,共有 <span class="arithmatex">\(3\)</span> 种方案可以爬到楼顶。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.png" data-dark-src="/assets/figure-svg/figures/9e9e0edf12f0646c73472004.json" data-src="/assets/figure-svg/figures/2f4eaf8ca3b7a95cf07cac27.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.svg" data-theme="light" tabindex="-1" type="image/svg+xml">爬到第 3 阶的方案数量</object><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">爬到第 3 阶的方案数量</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-1 爬到第 3 阶的方案数量 </p>
|
||||
<p><ha-svg-figure aria-label="爬到第三阶的三种方案" class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.png" data-dark-src="/assets/figure-svg/figures/9e9e0edf12f0646c73472004.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/2f4eaf8ca3b7a95cf07cac27.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">爬到第 3 阶的方案数量</object><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">爬到第 3 阶的方案数量</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-1 爬到第 3 阶的方案数量 </p><div class="ha-sr-only figure-description"><h2>图解 14-1:爬到第三阶的三种方案</h2><p>三条台阶轨迹展示爬到第 3 阶的不同走法。</p><p>设地面起点编号为 0,每一步可以选择向上爬 1 阶或 2 阶,目标是恰好爬上第 3 阶楼梯。</p><p>楼梯的台阶编号从左到右为 0 至 6,目标第 3 阶的踏面以绿色高亮。三组跳跃弧线都画在同一段楼梯上方,弧线与下方的台阶序列一一对应;图下注明“爬上第 3 阶楼梯共有 3 种方案”。</p><p>青色实线方案按台阶编号 0→1→2→3 前进,即连续三次各上 1 阶;蓝色点线方案按 0→2→3 前进,即先上 2 阶到第 2 阶、再上 1 阶到第 3 阶;橙色虚线方案按 0→1→3 前进,即先上 1 阶到第 1 阶、再上 2 阶到第 3 阶。先上 2 阶再上 1 阶与先上 1 阶再上 2 阶经过不同台阶,计为不同方案。</p></div>
|
||||
<p>本题的目标是求解方案数量,<strong>我们可以考虑通过回溯来穷举所有可能性</strong>。具体来说,将爬楼梯想象为一个多轮选择的过程:从地面出发,每轮选择上 <span class="arithmatex">\(1\)</span> 阶或 <span class="arithmatex">\(2\)</span> 阶,每当到达楼梯顶部时就将方案数量加 <span class="arithmatex">\(1\)</span> ,当越过楼梯顶部时就将其剪枝。代码如下所示:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio"/><input id="__tabbed_1_2" name="__tabbed_1" type="radio"/><input id="__tabbed_1_3" name="__tabbed_1" type="radio"/><input id="__tabbed_1_4" name="__tabbed_1" type="radio"/><input id="__tabbed_1_5" name="__tabbed_1" type="radio"/><input id="__tabbed_1_6" name="__tabbed_1" type="radio"/><input id="__tabbed_1_7" name="__tabbed_1" type="radio"/><input id="__tabbed_1_8" name="__tabbed_1" type="radio"/><input id="__tabbed_1_9" name="__tabbed_1" type="radio"/><input id="__tabbed_1_10" name="__tabbed_1" type="radio"/><input id="__tabbed_1_11" name="__tabbed_1" type="radio"/><input id="__tabbed_1_12" name="__tabbed_1" type="radio"/><input id="__tabbed_1_13" name="__tabbed_1" type="radio"/><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Ruby</label></div>
|
||||
<div class="tabbed-content">
|
||||
@@ -2381,8 +2381,8 @@ dp[i-1], dp[i-2], \dots, dp[2], dp[1]
|
||||
dp[i] = dp[i-1] + dp[i-2]
|
||||
\]</div>
|
||||
<p>这意味着在爬楼梯问题中,各个子问题之间存在递推关系,<strong>原问题的解可以由子问题的解构建得来</strong>。图 14-2 展示了该递推关系。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.png" data-dark-src="/assets/figure-svg/figures/9e038b79af326fb7cb67ffa8.json" data-src="/assets/figure-svg/figures/7d3d6cfdbe157476bbe897f5.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.svg" data-theme="light" tabindex="-1" type="image/svg+xml">方案数量递推关系</object><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">方案数量递推关系</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-2 方案数量递推关系 </p>
|
||||
<p><ha-svg-figure aria-label="第五阶的方案由第三、四阶相加" class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.png" data-dark-src="/assets/figure-svg/figures/9e038b79af326fb7cb67ffa8.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/7d3d6cfdbe157476bbe897f5.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">方案数量递推关系</object><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">方案数量递推关系</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-2 方案数量递推关系 </p><div class="ha-sr-only figure-description"><h3>图解 14-2:第五阶的方案由第三、四阶相加</h3><p>到达第 5 阶的最后一步只能来自第 3 阶或第 4 阶,因此方案数为 3+5=8。</p><p>每一步只能上 1 阶或 2 阶,记 dp[n] 为爬到第 n 阶楼梯的方案数,其中初始状态为 dp[1]=1、dp[2]=2。</p><p>楼梯上第 3、第 4、第 5 阶分别用绿色、青色和蓝色突出显示。3 条绿色虚线汇入第 3 阶(标有 dp[3]=3),5 条青色实线汇入第 4 阶(标有 dp[4]=5),随后分别经一条跨 2 阶的绿色虚线和一条跨 1 阶的青色实线汇入第 5 阶;顶部写出 dp[5]=dp[3]+dp[4]=8,底部对照列出阶数 1 至 6 与对应的方案数。</p><table><caption>图中阶数与方案数</caption><thead><tr><th scope="col">阶数 n</th><th scope="col">方案数 dp[n]</th></tr></thead><tbody><tr><th scope="row">1</th><td>1</td></tr><tr><th scope="row">2</th><td>2</td></tr><tr><th scope="row">3</th><td>3</td></tr><tr><th scope="row">4</th><td>5</td></tr><tr><th scope="row">5</th><td>8</td></tr><tr><th scope="row">6</th><td>13</td></tr></tbody></table><p>要爬到第 5 阶,最后一步要么是从第 3 阶向上跳 2 阶,要么是从第 4 阶向上跳 1 阶。把到达第 3 阶的 3 种走法各接上一次 2 阶跳跃,就得到 3 种到达第 5 阶的走法;把到达第 4 阶的 5 种走法各接上一次 1 阶跳跃,又得到另外 5 种走法。这两类走法的最后一步的来源阶不同,彼此不重不漏,所以 dp[5]=dp[3]+dp[4]=3+5=8。推广到任意第 i 阶(i≥3),都有 dp[i]=dp[i-1]+dp[i-2]。</p></div>
|
||||
<p>我们可以根据递推公式得到暴力搜索解法。以 <span class="arithmatex">\(dp[n]\)</span> 为起始点,<strong>递归地将一个较大问题拆解为两个较小问题的和</strong>,直至到达最小子问题 <span class="arithmatex">\(dp[1]\)</span> 和 <span class="arithmatex">\(dp[2]\)</span> 时返回。其中,最小子问题的解是已知的,即 <span class="arithmatex">\(dp[1] = 1\)</span>、<span class="arithmatex">\(dp[2] = 2\)</span> ,表示爬到第 <span class="arithmatex">\(1\)</span>、<span class="arithmatex">\(2\)</span> 阶分别有 <span class="arithmatex">\(1\)</span>、<span class="arithmatex">\(2\)</span> 种方案。</p>
|
||||
<p>观察以下代码,它和标准回溯代码都属于深度优先搜索,但更加简洁:</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio"/><input id="__tabbed_2_2" name="__tabbed_2" type="radio"/><input id="__tabbed_2_3" name="__tabbed_2" type="radio"/><input id="__tabbed_2_4" name="__tabbed_2" type="radio"/><input id="__tabbed_2_5" name="__tabbed_2" type="radio"/><input id="__tabbed_2_6" name="__tabbed_2" type="radio"/><input id="__tabbed_2_7" name="__tabbed_2" type="radio"/><input id="__tabbed_2_8" name="__tabbed_2" type="radio"/><input id="__tabbed_2_9" name="__tabbed_2" type="radio"/><input id="__tabbed_2_10" name="__tabbed_2" type="radio"/><input id="__tabbed_2_11" name="__tabbed_2" type="radio"/><input id="__tabbed_2_12" name="__tabbed_2" type="radio"/><input id="__tabbed_2_13" name="__tabbed_2" type="radio"/><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label></div>
|
||||
@@ -2611,8 +2611,8 @@ dp[i] = dp[i-1] + dp[i-2]
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20%E5%B7%B2%E7%9F%A5%20dp%5B1%5D%20%E5%92%8C%20dp%5B2%5D%20%EF%BC%8C%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201%29%20%2B%20dfs%28i%20-%202%29%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20return%20dfs%28n%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" rel="noopener noreferrer" target="_blank">全屏观看 ></a></div></p>
|
||||
</details>
|
||||
<p>图 14-3 展示了暴力搜索形成的递归树。对于问题 <span class="arithmatex">\(dp[n]\)</span> ,其递归树的深度为 <span class="arithmatex">\(n\)</span> ,时间复杂度为 <span class="arithmatex">\(O(2^n)\)</span> 。指数阶属于爆炸式增长,如果我们输入一个比较大的 <span class="arithmatex">\(n\)</span> ,则会陷入漫长的等待之中。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.png" data-dark-src="/assets/figure-svg/figures/0aaf5d674fdfe552216be409.json" data-src="/assets/figure-svg/figures/07d31ef0302d262878af4270.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">爬楼梯对应递归树</object><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">爬楼梯对应递归树</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-3 爬楼梯对应递归树 </p>
|
||||
<p><ha-svg-figure aria-label="暴力爬楼梯搜索的重叠子问题" class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.png" data-dark-src="/assets/figure-svg/figures/0aaf5d674fdfe552216be409.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/07d31ef0302d262878af4270.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">爬楼梯对应递归树</object><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">爬楼梯对应递归树</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-3 爬楼梯对应递归树 </p><div class="ha-sr-only figure-description"><h3>图解 14-3:暴力爬楼梯搜索的重叠子问题</h3><p>从 dp[9] 自顶向下暴力递归拆分时,相同的子问题不共享结果,会在不同分支中被反复展开求解。</p><p>爬楼梯问题中每步上 1 阶或 2 阶,dp[i] 表示爬到第 i 阶的方案数。暴力搜索直接按递推式 dp[i] = dp[i − 1] + dp[i − 2] 向下递归分解,直到触及基本情况 dp[1] = 1 或 dp[2] = 2。</p><p>求 dp[9] 时先求 dp[8] 与 dp[7];求 dp[8] 又会求 dp[7] 与 dp[6]。暴力树中的两个 dp[7] 不共享一次计算,每次遇到都会各自再次展开求解。</p><p>图中只画到前四层共 15 个状态节点,底部 16 个省略号表示继续递归至基本情况 dp[1] 或 dp[2]。表中次数(如 dp[5] 出现 4 次)只统计已画出的四层,不代表整个递归过程的总次数。</p><p>下表先列画出的四层,再按状态汇总。分解表的左右子问题按递推式列出;第 4 层各节点的子问题在图中均以省略号表示,并未逐个画出。其中 dp[4]、dp[3] 只出现在第 4 层,分解表中这两行的子问题均由递推式推出,图中未画出。</p><table><caption>已展示的四层递归状态</caption><thead><tr><th scope="col">层(根为第 1 层)</th><th scope="col">状态(从左到右)</th></tr></thead><tbody><tr><th scope="row">1</th><td>dp[9]</td></tr><tr><th scope="row">2</th><td>dp[8]、dp[7]</td></tr><tr><th scope="row">3</th><td>dp[7]、dp[6]、dp[6]、dp[5]</td></tr><tr><th scope="row">4</th><td>dp[6]、dp[5]、dp[5]、dp[4]、dp[5]、dp[4]、dp[4]、dp[3]</td></tr></tbody></table><table><caption>分解关系与前四层出现次数</caption><thead><tr><th scope="col">状态</th><th scope="col">左子问题</th><th scope="col">右子问题</th><th scope="col">前四层出现次数</th></tr></thead><tbody><tr><th scope="row">dp[9]</th><td>dp[8]</td><td>dp[7]</td><td>1</td></tr><tr><th scope="row">dp[8]</th><td>dp[7]</td><td>dp[6]</td><td>1</td></tr><tr><th scope="row">dp[7]</th><td>dp[6]</td><td>dp[5]</td><td>2</td></tr><tr><th scope="row">dp[6]</th><td>dp[5]</td><td>dp[4]</td><td>3</td></tr><tr><th scope="row">dp[5]</th><td>dp[4]</td><td>dp[3]</td><td>4</td></tr><tr><th scope="row">dp[4]</th><td>dp[3]</td><td>dp[2]</td><td>3</td></tr><tr><th scope="row">dp[3]</th><td>dp[2]</td><td>dp[1]</td><td>1</td></tr></tbody></table><p>图底文字将重叠子问题与指数级耗时联系起来:暴力递归反复求解同一状态,时间复杂度为 O(2^n)。</p></div>
|
||||
<p>观察图 14-3 ,<strong>指数阶的时间复杂度是“重叠子问题”导致的</strong>。例如 <span class="arithmatex">\(dp[9]\)</span> 被分解为 <span class="arithmatex">\(dp[8]\)</span> 和 <span class="arithmatex">\(dp[7]\)</span> ,<span class="arithmatex">\(dp[8]\)</span> 被分解为 <span class="arithmatex">\(dp[7]\)</span> 和 <span class="arithmatex">\(dp[6]\)</span> ,两者都包含子问题 <span class="arithmatex">\(dp[7]\)</span> 。</p>
|
||||
<p>以此类推,子问题中包含更小的重叠子问题,子子孙孙无穷尽也。绝大部分计算资源都浪费在这些重叠的子问题上。</p>
|
||||
<h2 id="1412">14.1.2 方法二:记忆化搜索<a class="headerlink" href="#1412" title="Permanent link">¶</a></h2>
|
||||
@@ -2949,8 +2949,8 @@ dp[i] = dp[i-1] + dp[i-2]
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int,%20mem%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E8%AE%B0%E5%BF%86%E5%8C%96%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20%E5%B7%B2%E7%9F%A5%20dp%5B1%5D%20%E5%92%8C%20dp%5B2%5D%20%EF%BC%8C%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20%E8%8B%A5%E5%AD%98%E5%9C%A8%E8%AE%B0%E5%BD%95%20dp%5Bi%5D%20%EF%BC%8C%E5%88%99%E7%9B%B4%E6%8E%A5%E8%BF%94%E5%9B%9E%E4%B9%8B%0A%20%20%20%20if%20mem%5Bi%5D%20!%3D%20-1%3A%0A%20%20%20%20%20%20%20%20return%20mem%5Bi%5D%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201,%20mem%29%20%2B%20dfs%28i%20-%202,%20mem%29%0A%20%20%20%20%23%20%E8%AE%B0%E5%BD%95%20dp%5Bi%5D%0A%20%20%20%20mem%5Bi%5D%20%3D%20count%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs_mem%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E8%AE%B0%E5%BF%86%E5%8C%96%E6%90%9C%E7%B4%A2%22%22%22%0A%20%20%20%20%23%20mem%5Bi%5D%20%E8%AE%B0%E5%BD%95%E7%88%AC%E5%88%B0%E7%AC%AC%20i%20%E9%98%B6%E7%9A%84%E6%96%B9%E6%A1%88%E6%80%BB%E6%95%B0%EF%BC%8C-1%20%E4%BB%A3%E8%A1%A8%E6%97%A0%E8%AE%B0%E5%BD%95%0A%20%20%20%20mem%20%3D%20%5B-1%5D%20*%20%28n%20%2B%201%29%0A%20%20%20%20return%20dfs%28n,%20mem%29%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs_mem%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=5&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" rel="noopener noreferrer" target="_blank">全屏观看 ></a></div></p>
|
||||
</details>
|
||||
<p>观察图 14-4 ,<strong>经过记忆化处理后,所有重叠子问题都只需计算一次,时间复杂度优化至 <span class="arithmatex">\(O(n)\)</span></strong> ,这是一个巨大的飞跃。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.png" data-dark-src="/assets/figure-svg/figures/60268cd998bef5cf2599a1cb.json" data-src="/assets/figure-svg/figures/687e3e9052cf30afdb28f760.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">记忆化搜索对应递归树</object><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">记忆化搜索对应递归树</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-4 记忆化搜索对应递归树 </p>
|
||||
<p><ha-svg-figure aria-label="记忆化搜索复用已算出的状态" class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.png" data-dark-src="/assets/figure-svg/figures/60268cd998bef5cf2599a1cb.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/687e3e9052cf30afdb28f760.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">记忆化搜索对应递归树</object><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">记忆化搜索对应递归树</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-4 记忆化搜索对应递归树 </p><div class="ha-sr-only figure-description"><h3>图解 14-4:记忆化搜索复用已算出的状态</h3><p>沿左侧主链首次计算并缓存各子问题,右侧再次遇到相同状态时直接读取缓存返回并参与父状态求和。</p><p>仍求解每步上 1 阶或 2 阶的爬楼梯方案数,基本情况为 dp[1] = 1、dp[2] = 2。记忆化搜索引入数组 mem 记录已算出的非基本状态值,基本情况先直接返回,非基本状态先查记录,仅在未记录时才继续向下展开。</p><p>画面中的首次计算主链为 dp[9] → dp[8] → dp[7] → … → dp[3],中间 dp[6] 至 dp[4] 用省略号代替;dp[3] 再分为基本情况 dp[2] 和 dp[1]。右侧明确画出带剪刀的缓存复用 dp[7]、dp[6]、dp[5],其余复用位置用省略号概括。按代码补全省略段,还会在 dp[6] 中复用 dp[4]、在 dp[5] 中复用 dp[3];dp[4] 的右子问题 dp[2] 是直接返回的基本情况,不属于缓存命中。</p><p>例如求 dp[9] 时先完成左子问题 dp[8],此过程中已经计算并保存 dp[7];随后求右子问题(带剪刀的 dp[7]),直接读缓存而不再重复展开,它仍参与 dp[9] 的加法。</p><p>下方两表分别列出图中三处明确标出的复用关系,以及代码对基本情况、首次计算、缓存命中的不同处理。</p><table><caption>图中明确标出的缓存复用</caption><thead><tr><th scope="col">当前父状态</th><th scope="col">先完成的左子问题</th><th scope="col">随后读取记录的右子问题</th></tr></thead><tbody><tr><th scope="row">dp[9]</th><td>dp[8]</td><td>dp[7]</td></tr><tr><th scope="row">dp[8]</th><td>dp[7]</td><td>dp[6]</td></tr><tr><th scope="row">dp[7]</th><td>dp[6](图中以省略号代替)</td><td>dp[5]</td></tr></tbody></table><table><caption>三种状态的处理</caption><thead><tr><th scope="col">遇到的状态</th><th scope="col">处理</th></tr></thead><tbody><tr><th scope="row">dp[1]、dp[2]</th><td>直接返回 1、2</td></tr><tr><th scope="row">非基本状态,尚无记录</th><td>递归计算、相加、写入 mem 后返回</td></tr><tr><th scope="row">非基本状态,已有记录</th><td>直接返回 mem 中的值,贡献仍参与加法</td></tr></tbody></table><p>每个非基本状态只展开计算一次,重复请求直接返回缓存值,时间复杂度因此降为 O(n)。</p></div>
|
||||
<h2 id="1413">14.1.3 方法三:动态规划<a class="headerlink" href="#1413" title="Permanent link">¶</a></h2>
|
||||
<p><strong>记忆化搜索是一种“从顶至底”的方法</strong>:我们从原问题(根节点)开始,递归地将较大子问题分解为较小子问题,直至解已知的最小子问题(叶节点)。之后,通过回溯逐层收集子问题的解,构建出原问题的解。</p>
|
||||
<p>与之相反,<strong>动态规划是一种“从底至顶”的方法</strong>:从最小子问题的解开始,迭代地构建更大子问题的解,直至得到原问题的解。</p>
|
||||
@@ -3196,8 +3196,8 @@ dp[i] = dp[i-1] + dp[i-2]
|
||||
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20climbing_stairs_dp%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E7%88%AC%E6%A5%BC%E6%A2%AF%EF%BC%9A%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%22%22%22%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20n%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%20dp%20%E8%A1%A8%EF%BC%8C%E7%94%A8%E4%BA%8E%E5%AD%98%E5%82%A8%E5%AD%90%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%0A%20%20%20%20dp%20%3D%20%5B0%5D%20*%20%28n%20%2B%201%29%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E7%8A%B6%E6%80%81%EF%BC%9A%E9%A2%84%E8%AE%BE%E6%9C%80%E5%B0%8F%E5%AD%90%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%0A%20%20%20%20dp%5B1%5D,%20dp%5B2%5D%20%3D%201,%202%0A%20%20%20%20%23%20%E7%8A%B6%E6%80%81%E8%BD%AC%E7%A7%BB%EF%BC%9A%E4%BB%8E%E8%BE%83%E5%B0%8F%E5%AD%90%E9%97%AE%E9%A2%98%E9%80%90%E6%AD%A5%E6%B1%82%E8%A7%A3%E8%BE%83%E5%A4%A7%E5%AD%90%E9%97%AE%E9%A2%98%0A%20%20%20%20for%20i%20in%20range%283,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20dp%5Bi%5D%20%3D%20dp%5Bi%20-%201%5D%20%2B%20dp%5Bi%20-%202%5D%0A%20%20%20%20return%20dp%5Bn%5D%0A%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dp%28n%29%0A%20%20%20%20print%28f%22%E7%88%AC%20%7Bn%7D%20%E9%98%B6%E6%A5%BC%E6%A2%AF%E5%85%B1%E6%9C%89%20%7Bres%7D%20%E7%A7%8D%E6%96%B9%E6%A1%88%22%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" rel="noopener noreferrer" target="_blank">全屏观看 ></a></div></p>
|
||||
</details>
|
||||
<p>图 14-5 模拟了以上代码的执行过程。</p>
|
||||
<p><ha-svg-figure class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.png" data-dark-src="/assets/figure-svg/figures/0242c587def8e2a9443db2e1.json" data-src="/assets/figure-svg/figures/b3bc6e8c43243b26ff9b1b2e.json" style=";--figure-ratio:960.0/540.0"><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.svg" data-theme="light" tabindex="-1" type="image/svg+xml">爬楼梯的动态规划过程</object><object data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">爬楼梯的动态规划过程</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-5 爬楼梯的动态规划过程 </p>
|
||||
<p><ha-svg-figure aria-label="从初始状态迭代计算爬楼梯方案数" class="animation-figure" data-book-key="chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.png" data-dark-src="/assets/figure-svg/figures/0242c587def8e2a9443db2e1.json" data-figure-accessible="" data-src="/assets/figure-svg/figures/b3bc6e8c43243b26ff9b1b2e.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">爬楼梯的动态规划过程</object><object aria-hidden="true" data="/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.dark.svg" data-theme="dark" inert="" tabindex="-1" type="image/svg+xml">爬楼梯的动态规划过程</object></ha-svg-figure></p>
|
||||
<p align="center"> 图 14-5 爬楼梯的动态规划过程 </p><div class="ha-sr-only figure-description"><h3>图解 14-5:从初始状态迭代计算爬楼梯方案数</h3><p>从初始状态 dp[1] = 1、dp[2] = 2 出发,每次将相邻前两项相加,自左向右迭代推算至 dp[9]。</p><p>爬楼梯每步可上 1 阶或 2 阶,dp[i] 表示爬到第 i 阶的方案数。本图展示自底向上的动态规划填表过程,无需借助递归调用栈。</p><p>先设置 dp[1] = 1、dp[2] = 2,再按 dp[3] 到 dp[9] 顺序依次计算 dp[i] = dp[i − 1] + dp[i − 2]:因为每项只依赖前两项,而计算到该项时它们都已先算出。</p><p>图中九个方块横向排列,只写状态名 dp[1] 至 dp[9];下方另标初值 dp[1] = 1、dp[2] = 2 和状态转移公式。数值 3、5、8、13、21、34 以及 dp[9] 的 55 均由这些初值和公式推算,并非方块中的标值。下表对应列出初值、依赖与推算结果。</p><table><caption>依赖关系与代入公式推算的值</caption><thead><tr><th scope="col">状态</th><th scope="col">来源</th><th scope="col">值及依据</th></tr></thead><tbody><tr><th scope="row">dp[1]</th><td>初始状态</td><td>1(图中给定初值)</td></tr><tr><th scope="row">dp[2]</th><td>初始状态</td><td>2(图中给定初值)</td></tr><tr><th scope="row">dp[3]</th><td>dp[2] + dp[1]</td><td>3(由公式推算)</td></tr><tr><th scope="row">dp[4]</th><td>dp[3] + dp[2]</td><td>5(由公式推算)</td></tr><tr><th scope="row">dp[5]</th><td>dp[4] + dp[3]</td><td>8(由公式推算)</td></tr><tr><th scope="row">dp[6]</th><td>dp[5] + dp[4]</td><td>13(由公式推算)</td></tr><tr><th scope="row">dp[7]</th><td>dp[6] + dp[5]</td><td>21(由公式推算)</td></tr><tr><th scope="row">dp[8]</th><td>dp[7] + dp[6]</td><td>34(由公式推算)</td></tr><tr><th scope="row">dp[9]</th><td>dp[8] + dp[7]</td><td>55(由公式推算)</td></tr></tbody></table><p>自底向上迭代时每个状态只计算一次,到达目标状态即终止;按图中初始值与转移公式推算,目标 dp[9] 对应的方案数为 55。</p></div>
|
||||
<p>与回溯算法一样,动态规划也使用“状态”概念来表示问题求解的特定阶段,每个状态都对应一个子问题以及相应的局部最优解。例如,爬楼梯问题的状态定义为当前所在楼梯阶数 <span class="arithmatex">\(i\)</span> 。</p>
|
||||
<p>根据以上内容,我们可以总结出动态规划的常用术语。</p>
|
||||
<ul>
|
||||
@@ -3527,5 +3527,5 @@ dp[i] = dp[i-1] + dp[i-2]
|
||||
<script src="../../javascripts/mathjax.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
|
||||
<script src="../../javascripts/animation_player.js"></script>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/b789dc8b01e43e756fc89964.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user