This commit is contained in:
krahets 2023-07-21 22:21:21 +08:00
parent f53ea2981d
commit 1188810504
8 changed files with 90 additions and 83 deletions

View file

@ -3547,9 +3547,35 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">build_tree.go</span><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="p">[</span><span class="nx">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="nx">dfs</span><span class="p">}</span>
<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a>
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="p">[</span><span class="nx">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="nx">buildTree</span><span class="p">}</span>
<div class="highlight"><span class="filename">build_tree.go</span><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="cm">/* 构建二叉树:分治 */</span>
<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="kd">func</span><span class="w"> </span><span class="nx">dfsBuildTree</span><span class="p">(</span><span class="nx">preorder</span><span class="p">,</span><span class="w"> </span><span class="nx">inorder</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="nx">hmap</span><span class="w"> </span><span class="kd">map</span><span class="p">[</span><span class="kt">int</span><span class="p">]</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">,</span><span class="w"> </span><span class="nx">l</span><span class="p">,</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="nx">TreeNode</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="w"> </span><span class="c1">// 子树区间为空时终止</span>
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">r</span><span class="o">-</span><span class="nx">l</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span>
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="w"> </span><span class="c1">// 初始化根节点</span>
<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="w"> </span><span class="nx">root</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">NewTreeNode</span><span class="p">(</span><span class="nx">preorder</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span>
<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="w"> </span><span class="c1">// 查询 m ,从而划分左右子树</span>
<a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a><span class="w"> </span><span class="nx">m</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">hmap</span><span class="p">[</span><span class="nx">preorder</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span>
<a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a><span class="w"> </span><span class="c1">// 子问题:构建左子树</span>
<a id="__codelineno-3-12" name="__codelineno-3-12" href="#__codelineno-3-12"></a><span class="w"> </span><span class="nx">root</span><span class="p">.</span><span class="nx">Left</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">dfsBuildTree</span><span class="p">(</span><span class="nx">preorder</span><span class="p">,</span><span class="w"> </span><span class="nx">inorder</span><span class="p">,</span><span class="w"> </span><span class="nx">hmap</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nx">l</span><span class="p">,</span><span class="w"> </span><span class="nx">m</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<a id="__codelineno-3-13" name="__codelineno-3-13" href="#__codelineno-3-13"></a><span class="w"> </span><span class="c1">// 子问题:构建右子树</span>
<a id="__codelineno-3-14" name="__codelineno-3-14" href="#__codelineno-3-14"></a><span class="w"> </span><span class="nx">root</span><span class="p">.</span><span class="nx">Right</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">dfsBuildTree</span><span class="p">(</span><span class="nx">preorder</span><span class="p">,</span><span class="w"> </span><span class="nx">inorder</span><span class="p">,</span><span class="w"> </span><span class="nx">hmap</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="o">+</span><span class="mi">1</span><span class="o">+</span><span class="nx">m</span><span class="o">-</span><span class="nx">l</span><span class="p">,</span><span class="w"> </span><span class="nx">m</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nx">r</span><span class="p">)</span>
<a id="__codelineno-3-15" name="__codelineno-3-15" href="#__codelineno-3-15"></a><span class="w"> </span><span class="c1">// 返回根节点</span>
<a id="__codelineno-3-16" name="__codelineno-3-16" href="#__codelineno-3-16"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">root</span>
<a id="__codelineno-3-17" name="__codelineno-3-17" href="#__codelineno-3-17"></a><span class="p">}</span>
<a id="__codelineno-3-18" name="__codelineno-3-18" href="#__codelineno-3-18"></a>
<a id="__codelineno-3-19" name="__codelineno-3-19" href="#__codelineno-3-19"></a><span class="cm">/* 构建二叉树 */</span>
<a id="__codelineno-3-20" name="__codelineno-3-20" href="#__codelineno-3-20"></a><span class="kd">func</span><span class="w"> </span><span class="nx">buildTree</span><span class="p">(</span><span class="nx">preorder</span><span class="p">,</span><span class="w"> </span><span class="nx">inorder</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="nx">TreeNode</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-21" name="__codelineno-3-21" href="#__codelineno-3-21"></a><span class="w"> </span><span class="c1">// 初始化哈希表,存储 inorder 元素到索引的映射</span>
<a id="__codelineno-3-22" name="__codelineno-3-22" href="#__codelineno-3-22"></a><span class="w"> </span><span class="nx">hmap</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">int</span><span class="p">]</span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">inorder</span><span class="p">))</span>
<a id="__codelineno-3-23" name="__codelineno-3-23" href="#__codelineno-3-23"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p">&lt;</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">inorder</span><span class="p">);</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-24" name="__codelineno-3-24" href="#__codelineno-3-24"></a><span class="w"> </span><span class="nx">hmap</span><span class="p">[</span><span class="nx">inorder</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">i</span>
<a id="__codelineno-3-25" name="__codelineno-3-25" href="#__codelineno-3-25"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-3-26" name="__codelineno-3-26" href="#__codelineno-3-26"></a>
<a id="__codelineno-3-27" name="__codelineno-3-27" href="#__codelineno-3-27"></a><span class="w"> </span><span class="nx">root</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">dfsBuildTree</span><span class="p">(</span><span class="nx">preorder</span><span class="p">,</span><span class="w"> </span><span class="nx">inorder</span><span class="p">,</span><span class="w"> </span><span class="nx">hmap</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">inorder</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<a id="__codelineno-3-28" name="__codelineno-3-28" href="#__codelineno-3-28"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">root</span>
<a id="__codelineno-3-29" name="__codelineno-3-29" href="#__codelineno-3-29"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">

