This commit is contained in:
krahets 2023-07-09 03:02:15 +08:00
parent 07d249a4de
commit 59261a2e5a
106 changed files with 3212 additions and 288 deletions

View file

@ -1949,8 +1949,8 @@
<li class="md-nav__item">
<a href="/chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="/chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1960,8 +1960,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1960,8 +1960,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2024,8 +2024,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2024,8 +2024,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2038,8 +2038,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>
@ -2426,9 +2426,36 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">subset_sum_i_naive.swift</span><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">backtrack</span><span class="p">}</span>
<a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a>
<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">subsetSumINaive</span><span class="p">}</span>
<div class="highlight"><span class="filename">subset_sum_i_naive.swift</span><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1" href="#__codelineno-8-1"></a><span class="cm">/* 回溯算法:子集和 I */</span>
<a id="__codelineno-8-2" name="__codelineno-8-2" href="#__codelineno-8-2"></a><span class="kd">func</span> <span class="nf">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">target</span><span class="p">:</span> <span class="nb">Int</span><span class="p">,</span> <span class="n">total</span><span class="p">:</span> <span class="nb">Int</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">res</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]])</span> <span class="p">{</span>
<a id="__codelineno-8-3" name="__codelineno-8-3" href="#__codelineno-8-3"></a> <span class="c1">// 子集和等于 target 时,记录解</span>
<a id="__codelineno-8-4" name="__codelineno-8-4" href="#__codelineno-8-4"></a> <span class="k">if</span> <span class="n">total</span> <span class="p">==</span> <span class="n">target</span> <span class="p">{</span>
<a id="__codelineno-8-5" name="__codelineno-8-5" href="#__codelineno-8-5"></a> <span class="n">res</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<a id="__codelineno-8-6" name="__codelineno-8-6" href="#__codelineno-8-6"></a> <span class="k">return</span>
<a id="__codelineno-8-7" name="__codelineno-8-7" href="#__codelineno-8-7"></a> <span class="p">}</span>
<a id="__codelineno-8-8" name="__codelineno-8-8" href="#__codelineno-8-8"></a> <span class="c1">// 遍历所有选择</span>
<a id="__codelineno-8-9" name="__codelineno-8-9" href="#__codelineno-8-9"></a> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="bp">stride</span><span class="p">(</span><span class="n">from</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">choices</span><span class="p">.</span><span class="bp">count</span><span class="p">,</span> <span class="n">by</span><span class="p">:</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<a id="__codelineno-8-10" name="__codelineno-8-10" href="#__codelineno-8-10"></a> <span class="c1">// 剪枝:若子集和超过 target ,则跳过该选择</span>
<a id="__codelineno-8-11" name="__codelineno-8-11" href="#__codelineno-8-11"></a> <span class="k">if</span> <span class="n">total</span> <span class="o">+</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">target</span> <span class="p">{</span>
<a id="__codelineno-8-12" name="__codelineno-8-12" href="#__codelineno-8-12"></a> <span class="k">continue</span>
<a id="__codelineno-8-13" name="__codelineno-8-13" href="#__codelineno-8-13"></a> <span class="p">}</span>
<a id="__codelineno-8-14" name="__codelineno-8-14" href="#__codelineno-8-14"></a> <span class="c1">// 尝试:做出选择,更新元素和 total</span>
<a id="__codelineno-8-15" name="__codelineno-8-15" href="#__codelineno-8-15"></a> <span class="n">state</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<a id="__codelineno-8-16" name="__codelineno-8-16" href="#__codelineno-8-16"></a> <span class="c1">// 进行下一轮选择</span>
<a id="__codelineno-8-17" name="__codelineno-8-17" href="#__codelineno-8-17"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="n">target</span><span class="p">,</span> <span class="n">total</span><span class="p">:</span> <span class="n">total</span> <span class="o">+</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">choices</span><span class="p">:</span> <span class="n">choices</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-8-18" name="__codelineno-8-18" href="#__codelineno-8-18"></a> <span class="c1">// 回退:撤销选择,恢复到之前的状态</span>
<a id="__codelineno-8-19" name="__codelineno-8-19" href="#__codelineno-8-19"></a> <span class="n">state</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span>
<a id="__codelineno-8-20" name="__codelineno-8-20" href="#__codelineno-8-20"></a> <span class="p">}</span>
<a id="__codelineno-8-21" name="__codelineno-8-21" href="#__codelineno-8-21"></a><span class="p">}</span>
<a id="__codelineno-8-22" name="__codelineno-8-22" href="#__codelineno-8-22"></a>
<a id="__codelineno-8-23" name="__codelineno-8-23" href="#__codelineno-8-23"></a><span class="cm">/* 求解子集和 I包含重复子集 */</span>
<a id="__codelineno-8-24" name="__codelineno-8-24" href="#__codelineno-8-24"></a><span class="kd">func</span> <span class="nf">subsetSumINaive</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">target</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">{</span>
<a id="__codelineno-8-25" name="__codelineno-8-25" href="#__codelineno-8-25"></a> <span class="kd">var</span> <span class="nv">state</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span> <span class="c1">// 状态(子集)</span>
<a id="__codelineno-8-26" name="__codelineno-8-26" href="#__codelineno-8-26"></a> <span class="kd">let</span> <span class="nv">total</span> <span class="p">=</span> <span class="mi">0</span> <span class="c1">// 子集和</span>
<a id="__codelineno-8-27" name="__codelineno-8-27" href="#__codelineno-8-27"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">=</span> <span class="p">[]</span> <span class="c1">// 结果列表(子集列表)</span>
<a id="__codelineno-8-28" name="__codelineno-8-28" href="#__codelineno-8-28"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="n">target</span><span class="p">,</span> <span class="n">total</span><span class="p">:</span> <span class="n">total</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="n">nums</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-8-29" name="__codelineno-8-29" href="#__codelineno-8-29"></a> <span class="k">return</span> <span class="n">res</span>
<a id="__codelineno-8-30" name="__codelineno-8-30" href="#__codelineno-8-30"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
@ -2666,9 +2693,39 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">subset_sum_i.swift</span><pre><span></span><code><a id="__codelineno-19-1" name="__codelineno-19-1" href="#__codelineno-19-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">backtrack</span><span class="p">}</span>
<a id="__codelineno-19-2" name="__codelineno-19-2" href="#__codelineno-19-2"></a>
<a id="__codelineno-19-3" name="__codelineno-19-3" href="#__codelineno-19-3"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">subsetSumI</span><span class="p">}</span>
<div class="highlight"><span class="filename">subset_sum_i.swift</span><pre><span></span><code><a id="__codelineno-19-1" name="__codelineno-19-1" href="#__codelineno-19-1"></a><span class="cm">/* 回溯算法:子集和 I */</span>
<a id="__codelineno-19-2" name="__codelineno-19-2" href="#__codelineno-19-2"></a><span class="kd">func</span> <span class="nf">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">target</span><span class="p">:</span> <span class="nb">Int</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">start</span><span class="p">:</span> <span class="nb">Int</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]])</span> <span class="p">{</span>
<a id="__codelineno-19-3" name="__codelineno-19-3" href="#__codelineno-19-3"></a> <span class="c1">// 子集和等于 target 时,记录解</span>
<a id="__codelineno-19-4" name="__codelineno-19-4" href="#__codelineno-19-4"></a> <span class="k">if</span> <span class="n">target</span> <span class="p">==</span> <span class="mi">0</span> <span class="p">{</span>
<a id="__codelineno-19-5" name="__codelineno-19-5" href="#__codelineno-19-5"></a> <span class="n">res</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<a id="__codelineno-19-6" name="__codelineno-19-6" href="#__codelineno-19-6"></a> <span class="k">return</span>
<a id="__codelineno-19-7" name="__codelineno-19-7" href="#__codelineno-19-7"></a> <span class="p">}</span>
<a id="__codelineno-19-8" name="__codelineno-19-8" href="#__codelineno-19-8"></a> <span class="c1">// 遍历所有选择</span>
<a id="__codelineno-19-9" name="__codelineno-19-9" href="#__codelineno-19-9"></a> <span class="c1">// 剪枝二:从 start 开始遍历,避免生成重复子集</span>
<a id="__codelineno-19-10" name="__codelineno-19-10" href="#__codelineno-19-10"></a> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="bp">stride</span><span class="p">(</span><span class="n">from</span><span class="p">:</span> <span class="n">start</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">choices</span><span class="p">.</span><span class="bp">count</span><span class="p">,</span> <span class="n">by</span><span class="p">:</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<a id="__codelineno-19-11" name="__codelineno-19-11" href="#__codelineno-19-11"></a> <span class="c1">// 剪枝一:若子集和超过 target ,则直接结束循环</span>
<a id="__codelineno-19-12" name="__codelineno-19-12" href="#__codelineno-19-12"></a> <span class="c1">// 这是因为数组已排序,后边元素更大,子集和一定超过 target</span>
<a id="__codelineno-19-13" name="__codelineno-19-13" href="#__codelineno-19-13"></a> <span class="k">if</span> <span class="n">target</span> <span class="o">-</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="p">{</span>
<a id="__codelineno-19-14" name="__codelineno-19-14" href="#__codelineno-19-14"></a> <span class="k">break</span>
<a id="__codelineno-19-15" name="__codelineno-19-15" href="#__codelineno-19-15"></a> <span class="p">}</span>
<a id="__codelineno-19-16" name="__codelineno-19-16" href="#__codelineno-19-16"></a> <span class="c1">// 尝试:做出选择,更新 target, start</span>
<a id="__codelineno-19-17" name="__codelineno-19-17" href="#__codelineno-19-17"></a> <span class="n">state</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<a id="__codelineno-19-18" name="__codelineno-19-18" href="#__codelineno-19-18"></a> <span class="c1">// 进行下一轮选择</span>
<a id="__codelineno-19-19" name="__codelineno-19-19" href="#__codelineno-19-19"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="n">target</span> <span class="o">-</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">choices</span><span class="p">:</span> <span class="n">choices</span><span class="p">,</span> <span class="n">start</span><span class="p">:</span> <span class="n">i</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-19-20" name="__codelineno-19-20" href="#__codelineno-19-20"></a> <span class="c1">// 回退:撤销选择,恢复到之前的状态</span>
<a id="__codelineno-19-21" name="__codelineno-19-21" href="#__codelineno-19-21"></a> <span class="n">state</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span>
<a id="__codelineno-19-22" name="__codelineno-19-22" href="#__codelineno-19-22"></a> <span class="p">}</span>
<a id="__codelineno-19-23" name="__codelineno-19-23" href="#__codelineno-19-23"></a><span class="p">}</span>
<a id="__codelineno-19-24" name="__codelineno-19-24" href="#__codelineno-19-24"></a>
<a id="__codelineno-19-25" name="__codelineno-19-25" href="#__codelineno-19-25"></a><span class="cm">/* 求解子集和 I */</span>
<a id="__codelineno-19-26" name="__codelineno-19-26" href="#__codelineno-19-26"></a><span class="kd">func</span> <span class="nf">subsetSumI</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">target</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">{</span>
<a id="__codelineno-19-27" name="__codelineno-19-27" href="#__codelineno-19-27"></a> <span class="kd">var</span> <span class="nv">state</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span> <span class="c1">// 状态(子集)</span>
<a id="__codelineno-19-28" name="__codelineno-19-28" href="#__codelineno-19-28"></a> <span class="kd">let</span> <span class="nv">nums</span> <span class="p">=</span> <span class="n">nums</span><span class="p">.</span><span class="bp">sorted</span><span class="p">()</span> <span class="c1">// 对 nums 进行排序</span>
<a id="__codelineno-19-29" name="__codelineno-19-29" href="#__codelineno-19-29"></a> <span class="kd">let</span> <span class="nv">start</span> <span class="p">=</span> <span class="mi">0</span> <span class="c1">// 遍历起始点</span>
<a id="__codelineno-19-30" name="__codelineno-19-30" href="#__codelineno-19-30"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">=</span> <span class="p">[]</span> <span class="c1">// 结果列表(子集列表)</span>
<a id="__codelineno-19-31" name="__codelineno-19-31" href="#__codelineno-19-31"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="n">target</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="n">nums</span><span class="p">,</span> <span class="n">start</span><span class="p">:</span> <span class="n">start</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-19-32" name="__codelineno-19-32" href="#__codelineno-19-32"></a> <span class="k">return</span> <span class="n">res</span>
<a id="__codelineno-19-33" name="__codelineno-19-33" href="#__codelineno-19-33"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
@ -2923,9 +2980,44 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">subset_sum_ii.swift</span><pre><span></span><code><a id="__codelineno-30-1" name="__codelineno-30-1" href="#__codelineno-30-1"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">backtrack</span><span class="p">}</span>
<a id="__codelineno-30-2" name="__codelineno-30-2" href="#__codelineno-30-2"></a>
<a id="__codelineno-30-3" name="__codelineno-30-3" href="#__codelineno-30-3"></a><span class="p">[</span><span class="kd">class</span><span class="p">]{}</span><span class="o">-</span><span class="p">[</span><span class="kd">func</span><span class="p">]{</span><span class="n">subsetSumII</span><span class="p">}</span>
<div class="highlight"><span class="filename">subset_sum_ii.swift</span><pre><span></span><code><a id="__codelineno-30-1" name="__codelineno-30-1" href="#__codelineno-30-1"></a><span class="cm">/* 回溯算法:子集和 II */</span>
<a id="__codelineno-30-2" name="__codelineno-30-2" href="#__codelineno-30-2"></a><span class="kd">func</span> <span class="nf">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">target</span><span class="p">:</span> <span class="nb">Int</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">start</span><span class="p">:</span> <span class="nb">Int</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="kr">inout</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]])</span> <span class="p">{</span>
<a id="__codelineno-30-3" name="__codelineno-30-3" href="#__codelineno-30-3"></a> <span class="c1">// 子集和等于 target 时,记录解</span>
<a id="__codelineno-30-4" name="__codelineno-30-4" href="#__codelineno-30-4"></a> <span class="k">if</span> <span class="n">target</span> <span class="p">==</span> <span class="mi">0</span> <span class="p">{</span>
<a id="__codelineno-30-5" name="__codelineno-30-5" href="#__codelineno-30-5"></a> <span class="n">res</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>
<a id="__codelineno-30-6" name="__codelineno-30-6" href="#__codelineno-30-6"></a> <span class="k">return</span>
<a id="__codelineno-30-7" name="__codelineno-30-7" href="#__codelineno-30-7"></a> <span class="p">}</span>
<a id="__codelineno-30-8" name="__codelineno-30-8" href="#__codelineno-30-8"></a> <span class="c1">// 遍历所有选择</span>
<a id="__codelineno-30-9" name="__codelineno-30-9" href="#__codelineno-30-9"></a> <span class="c1">// 剪枝二:从 start 开始遍历,避免生成重复子集</span>
<a id="__codelineno-30-10" name="__codelineno-30-10" href="#__codelineno-30-10"></a> <span class="c1">// 剪枝三:从 start 开始遍历,避免重复选择同一元素</span>
<a id="__codelineno-30-11" name="__codelineno-30-11" href="#__codelineno-30-11"></a> <span class="k">for</span> <span class="n">i</span> <span class="k">in</span> <span class="bp">stride</span><span class="p">(</span><span class="n">from</span><span class="p">:</span> <span class="n">start</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">choices</span><span class="p">.</span><span class="bp">count</span><span class="p">,</span> <span class="n">by</span><span class="p">:</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<a id="__codelineno-30-12" name="__codelineno-30-12" href="#__codelineno-30-12"></a> <span class="c1">// 剪枝一:若子集和超过 target ,则直接结束循环</span>
<a id="__codelineno-30-13" name="__codelineno-30-13" href="#__codelineno-30-13"></a> <span class="c1">// 这是因为数组已排序,后边元素更大,子集和一定超过 target</span>
<a id="__codelineno-30-14" name="__codelineno-30-14" href="#__codelineno-30-14"></a> <span class="k">if</span> <span class="n">target</span> <span class="o">-</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="p">{</span>
<a id="__codelineno-30-15" name="__codelineno-30-15" href="#__codelineno-30-15"></a> <span class="k">break</span>
<a id="__codelineno-30-16" name="__codelineno-30-16" href="#__codelineno-30-16"></a> <span class="p">}</span>
<a id="__codelineno-30-17" name="__codelineno-30-17" href="#__codelineno-30-17"></a> <span class="c1">// 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过</span>
<a id="__codelineno-30-18" name="__codelineno-30-18" href="#__codelineno-30-18"></a> <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="n">start</span><span class="p">,</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">==</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="p">{</span>
<a id="__codelineno-30-19" name="__codelineno-30-19" href="#__codelineno-30-19"></a> <span class="k">continue</span>
<a id="__codelineno-30-20" name="__codelineno-30-20" href="#__codelineno-30-20"></a> <span class="p">}</span>
<a id="__codelineno-30-21" name="__codelineno-30-21" href="#__codelineno-30-21"></a> <span class="c1">// 尝试:做出选择,更新 target, start</span>
<a id="__codelineno-30-22" name="__codelineno-30-22" href="#__codelineno-30-22"></a> <span class="n">state</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<a id="__codelineno-30-23" name="__codelineno-30-23" href="#__codelineno-30-23"></a> <span class="c1">// 进行下一轮选择</span>
<a id="__codelineno-30-24" name="__codelineno-30-24" href="#__codelineno-30-24"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="n">target</span> <span class="o">-</span> <span class="n">choices</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">choices</span><span class="p">:</span> <span class="n">choices</span><span class="p">,</span> <span class="n">start</span><span class="p">:</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-30-25" name="__codelineno-30-25" href="#__codelineno-30-25"></a> <span class="c1">// 回退:撤销选择,恢复到之前的状态</span>
<a id="__codelineno-30-26" name="__codelineno-30-26" href="#__codelineno-30-26"></a> <span class="n">state</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span>
<a id="__codelineno-30-27" name="__codelineno-30-27" href="#__codelineno-30-27"></a> <span class="p">}</span>
<a id="__codelineno-30-28" name="__codelineno-30-28" href="#__codelineno-30-28"></a><span class="p">}</span>
<a id="__codelineno-30-29" name="__codelineno-30-29" href="#__codelineno-30-29"></a>
<a id="__codelineno-30-30" name="__codelineno-30-30" href="#__codelineno-30-30"></a><span class="cm">/* 求解子集和 II */</span>
<a id="__codelineno-30-31" name="__codelineno-30-31" href="#__codelineno-30-31"></a><span class="kd">func</span> <span class="nf">subsetSumII</span><span class="p">(</span><span class="n">nums</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">],</span> <span class="n">target</span><span class="p">:</span> <span class="nb">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">{</span>
<a id="__codelineno-30-32" name="__codelineno-30-32" href="#__codelineno-30-32"></a> <span class="kd">var</span> <span class="nv">state</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">]</span> <span class="p">=</span> <span class="p">[]</span> <span class="c1">// 状态(子集)</span>
<a id="__codelineno-30-33" name="__codelineno-30-33" href="#__codelineno-30-33"></a> <span class="kd">let</span> <span class="nv">nums</span> <span class="p">=</span> <span class="n">nums</span><span class="p">.</span><span class="bp">sorted</span><span class="p">()</span> <span class="c1">// 对 nums 进行排序</span>
<a id="__codelineno-30-34" name="__codelineno-30-34" href="#__codelineno-30-34"></a> <span class="kd">let</span> <span class="nv">start</span> <span class="p">=</span> <span class="mi">0</span> <span class="c1">// 遍历起始点</span>
<a id="__codelineno-30-35" name="__codelineno-30-35" href="#__codelineno-30-35"></a> <span class="kd">var</span> <span class="nv">res</span><span class="p">:</span> <span class="p">[[</span><span class="nb">Int</span><span class="p">]]</span> <span class="p">=</span> <span class="p">[]</span> <span class="c1">// 结果列表(子集列表)</span>
<a id="__codelineno-30-36" name="__codelineno-30-36" href="#__codelineno-30-36"></a> <span class="n">backtrack</span><span class="p">(</span><span class="n">state</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">state</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="n">target</span><span class="p">,</span> <span class="n">choices</span><span class="p">:</span> <span class="n">nums</span><span class="p">,</span> <span class="n">start</span><span class="p">:</span> <span class="n">start</span><span class="p">,</span> <span class="n">res</span><span class="p">:</span> <span class="p">&amp;</span><span class="n">res</span><span class="p">)</span>
<a id="__codelineno-30-37" name="__codelineno-30-37" href="#__codelineno-30-37"></a> <span class="k">return</span> <span class="n">res</span>
<a id="__codelineno-30-38" name="__codelineno-30-38" href="#__codelineno-30-38"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">

View file

@ -1972,8 +1972,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2037,8 +2037,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2065,8 +2065,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2113,8 +2113,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1972,8 +1972,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2031,8 +2031,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -18,7 +18,7 @@
<link rel="prev" href="../intro_to_dynamic_programming/">
<link rel="next" href="../knapsack_problem/">
<link rel="next" href="../dp_solution_pipeline/">
<link rel="icon" href="../../assets/images/favicon.png">
<meta name="generator" content="mkdocs-1.4.2, mkdocs-material-9.1.11">
@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>
@ -2244,7 +2244,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cost</span><span class="p">.</span><span class="na">length</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
<a id="__codelineno-0-5" name="__codelineno-0-5" href="#__codelineno-0-5"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">cost</span><span class="o">[</span><span class="n">n</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-0-6" name="__codelineno-0-6" href="#__codelineno-0-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-0-7" name="__codelineno-0-7" href="#__codelineno-0-7"></a><span class="w"> </span><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">dp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="o">[</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-0-8" name="__codelineno-0-8" href="#__codelineno-0-8"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-0-9" name="__codelineno-0-9" href="#__codelineno-0-9"></a><span class="w"> </span><span class="n">dp</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cost</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="p">;</span>
@ -2263,7 +2263,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]
<a id="__codelineno-1-3" name="__codelineno-1-3" href="#__codelineno-1-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cost</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<a id="__codelineno-1-4" name="__codelineno-1-4" href="#__codelineno-1-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
<a id="__codelineno-1-5" name="__codelineno-1-5" href="#__codelineno-1-5"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">cost</span><span class="p">[</span><span class="n">n</span><span class="p">];</span>
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-1-6" name="__codelineno-1-6" href="#__codelineno-1-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-1-7" name="__codelineno-1-7" href="#__codelineno-1-7"></a><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">dp</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<a id="__codelineno-1-8" name="__codelineno-1-8" href="#__codelineno-1-8"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-1-9" name="__codelineno-1-9" href="#__codelineno-1-9"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cost</span><span class="p">[</span><span class="mi">1</span><span class="p">];</span>
@ -2282,7 +2282,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]
<a id="__codelineno-2-3" name="__codelineno-2-3" href="#__codelineno-2-3"></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">cost</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<a id="__codelineno-2-4" name="__codelineno-2-4" href="#__codelineno-2-4"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a id="__codelineno-2-5" name="__codelineno-2-5" href="#__codelineno-2-5"></a> <span class="k">return</span> <span class="n">cost</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="c1"># 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-2-6" name="__codelineno-2-6" href="#__codelineno-2-6"></a> <span class="c1"># 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-2-7" name="__codelineno-2-7" href="#__codelineno-2-7"></a> <span class="n">dp</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<a id="__codelineno-2-8" name="__codelineno-2-8" href="#__codelineno-2-8"></a> <span class="c1"># 初始状态:预设最小子问题的解</span>
<a id="__codelineno-2-9" name="__codelineno-2-9" href="#__codelineno-2-9"></a> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">cost</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">cost</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
@ -2314,7 +2314,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]
<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cost</span><span class="p">.</span><span class="n">Length</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
<a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">2</span><span class="p">)</span>
<a id="__codelineno-7-5" name="__codelineno-7-5" href="#__codelineno-7-5"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">cost</span><span class="p">[</span><span class="n">n</span><span class="p">];</span>
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-7-7" name="__codelineno-7-7" href="#__codelineno-7-7"></a><span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">dp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">];</span>
<a id="__codelineno-7-8" name="__codelineno-7-8" href="#__codelineno-7-8"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-7-9" name="__codelineno-7-9" href="#__codelineno-7-9"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">cost</span><span class="p">[</span><span class="m">1</span><span class="p">];</span>
@ -2475,7 +2475,7 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
<a id="__codelineno-22-3" name="__codelineno-22-3" href="#__codelineno-22-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-22-4" name="__codelineno-22-4" href="#__codelineno-22-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<a id="__codelineno-22-5" name="__codelineno-22-5" href="#__codelineno-22-5"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-22-6" name="__codelineno-22-6" href="#__codelineno-22-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-22-6" name="__codelineno-22-6" href="#__codelineno-22-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-22-7" name="__codelineno-22-7" href="#__codelineno-22-7"></a><span class="w"> </span><span class="kt">int</span><span class="o">[][]</span><span class="w"> </span><span class="n">dp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="o">[</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">][</span><span class="mi">3</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-22-8" name="__codelineno-22-8" href="#__codelineno-22-8"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-22-9" name="__codelineno-22-9" href="#__codelineno-22-9"></a><span class="w"> </span><span class="n">dp</span><span class="o">[</span><span class="mi">1</span><span class="o">][</span><span class="mi">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
@ -2497,7 +2497,7 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
<a id="__codelineno-23-3" name="__codelineno-23-3" href="#__codelineno-23-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-23-4" name="__codelineno-23-4" href="#__codelineno-23-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<a id="__codelineno-23-5" name="__codelineno-23-5" href="#__codelineno-23-5"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-23-6" name="__codelineno-23-6" href="#__codelineno-23-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-23-6" name="__codelineno-23-6" href="#__codelineno-23-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-23-7" name="__codelineno-23-7" href="#__codelineno-23-7"></a><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">dp</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">));</span>
<a id="__codelineno-23-8" name="__codelineno-23-8" href="#__codelineno-23-8"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-23-9" name="__codelineno-23-9" href="#__codelineno-23-9"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
@ -2518,7 +2518,7 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
<a id="__codelineno-24-2" name="__codelineno-24-2" href="#__codelineno-24-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;带约束爬楼梯:动态规划&quot;&quot;&quot;</span>
<a id="__codelineno-24-3" name="__codelineno-24-3" href="#__codelineno-24-3"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a id="__codelineno-24-4" name="__codelineno-24-4" href="#__codelineno-24-4"></a> <span class="k">return</span> <span class="n">n</span>
<a id="__codelineno-24-5" name="__codelineno-24-5" href="#__codelineno-24-5"></a> <span class="c1"># 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-24-5" name="__codelineno-24-5" href="#__codelineno-24-5"></a> <span class="c1"># 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-24-6" name="__codelineno-24-6" href="#__codelineno-24-6"></a> <span class="n">dp</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mi">3</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
<a id="__codelineno-24-7" name="__codelineno-24-7" href="#__codelineno-24-7"></a> <span class="c1"># 初始状态:预设最小子问题的解</span>
<a id="__codelineno-24-8" name="__codelineno-24-8" href="#__codelineno-24-8"></a> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span>
@ -2552,7 +2552,7 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
<a id="__codelineno-29-3" name="__codelineno-29-3" href="#__codelineno-29-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-29-4" name="__codelineno-29-4" href="#__codelineno-29-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<a id="__codelineno-29-5" name="__codelineno-29-5" href="#__codelineno-29-5"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-29-6" name="__codelineno-29-6" href="#__codelineno-29-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-29-6" name="__codelineno-29-6" href="#__codelineno-29-6"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-29-7" name="__codelineno-29-7" href="#__codelineno-29-7"></a><span class="w"> </span><span class="kt">int</span><span class="p">[,]</span><span class="w"> </span><span class="n">dp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">];</span>
<a id="__codelineno-29-8" name="__codelineno-29-8" href="#__codelineno-29-8"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-29-9" name="__codelineno-29-9" href="#__codelineno-29-9"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
@ -2682,13 +2682,13 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
<a href="../knapsack_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 13.4. &amp;nbsp; 0-1 背包问题New" rel="next">
<a href="../dp_solution_pipeline/" class="md-footer__link md-footer__link--next" aria-label="下一页: 13.3. &amp;nbsp; DP 解题思路New" rel="next">
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
13.4. &nbsp; 0-1 背包问题New
13.3. &nbsp; DP 解题思路New
</div>
</div>
<div class="md-footer__button md-icon">

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

File diff suppressed because it is too large Load diff

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>
@ -2689,7 +2689,7 @@ dp[i] = dp[i-1] + dp[i-2]
<a id="__codelineno-33-2" name="__codelineno-33-2" href="#__codelineno-33-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsDP</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-33-3" name="__codelineno-33-3" href="#__codelineno-33-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
<a id="__codelineno-33-4" name="__codelineno-33-4" href="#__codelineno-33-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<a id="__codelineno-33-5" name="__codelineno-33-5" href="#__codelineno-33-5"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-33-5" name="__codelineno-33-5" href="#__codelineno-33-5"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-33-6" name="__codelineno-33-6" href="#__codelineno-33-6"></a><span class="w"> </span><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">dp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="o">[</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">]</span><span class="p">;</span>
<a id="__codelineno-33-7" name="__codelineno-33-7" href="#__codelineno-33-7"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-33-8" name="__codelineno-33-8" href="#__codelineno-33-8"></a><span class="w"> </span><span class="n">dp</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
@ -2707,7 +2707,7 @@ dp[i] = dp[i-1] + dp[i-2]
<a id="__codelineno-34-2" name="__codelineno-34-2" href="#__codelineno-34-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsDP</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-34-3" name="__codelineno-34-3" href="#__codelineno-34-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span>
<a id="__codelineno-34-4" name="__codelineno-34-4" href="#__codelineno-34-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<a id="__codelineno-34-5" name="__codelineno-34-5" href="#__codelineno-34-5"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-34-5" name="__codelineno-34-5" href="#__codelineno-34-5"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-34-6" name="__codelineno-34-6" href="#__codelineno-34-6"></a><span class="w"> </span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">dp</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<a id="__codelineno-34-7" name="__codelineno-34-7" href="#__codelineno-34-7"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-34-8" name="__codelineno-34-8" href="#__codelineno-34-8"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
@ -2725,7 +2725,7 @@ dp[i] = dp[i-1] + dp[i-2]
<a id="__codelineno-35-2" name="__codelineno-35-2" href="#__codelineno-35-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;爬楼梯:动态规划&quot;&quot;&quot;</span>
<a id="__codelineno-35-3" name="__codelineno-35-3" href="#__codelineno-35-3"></a> <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<a id="__codelineno-35-4" name="__codelineno-35-4" href="#__codelineno-35-4"></a> <span class="k">return</span> <span class="n">n</span>
<a id="__codelineno-35-5" name="__codelineno-35-5" href="#__codelineno-35-5"></a> <span class="c1"># 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-35-5" name="__codelineno-35-5" href="#__codelineno-35-5"></a> <span class="c1"># 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-35-6" name="__codelineno-35-6" href="#__codelineno-35-6"></a> <span class="n">dp</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<a id="__codelineno-35-7" name="__codelineno-35-7" href="#__codelineno-35-7"></a> <span class="c1"># 初始状态:预设最小子问题的解</span>
<a id="__codelineno-35-8" name="__codelineno-35-8" href="#__codelineno-35-8"></a> <span class="n">dp</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">dp</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
@ -2756,7 +2756,7 @@ dp[i] = dp[i-1] + dp[i-2]
<a id="__codelineno-40-2" name="__codelineno-40-2" href="#__codelineno-40-2"></a><span class="kt">int</span><span class="w"> </span><span class="nf">climbingStairsDP</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-40-3" name="__codelineno-40-3" href="#__codelineno-40-3"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">2</span><span class="p">)</span>
<a id="__codelineno-40-4" name="__codelineno-40-4" href="#__codelineno-40-4"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<a id="__codelineno-40-5" name="__codelineno-40-5" href="#__codelineno-40-5"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-40-5" name="__codelineno-40-5" href="#__codelineno-40-5"></a><span class="w"> </span><span class="c1">// 初始化 dp 表,用于存储子问题的解</span>
<a id="__codelineno-40-6" name="__codelineno-40-6" href="#__codelineno-40-6"></a><span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">dp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="n">n</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">];</span>
<a id="__codelineno-40-7" name="__codelineno-40-7" href="#__codelineno-40-7"></a><span class="w"> </span><span class="c1">// 初始状态:预设最小子问题的解</span>
<a id="__codelineno-40-8" name="__codelineno-40-8" href="#__codelineno-40-8"></a><span class="w"> </span><span class="n">dp</span><span class="p">[</span><span class="m">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="p">;</span>
@ -2785,14 +2785,14 @@ dp[i] = dp[i-1] + dp[i-2]
</div>
<p>与回溯算法一样,动态规划也使用“状态”概念来表示问题求解的某个特定阶段,每个状态都对应一个子问题以及相应的局部最优解。例如对于爬楼梯问题,状态定义为当前所在楼梯阶数 <span class="arithmatex">\(i\)</span><strong>动态规划的常用术语包括</strong></p>
<ul>
<li><span class="arithmatex">\(dp\)</span> 数组称为「状态列表」,<span class="arithmatex">\(dp[i]\)</span> 代表第 <span class="arithmatex">\(i\)</span> 个状态的解;</li>
<li>数组 <code>dp</code> 称为「<span class="arithmatex">\(dp\)</span> 表」,<span class="arithmatex">\(dp[i]\)</span> 表示状态 <span class="arithmatex">\(i\)</span> 对应子问题的解;</li>
<li>将最小子问题对应的状态(即第 <span class="arithmatex">\(1\)</span> , <span class="arithmatex">\(2\)</span> 阶楼梯)称为「初始状态」;</li>
<li>将递推公式 <span class="arithmatex">\(dp[i] = dp[i-1] + dp[i-2]\)</span> 称为「状态转移方程」;</li>
</ul>
<p><img alt="爬楼梯的动态规划过程" src="../intro_to_dynamic_programming.assets/climbing_stairs_dp.png" /></p>
<p align="center"> Fig. 爬楼梯的动态规划过程 </p>
<p>细心的你可能发现,<strong>由于 <span class="arithmatex">\(dp[i]\)</span> 只与 <span class="arithmatex">\(dp[i-1]\)</span><span class="arithmatex">\(dp[i-2]\)</span> 有关,因此我们无需使用一个数组 <code>dp</code> 来存储所有状态</strong>,而只需两个变量滚动前进即可。如以下代码所示,由于省去了数组 <code>dp</code> 占用的空间,因此空间复杂度从 <span class="arithmatex">\(O(n)\)</span> 降低至 <span class="arithmatex">\(O(1)\)</span></p>
<p>细心的你可能发现,<strong>由于 <span class="arithmatex">\(dp[i]\)</span> 只与 <span class="arithmatex">\(dp[i-1]\)</span><span class="arithmatex">\(dp[i-2]\)</span> 有关,因此我们无需使用一个数组 <code>dp</code> 来存储所有子问题的解</strong>,而只需两个变量滚动前进即可。如以下代码所示,由于省去了数组 <code>dp</code> 占用的空间,因此空间复杂度从 <span class="arithmatex">\(O(n)\)</span> 降低至 <span class="arithmatex">\(O(1)\)</span></p>
<div class="tabbed-set tabbed-alternate" data-tabs="5:11"><input checked="checked" id="__tabbed_5_1" name="__tabbed_5" type="radio" /><input id="__tabbed_5_2" name="__tabbed_5" type="radio" /><input id="__tabbed_5_3" name="__tabbed_5" type="radio" /><input id="__tabbed_5_4" name="__tabbed_5" type="radio" /><input id="__tabbed_5_5" name="__tabbed_5" type="radio" /><input id="__tabbed_5_6" name="__tabbed_5" type="radio" /><input id="__tabbed_5_7" name="__tabbed_5" type="radio" /><input id="__tabbed_5_8" name="__tabbed_5" type="radio" /><input id="__tabbed_5_9" name="__tabbed_5" type="radio" /><input id="__tabbed_5_10" name="__tabbed_5" type="radio" /><input id="__tabbed_5_11" name="__tabbed_5" type="radio" /><div class="tabbed-labels"><label for="__tabbed_5_1">Java</label><label for="__tabbed_5_2">C++</label><label for="__tabbed_5_3">Python</label><label for="__tabbed_5_4">Go</label><label for="__tabbed_5_5">JavaScript</label><label for="__tabbed_5_6">TypeScript</label><label for="__tabbed_5_7">C</label><label for="__tabbed_5_8">C#</label><label for="__tabbed_5_9">Swift</label><label for="__tabbed_5_10">Zig</label><label for="__tabbed_5_11">Dart</label></div>
<div class="tabbed-content">
<div class="tabbed-block">

View file

@ -15,7 +15,7 @@
<link rel="canonical" href="https://www.hello-algo.com/chapter_dynamic_programming/knapsack_problem/">
<link rel="prev" href="../dp_problem_features/">
<link rel="prev" href="../dp_solution_pipeline/">
<link rel="next" href="../../chapter_appendix/installation/">
@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>
@ -2242,12 +2242,11 @@
<p align="center"> Fig. 0-1 背包的示例数据 </p>
<p>我们可以将 0-1 背包问题看作是一个由 <span class="arithmatex">\(n\)</span> 轮决策组成的过程,每个物体都有不放入和放入两种决策,因此该问题是满足决策树模型的。此外,该问题的目标是求解“在限定背包容量下的最大价值”,因此较大概率是个动态规划问题。我们接下来尝试求解它。</p>
<p><strong>第一步:思考每一轮的决策是什么,从而得到状态定义</strong></p>
<p>在 0-1 背包问题中,不放入背包,背包容量不变;放入背包,背包容量减小。由此可得状态定义:物品编号 <span class="arithmatex">\(i\)</span> 和背包容量 <span class="arithmatex">\(c\)</span> ,记为 <span class="arithmatex">\([i, c]\)</span></p>
<p><strong>第二步:明确子问题是什么,从而得到 <span class="arithmatex">\(dp\)</span> 列表</strong></p>
<p>状态 <span class="arithmatex">\([i, c]\)</span> 对应的子问题为:<strong><span class="arithmatex">\(i\)</span> 个物品在容量为 <span class="arithmatex">\(c\)</span> 背包中的最大价值</strong>,记为 <span class="arithmatex">\(dp[i, c]\)</span></p>
<p><strong>第一步:思考每轮的决策,定义状态,从而得到 <span class="arithmatex">\(dp\)</span></strong></p>
<p>在 0-1 背包问题中,不放入背包,背包容量不变;放入背包,背包容量减小。由此可得状态定义:当前物品编号 <span class="arithmatex">\(i\)</span> 和剩余背包容量 <span class="arithmatex">\(c\)</span> ,记为 <span class="arithmatex">\([i, c]\)</span></p>
<p>状态 <span class="arithmatex">\([i, c]\)</span> 对应的子问题为:<strong><span class="arithmatex">\(i\)</span> 个物品在剩余容量为 <span class="arithmatex">\(c\)</span> 的背包中的最大价值</strong>,记为 <span class="arithmatex">\(dp[i, c]\)</span></p>
<p>至此,我们得到一个尺寸为 <span class="arithmatex">\(n \times cap\)</span> 的二维 <span class="arithmatex">\(dp\)</span> 矩阵。</p>
<p><strong>步:找出最优子结构,进而推导出状态转移方程</strong></p>
<p><strong>步:找出最优子结构,进而推导出状态转移方程</strong></p>
<p>当我们做出物品 <span class="arithmatex">\(i\)</span> 的决策后,剩余的是前 <span class="arithmatex">\(i-1\)</span> 个物品的决策。因此,状态转移分为两种情况:</p>
<ul>
<li><strong>不放入物品 <span class="arithmatex">\(i\)</span></strong> :背包容量不变,状态转移至 <span class="arithmatex">\([i-1, c]\)</span> </li>
@ -2258,6 +2257,9 @@
dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
\]</div>
<p>需要注意的是,若当前物品重量 <span class="arithmatex">\(wgt[i - 1]\)</span> 超出剩余背包容量 <span class="arithmatex">\(c\)</span> ,则只能选择不放入背包。</p>
<p><strong>第三步:确定边界条件和状态转移顺序</strong></p>
<p>当无物品或无剩余背包容量时最大价值为 <span class="arithmatex">\(0\)</span> ,即所有 <span class="arithmatex">\(dp[i, 0]\)</span><span class="arithmatex">\(dp[0, c]\)</span> 都等于 <span class="arithmatex">\(0\)</span></p>
<p>当前状态 <span class="arithmatex">\([i, c]\)</span> 从上方的状态 <span class="arithmatex">\([i-1, c]\)</span> 和左上方的状态 <span class="arithmatex">\([i-1, c-wgt[i-1]]\)</span> 转移而来,因此通过两层循环正序遍历整个 <span class="arithmatex">\(dp\)</span> 表即可。</p>
<h2 id="1341">13.4.1. &nbsp; 方法一:暴力搜索<a class="headerlink" href="#1341" title="Permanent link">&para;</a></h2>
<p>搜索代码包含以下要素:</p>
<ul>
@ -2416,7 +2418,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
<div class="highlight"><span class="filename">knapsack.py</span><pre><span></span><code><a id="__codelineno-24-1" name="__codelineno-24-1" href="#__codelineno-24-1"></a><span class="k">def</span> <span class="nf">knapsack_dp</span><span class="p">(</span><span class="n">wgt</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">cap</span><span class="p">):</span>
<a id="__codelineno-24-2" name="__codelineno-24-2" href="#__codelineno-24-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;0-1 背包:动态规划&quot;&quot;&quot;</span>
<a id="__codelineno-24-3" name="__codelineno-24-3" href="#__codelineno-24-3"></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">wgt</span><span class="p">)</span>
<a id="__codelineno-24-4" name="__codelineno-24-4" href="#__codelineno-24-4"></a> <span class="c1"># 初始化 dp </span>
<a id="__codelineno-24-4" name="__codelineno-24-4" href="#__codelineno-24-4"></a> <span class="c1"># 初始化 dp 表</span>
<a id="__codelineno-24-5" name="__codelineno-24-5" href="#__codelineno-24-5"></a> <span class="n">dp</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">cap</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
<a id="__codelineno-24-6" name="__codelineno-24-6" href="#__codelineno-24-6"></a> <span class="c1"># 状态转移</span>
<a id="__codelineno-24-7" name="__codelineno-24-7" href="#__codelineno-24-7"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
@ -2551,7 +2553,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
<div class="highlight"><span class="filename">knapsack.py</span><pre><span></span><code><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a><span class="k">def</span> <span class="nf">knapsack_dp_comp</span><span class="p">(</span><span class="n">wgt</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">cap</span><span class="p">):</span>
<a id="__codelineno-35-2" name="__codelineno-35-2" href="#__codelineno-35-2"></a><span class="w"> </span><span class="sd">&quot;&quot;&quot;0-1 背包:状态压缩后的动态规划&quot;&quot;&quot;</span>
<a id="__codelineno-35-3" name="__codelineno-35-3" href="#__codelineno-35-3"></a> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">wgt</span><span class="p">)</span>
<a id="__codelineno-35-4" name="__codelineno-35-4" href="#__codelineno-35-4"></a> <span class="c1"># 初始化 dp </span>
<a id="__codelineno-35-4" name="__codelineno-35-4" href="#__codelineno-35-4"></a> <span class="c1"># 初始化 dp 表</span>
<a id="__codelineno-35-5" name="__codelineno-35-5" href="#__codelineno-35-5"></a> <span class="n">dp</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">cap</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<a id="__codelineno-35-6" name="__codelineno-35-6" href="#__codelineno-35-6"></a> <span class="c1"># 状态转移</span>
<a id="__codelineno-35-7" name="__codelineno-35-7" href="#__codelineno-35-7"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
@ -2677,7 +2679,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../dp_problem_features/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 13.2. &amp;nbsp; DP 问题特性New" rel="prev">
<a href="../dp_solution_pipeline/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 13.3. &amp;nbsp; DP 解题思路New" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
@ -2686,7 +2688,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
上一页
</span>
<div class="md-ellipsis">
13.2. &nbsp; DP 问题特性New
13.3. &nbsp; DP 解题思路New
</div>
</div>
</a>

View file

@ -2044,8 +2044,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2050,8 +2050,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2024,8 +2024,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2030,8 +2030,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2051,8 +2051,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1972,8 +1972,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1972,8 +1972,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1960,8 +1960,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2031,8 +2031,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1972,8 +1972,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1958,8 +1958,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1972,8 +1972,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2024,8 +2024,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2031,8 +2031,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2010,8 +2010,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2037,8 +2037,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -1962,8 +1962,8 @@
<li class="md-nav__item">
<a href="../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2037,8 +2037,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2071,8 +2071,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2003,8 +2003,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2017,8 +2017,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2112,8 +2112,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2051,8 +2051,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2058,8 +2058,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

View file

@ -2050,8 +2050,8 @@
<li class="md-nav__item">
<a href="../../chapter_dynamic_programming/dp_solution_pipeline.md" class="md-nav__link">
13.3. &nbsp; DP 解题步骤New
<a href="../../chapter_dynamic_programming/dp_solution_pipeline/" class="md-nav__link">
13.3. &nbsp; DP 解题思路New
</a>
</li>

Some files were not shown because too many files have changed in this diff Show more