View file

@ -3548,11 +3548,37 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">hanota.go</span><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="p">[</span><span class="nx">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="nx">move</span><span class="p">}</span>
<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a>
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="p">[</span><span class="nx">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="nx">dfs</span><span class="p">}</span>
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a>
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="p">[</span><span class="nx">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="nx">hanota</span><span class="p">}</span>
<div class="highlight"><span class="filename">hanota.go</span><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a><span class="cm">/* 移动一个圆盘 */</span>
<a id="__codelineno-3-2" name="__codelineno-3-2" href="#__codelineno-3-2"></a><span class="kd">func</span><span class="w"> </span><span class="nx">move</span><span class="p">(</span><span class="nx">src</span><span class="p">,</span><span class="w"> </span><span class="nx">tar</span><span class="w"> </span><span class="o">*</span><span class="nx">list</span><span class="p">.</span><span class="nx">List</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-3" name="__codelineno-3-3" href="#__codelineno-3-3"></a><span class="w"> </span><span class="c1">// 从 src 顶部拿出一个圆盘</span>
<a id="__codelineno-3-4" name="__codelineno-3-4" href="#__codelineno-3-4"></a><span class="w"> </span><span class="nx">pan</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">src</span><span class="p">.</span><span class="nx">Back</span><span class="p">()</span>
<a id="__codelineno-3-5" name="__codelineno-3-5" href="#__codelineno-3-5"></a><span class="w"> </span><span class="c1">// 将圆盘放入 tar 顶部</span>
<a id="__codelineno-3-6" name="__codelineno-3-6" href="#__codelineno-3-6"></a><span class="w"> </span><span class="nx">tar</span><span class="p">.</span><span class="nx">PushBack</span><span class="p">(</span><span class="nx">pan</span><span class="p">.</span><span class="nx">Value</span><span class="p">)</span>
<a id="__codelineno-3-7" name="__codelineno-3-7" href="#__codelineno-3-7"></a><span class="w"> </span><span class="c1">// 移除 src 顶部圆盘</span>
<a id="__codelineno-3-8" name="__codelineno-3-8" href="#__codelineno-3-8"></a><span class="w"> </span><span class="nx">src</span><span class="p">.</span><span class="nx">Remove</span><span class="p">(</span><span class="nx">pan</span><span class="p">)</span>
<a id="__codelineno-3-9" name="__codelineno-3-9" href="#__codelineno-3-9"></a><span class="p">}</span>
<a id="__codelineno-3-10" name="__codelineno-3-10" href="#__codelineno-3-10"></a>
<a id="__codelineno-3-11" name="__codelineno-3-11" href="#__codelineno-3-11"></a><span class="cm">/* 求解汉诺塔:问题 f(i) */</span>
<a id="__codelineno-3-12" name="__codelineno-3-12" href="#__codelineno-3-12"></a><span class="kd">func</span><span class="w"> </span><span class="nx">dfsHanota</span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="kt">int</span><span class="p">,</span><span class="w"> </span><span class="nx">src</span><span class="p">,</span><span class="w"> </span><span class="nx">buf</span><span class="p">,</span><span class="w"> </span><span class="nx">tar</span><span class="w"> </span><span class="o">*</span><span class="nx">list</span><span class="p">.</span><span class="nx">List</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-13" name="__codelineno-3-13" href="#__codelineno-3-13"></a><span class="w"> </span><span class="c1">// 若 src 只剩下一个圆盘,则直接将其移到 tar</span>
<a id="__codelineno-3-14" name="__codelineno-3-14" href="#__codelineno-3-14"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-15" name="__codelineno-3-15" href="#__codelineno-3-15"></a><span class="w"> </span><span class="nx">move</span><span class="p">(</span><span class="nx">src</span><span class="p">,</span><span class="w"> </span><span class="nx">tar</span><span class="p">)</span>
<a id="__codelineno-3-16" name="__codelineno-3-16" href="#__codelineno-3-16"></a><span class="w"> </span><span class="k">return</span>
<a id="__codelineno-3-17" name="__codelineno-3-17" href="#__codelineno-3-17"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-3-18" name="__codelineno-3-18" href="#__codelineno-3-18"></a><span class="w"> </span><span class="c1">// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf</span>
<a id="__codelineno-3-19" name="__codelineno-3-19" href="#__codelineno-3-19"></a><span class="w"> </span><span class="nx">dfsHanota</span><span class="p">(</span><span class="nx">i</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nx">src</span><span class="p">,</span><span class="w"> </span><span class="nx">tar</span><span class="p">,</span><span class="w"> </span><span class="nx">buf</span><span class="p">)</span>
<a id="__codelineno-3-20" name="__codelineno-3-20" href="#__codelineno-3-20"></a><span class="w"> </span><span class="c1">// 子问题 f(1) :将 src 剩余一个圆盘移到 tar</span>
<a id="__codelineno-3-21" name="__codelineno-3-21" href="#__codelineno-3-21"></a><span class="w"> </span><span class="nx">move</span><span class="p">(</span><span class="nx">src</span><span class="p">,</span><span class="w"> </span><span class="nx">tar</span><span class="p">)</span>
<a id="__codelineno-3-22" name="__codelineno-3-22" href="#__codelineno-3-22"></a><span class="w"> </span><span class="c1">// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar</span>
<a id="__codelineno-3-23" name="__codelineno-3-23" href="#__codelineno-3-23"></a><span class="w"> </span><span class="nx">dfsHanota</span><span class="p">(</span><span class="nx">i</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nx">buf</span><span class="p">,</span><span class="w"> </span><span class="nx">src</span><span class="p">,</span><span class="w"> </span><span class="nx">tar</span><span class="p">)</span>
<a id="__codelineno-3-24" name="__codelineno-3-24" href="#__codelineno-3-24"></a><span class="p">}</span>
<a id="__codelineno-3-25" name="__codelineno-3-25" href="#__codelineno-3-25"></a>
<a id="__codelineno-3-26" name="__codelineno-3-26" href="#__codelineno-3-26"></a><span class="cm">/* 求解汉诺塔 */</span>
<a id="__codelineno-3-27" name="__codelineno-3-27" href="#__codelineno-3-27"></a><span class="kd">func</span><span class="w"> </span><span class="nx">hanota</span><span class="p">(</span><span class="nx">A</span><span class="p">,</span><span class="w"> </span><span class="nx">B</span><span class="p">,</span><span class="w"> </span><span class="nx">C</span><span class="w"> </span><span class="o">*</span><span class="nx">list</span><span class="p">.</span><span class="nx">List</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-3-28" name="__codelineno-3-28" href="#__codelineno-3-28"></a><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">A</span><span class="p">.</span><span class="nx">Len</span><span class="p">()</span>
<a id="__codelineno-3-29" name="__codelineno-3-29" href="#__codelineno-3-29"></a><span class="w"> </span><span class="c1">// 将 A 顶部 n 个圆盘借助 B 移到 C</span>
<a id="__codelineno-3-30" name="__codelineno-3-30" href="#__codelineno-3-30"></a><span class="w"> </span><span class="nx">dfsHanota</span><span class="p">(</span><span class="nx">n</span><span class="p">,</span><span class="w"> </span><span class="nx">A</span><span class="p">,</span><span class="w"> </span><span class="nx">B</span><span class="p">,</span><span class="w"> </span><span class="nx">C</span><span class="p">)</span>
<a id="__codelineno-3-31" name="__codelineno-3-31" href="#__codelineno-3-31"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">

View file

@ -3048,28 +3048,21 @@
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
第一步:问题分析
贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
第二步:贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_3" class="md-nav__link">
代码实现
</a>
</li>
<li class="md-nav__item">
<a href="#_4" class="md-nav__link">
第三步:正确性证明
<a href="#_3" class="md-nav__link">
正确性证明
</a>
</li>
@ -3330,28 +3323,21 @@
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
第一步:问题分析
贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
第二步:贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_3" class="md-nav__link">
代码实现
</a>
</li>
<li class="md-nav__item">
<a href="#_4" class="md-nav__link">
第三步:正确性证明
<a href="#_3" class="md-nav__link">
正确性证明
</a>
</li>
@ -3388,7 +3374,6 @@
<p><img alt="分数背包问题的示例数据" src="../fractional_knapsack_problem.assets/fractional_knapsack_example.png" /></p>
<p align="center"> Fig. 分数背包问题的示例数据 </p>
<h3 id="_1">第一步:问题分析<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>本题和 0-1 背包整体上非常相似,状态包含当前物品 <span class="arithmatex">\(i\)</span> 和容量 <span class="arithmatex">\(c\)</span> ,目标是求不超过背包容量下的最大价值。</p>
<p>不同点在于,本题允许只选择物品的一部分,我们可以对物品任意地进行切分,并按照重量比例来计算物品价值,因此有:</p>
<ol>
@ -3398,7 +3383,7 @@
<p><img alt="物品在单位重量下的价值" src="../fractional_knapsack_problem.assets/fractional_knapsack_unit_value.png" /></p>
<p align="center"> Fig. 物品在单位重量下的价值 </p>
<h3 id="_2">第二步:贪心策略确定<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<h3 id="_1">贪心策略确定<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>最大化背包内物品总价值,<strong>本质上是要最大化单位重量下的物品价值</strong>。由此便可推出本题的贪心策略:</p>
<ol>
<li>将物品按照单位价值从高到低进行排序。</li>
@ -3408,7 +3393,7 @@
<p><img alt="分数背包的贪心策略" src="../fractional_knapsack_problem.assets/fractional_knapsack_greedy_strategy.png" /></p>
<p align="center"> Fig. 分数背包的贪心策略 </p>
<h3 id="_3">代码实现<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<h3 id="_2">代码实现<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<p>我们构建了一个物品类 <code>Item</code> ,以便将物品按照单位价值进行排序。循环进行贪心选择,当背包已满时跳出并返回解。</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:11"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Java</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Python</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">JavaScript</label><label for="__tabbed_1_6">TypeScript</label><label for="__tabbed_1_7">C</label><label for="__tabbed_1_8">C#</label><label for="__tabbed_1_9">Swift</label><label for="__tabbed_1_10">Zig</label><label for="__tabbed_1_11">Dart</label></div>
<div class="tabbed-content">
@ -3569,7 +3554,7 @@
</div>
</div>
<p>最差情况下,需要遍历整个物品列表,<strong>因此时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> ,其中 <span class="arithmatex">\(n\)</span> 为物品数量。由于初始化了一个 <code>Item</code> 对象列表,<strong>因此空间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong></p>
<h3 id="_4">第三步:正确性证明<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3>
<h3 id="_3">正确性证明<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<p>采用反证法。假设物品 <span class="arithmatex">\(x\)</span> 是单位价值最高的物品,使用某算法求得最大价值为 <span class="arithmatex">\(res\)</span> ,但该解中不包含物品 <span class="arithmatex">\(x\)</span></p>
<p>现在从背包中拿出单位重量的任意物品,并替换为单位重量的物品 <span class="arithmatex">\(x\)</span> 。由于物品 <span class="arithmatex">\(x\)</span> 的单位价值最高,因此替换后的总价值一定大于 <span class="arithmatex">\(res\)</span><strong>这与 <span class="arithmatex">\(res\)</span> 是最优解矛盾,说明最优解中必须包含物品 <span class="arithmatex">\(x\)</span></strong></p>
<p>对于该解中的其他物品,我们也可以构建出上述矛盾。总而言之,<strong>单位价值更大的物品总是更优选择</strong>,这说明贪心策略是有效的。</p>

View file

@ -3076,28 +3076,21 @@
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
第一步:问题分析
贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
第二步:贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_3" class="md-nav__link">
代码实现
</a>
</li>
<li class="md-nav__item">
<a href="#_4" class="md-nav__link">
第三步:正确性证明
<a href="#_3" class="md-nav__link">
正确性证明
</a>
</li>
@ -3330,28 +3323,21 @@
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
第一步:问题分析
贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
第二步:贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_3" class="md-nav__link">
代码实现
</a>
</li>
<li class="md-nav__item">
<a href="#_4" class="md-nav__link">
第三步:正确性证明
<a href="#_3" class="md-nav__link">
正确性证明
</a>
</li>
@ -3388,14 +3374,13 @@
<p><img alt="最大容量问题的示例数据" src="../max_capacity_problem.assets/max_capacity_example.png" /></p>
<p align="center"> Fig. 最大容量问题的示例数据 </p>
<h3 id="_1">第一步:问题分析<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>容器由任意两个隔板围成,<strong>因此本题的状态为两个隔板的索引,记为 <span class="arithmatex">\([i, j]\)</span></strong></p>
<p>根据定义,容量等于高度乘以宽度,其中高度由短板决定,宽度是两隔板的索引之差。设容量为 <span class="arithmatex">\(cap[i, j]\)</span> ,可得计算公式:</p>
<div class="arithmatex">\[
cap[i, j] = \min(ht[i], ht[j]) \times (j - i)
\]</div>
<p>设数组长度为 <span class="arithmatex">\(n\)</span> ,两个隔板的组合数量(即状态总数)为 <span class="arithmatex">\(C_n^2 = \frac{n(n - 1)}{2}\)</span> 个。最直接地,<strong>我们可以穷举所有状态</strong>,从而求得最大容量,时间复杂度为 <span class="arithmatex">\(O(n^2)\)</span></p>
<h3 id="_2">第二步:贪心策略确定<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<h3 id="_1">贪心策略确定<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>当然,这道题还有更高效率的解法。如下图所示,现选取一个状态 <span class="arithmatex">\([i, j]\)</span> ,其满足索引 <span class="arithmatex">\(i &lt; j\)</span> 且高度 <span class="arithmatex">\(ht[i] &lt; ht[j]\)</span> ,即 <span class="arithmatex">\(i\)</span> 为短板、 <span class="arithmatex">\(j\)</span> 为长板。</p>
<p><img alt="初始状态" src="../max_capacity_problem.assets/max_capacity_initial_state.png" /></p>
<p align="center"> Fig. 初始状态 </p>
@ -3450,7 +3435,7 @@ cap[i, j] = \min(ht[i], ht[j]) \times (j - i)
</div>
</div>
</div>
<h3 id="_3">代码实现<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<h3 id="_2">代码实现<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<p>如下代码所示,循环最多 <span class="arithmatex">\(n\)</span> 轮,<strong>因此时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> 。变量 <span class="arithmatex">\(i\)</span> , <span class="arithmatex">\(j\)</span> , <span class="arithmatex">\(res\)</span> 使用常数大小额外空间,<strong>因此空间复杂度为 <span class="arithmatex">\(O(1)\)</span></strong></p>
<div class="tabbed-set tabbed-alternate" data-tabs="2:11"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Java</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Python</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">JavaScript</label><label for="__tabbed_2_6">TypeScript</label><label for="__tabbed_2_7">C</label><label for="__tabbed_2_8">C#</label><label for="__tabbed_2_9">Swift</label><label for="__tabbed_2_10">Zig</label><label for="__tabbed_2_11">Dart</label></div>
<div class="tabbed-content">
@ -3554,7 +3539,7 @@ cap[i, j] = \min(ht[i], ht[j]) \times (j - i)
</div>
</div>
</div>
<h3 id="_4">第三步:正确性证明<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3>
<h3 id="_3">正确性证明<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<p>之所以贪心比穷举更快,是因为每轮的贪心选择都会“跳过”一些状态。</p>
<p>比如在状态 <span class="arithmatex">\(cap[i, j]\)</span> 下,<span class="arithmatex">\(i\)</span> 为短板、<span class="arithmatex">\(j\)</span> 为长板。若贪心地将短板 <span class="arithmatex">\(i\)</span> 向内移动一格,会导致以下状态被“跳过”,<strong>意味着之后无法验证这些状态的容量大小</strong></p>
<div class="arithmatex">\[

View file

@ -3104,28 +3104,21 @@
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
第一步:问题分析
贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
第二步:贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_3" class="md-nav__link">
代码实现
</a>
</li>
<li class="md-nav__item">
<a href="#_4" class="md-nav__link">
第三步:正确性证明
<a href="#_3" class="md-nav__link">
正确性证明
</a>
</li>
@ -3330,28 +3323,21 @@
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
第一步:问题分析
贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
第二步:贪心策略确定
</a>
</li>
<li class="md-nav__item">
<a href="#_3" class="md-nav__link">
代码实现
</a>
</li>
<li class="md-nav__item">
<a href="#_4" class="md-nav__link">
第三步:正确性证明
<a href="#_3" class="md-nav__link">
正确性证明
</a>
</li>
@ -3384,7 +3370,6 @@
<p class="admonition-title">Question</p>
<p>给定一个正整数 <span class="arithmatex">\(n\)</span> ,将其切分为至少两个正整数的和,求切分后所有整数的乘积最大是多少。</p>
</div>
<h3 id="_1">第一步:问题分析<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p><img alt="最大切分乘积的问题定义" src="../max_product_cutting_problem.assets/max_product_cutting_definition.png" /></p>
<p align="center"> Fig. 最大切分乘积的问题定义 </p>
@ -3397,7 +3382,7 @@ n = \sum_{i=1}^{m}n_i
\max(\prod_{i=1}^{m}n_i)
\]</div>
<p>我们需要思考的是:切分数量 <span class="arithmatex">\(m\)</span> 应该多大,每个 <span class="arithmatex">\(n_i\)</span> 应该是多少?</p>
<h3 id="_2">第二步:贪心策略确定<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<h3 id="_1">贪心策略确定<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>根据经验,两个整数的和往往比它们的积更小。假设从 <span class="arithmatex">\(n\)</span> 中分出一个因子 <span class="arithmatex">\(2\)</span> ,则它们的乘积为 <span class="arithmatex">\(2(n-2)\)</span> 。我们将该乘积与 <span class="arithmatex">\(n\)</span> 作比较:</p>
<div class="arithmatex">\[
\begin{aligned}
@ -3424,7 +3409,7 @@ n &amp; \geq 4
<li>当余数为 <span class="arithmatex">\(2\)</span> 时,不继续划分,保留之。</li>
<li>当余数为 <span class="arithmatex">\(1\)</span> 时,由于 <span class="arithmatex">\(2 \times 2 &gt; 1 \times 3\)</span> ,因此应将最后一个 <span class="arithmatex">\(3\)</span> 替换为 <span class="arithmatex">\(2\)</span></li>
</ol>
<h3 id="_3">代码实现<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<h3 id="_2">代码实现<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<p>在代码中,我们无需开启循环来切分,可以直接利用向下整除得到 <span class="arithmatex">\(3\)</span> 的个数 <span class="arithmatex">\(a\)</span> ,用取模运算得到余数 <span class="arithmatex">\(b\)</span> ,即:</p>
<div class="arithmatex">\[
n = 3 a + b
@ -3539,7 +3524,7 @@ n = 3 a + b
<li>函数 <code>math.pow()</code> 内部调用 C 语言库的 <code>pow()</code> 函数,其执行浮点取幂,时间复杂度为 <span class="arithmatex">\(O(1)\)</span></li>
</ul>
<p>变量 <span class="arithmatex">\(a\)</span> , <span class="arithmatex">\(b\)</span> 使用常数大小的额外空间,<strong>因此空间复杂度为 <span class="arithmatex">\(O(1)\)</span></strong></p>
<h3 id="_4">第三步:正确性证明<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3>
<h3 id="_3">正确性证明<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
<p>使用反证法,只分析 <span class="arithmatex">\(n \geq 3\)</span> 的情况。</p>
<ol>
<li><strong>所有因子 <span class="arithmatex">\(\leq 3\)</span></strong> :假设最优切分方案中存在 <span class="arithmatex">\(\geq 4\)</span> 的因子 <span class="arithmatex">\(x\)</span> ,那么一定可以将其继续划分为 <span class="arithmatex">\(2(x-2)\)</span> ,从而获得更大的乘积。这与假设矛盾。</li>

View file

@ -4711,8 +4711,8 @@
<h2 id="613">6.1.3. &nbsp; 哈希冲突与扩容<a class="headerlink" href="#613" title="Permanent link">&para;</a></h2>
<p>本质上看,哈希函数的作用是将输入空间(<code>key</code> 范围)映射到输出空间(数组索引范围),而输入空间往往远大于输出空间。因此,<strong>理论上一定存在“多个输入对应相同输出”的情况</strong></p>
<p>对于上述示例中的哈希函数,当输入的 <code>key</code> 后两位相同时,哈希函数的输出结果也相同。例如,查询学号为 12836 和 20336 的两个学生时,我们得到:</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-34-1" name="__codelineno-34-1" href="#__codelineno-34-1"></a><span class="m">12386</span><span class="w"> </span>%<span class="w"> </span><span class="nv">100</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">36</span>
<a id="__codelineno-34-2" name="__codelineno-34-2" href="#__codelineno-34-2"></a><span class="m">20386</span><span class="w"> </span>%<span class="w"> </span><span class="nv">100</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">36</span>
<div class="highlight"><pre><span></span><code><a id="__codelineno-34-1" name="__codelineno-34-1" href="#__codelineno-34-1"></a><span class="m">12836</span><span class="w"> </span>%<span class="w"> </span><span class="nv">100</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">36</span>
<a id="__codelineno-34-2" name="__codelineno-34-2" href="#__codelineno-34-2"></a><span class="m">20336</span><span class="w"> </span>%<span class="w"> </span><span class="nv">100</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">36</span>
</code></pre></div>
<p>如下图所示,两个学号指向了同一个姓名,这显然是不对的。我们将这种多个输入对应同一输出的情况称为「哈希冲突 Hash Collision」。</p>
<p><img alt="哈希冲突示例" src="../hash_map.assets/hash_collision.png" /></p>

File diff suppressed because one or more lines are too long

Binary file not shown.