This commit is contained in:
krahets 2023-09-11 04:31:56 +08:00
parent aa70e390c6
commit 7f61831f14
5 changed files with 47 additions and 11 deletions

View file

@ -3456,8 +3456,8 @@
<h1 id="162">16.2 &nbsp; 一起参与创作<a class="headerlink" href="#162" title="Permanent link">&para;</a></h1>
<p>由于作者能力有限,书中难免存在一些遗漏和错误,请您谅解。如果您发现了笔误、失效链接、内容缺失、文字歧义、解释不清晰或行文结构不合理等问题,请协助我们进行修正,以帮助其他读者获得更优质的学习资源。</p>
<p>所有<a href="https://github.com/krahets/hello-algo/graphs/contributors">撰稿人</a>的 GitHub ID 将在仓库、网页版和 PDF 版的主页上进行展示,以感谢他们对开源社区的无私奉献。</p>
<p>由于作者能力有限,书中难免存在一些遗漏和错误,请您谅解。如果您发现了笔误、失效链接、内容缺失、文字歧义、解释不清晰或行文结构不合理等问题,请协助我们进行修正,以给读者提供更优质的学习资源。</p>
<p>所有<a href="https://github.com/krahets/hello-algo/graphs/contributors">撰稿人</a>的 GitHub ID 将被展示本书的仓库主页上,以感谢他们对开源社区的无私奉献。</p>
<div class="admonition success">
<p class="admonition-title">开源的魅力</p>
<p>纸质书籍的两次印刷的间隔时间往往需要数年,内容更新非常不方便。</p>
@ -3484,10 +3484,8 @@
<li>刷新仓库网页点击“Create pull request”按钮即可发起拉取请求。</li>
</ol>
<h3 id="3-docker">3. &nbsp; Docker 部署<a class="headerlink" href="#3-docker" title="Permanent link">&para;</a></h3>
<p>执行以下 Docker 脚本,稍等片刻,即可在网页 <code>http://localhost:8000</code> 访问本项目。</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a>git<span class="w"> </span>clone<span class="w"> </span>https://github.com/krahets/hello-algo.git
<a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="nb">cd</span><span class="w"> </span>hello-algo
<a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a>docker-compose<span class="w"> </span>up<span class="w"> </span>-d
<p><code>hello-algo</code> 根目录下,执行以下 Docker 脚本,即可在 <code>http://localhost:8000</code> 访问本项目。</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a>docker-compose<span class="w"> </span>up<span class="w"> </span>-d
</code></pre></div>
<p>使用以下命令即可删除部署。</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>docker-compose<span class="w"> </span>down

View file

@ -3637,11 +3637,49 @@
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">top_k.js</span><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-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="nx">func</span><span class="p">]{</span><span class="nx">topKHeap</span><span class="p">}</span>
<div class="highlight"><span class="filename">top_k.js</span><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1" href="#__codelineno-6-1"></a><span class="cm">/* 基于堆查找数组中最大的 k 个元素 */</span>
<a id="__codelineno-6-2" name="__codelineno-6-2" href="#__codelineno-6-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">topKHeap</span><span class="p">(</span><span class="nx">nums</span><span class="p">,</span><span class="w"> </span><span class="nx">k</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-3" name="__codelineno-6-3" href="#__codelineno-6-3"></a><span class="w"> </span><span class="c1">// 使用大顶堆 MaxHeap对数组 nums 取相反数</span>
<a id="__codelineno-6-4" name="__codelineno-6-4" href="#__codelineno-6-4"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">invertedNums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">nums</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">num</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="o">-</span><span class="nx">num</span><span class="p">);</span>
<a id="__codelineno-6-5" name="__codelineno-6-5" href="#__codelineno-6-5"></a><span class="w"> </span><span class="c1">// 将数组的前 k 个元素入堆</span>
<a id="__codelineno-6-6" name="__codelineno-6-6" href="#__codelineno-6-6"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">heap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">MaxHeap</span><span class="p">(</span><span class="nx">invertedNums</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="nx">k</span><span class="p">));</span>
<a id="__codelineno-6-7" name="__codelineno-6-7" href="#__codelineno-6-7"></a><span class="w"> </span><span class="c1">// 从第 k+1 个元素开始,保持堆的长度为 k</span>
<a id="__codelineno-6-8" name="__codelineno-6-8" href="#__codelineno-6-8"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">k</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">invertedNums</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-9" name="__codelineno-6-9" href="#__codelineno-6-9"></a><span class="w"> </span><span class="c1">// 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆</span>
<a id="__codelineno-6-10" name="__codelineno-6-10" href="#__codelineno-6-10"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">invertedNums</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">peek</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-11" name="__codelineno-6-11" href="#__codelineno-6-11"></a><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
<a id="__codelineno-6-12" name="__codelineno-6-12" href="#__codelineno-6-12"></a><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">invertedNums</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
<a id="__codelineno-6-13" name="__codelineno-6-13" href="#__codelineno-6-13"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-6-14" name="__codelineno-6-14" href="#__codelineno-6-14"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-6-15" name="__codelineno-6-15" href="#__codelineno-6-15"></a><span class="w"> </span><span class="c1">// 取出堆中元素</span>
<a id="__codelineno-6-16" name="__codelineno-6-16" href="#__codelineno-6-16"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">maxHeap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">getMaxHeap</span><span class="p">();</span>
<a id="__codelineno-6-17" name="__codelineno-6-17" href="#__codelineno-6-17"></a><span class="w"> </span><span class="c1">// 对堆中元素取相反数</span>
<a id="__codelineno-6-18" name="__codelineno-6-18" href="#__codelineno-6-18"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">invertedMaxHeap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">maxHeap</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">num</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="o">-</span><span class="nx">num</span><span class="p">);</span>
<a id="__codelineno-6-19" name="__codelineno-6-19" href="#__codelineno-6-19"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">invertedMaxHeap</span><span class="p">;</span>
<a id="__codelineno-6-20" name="__codelineno-6-20" href="#__codelineno-6-20"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><span class="filename">top_k.ts</span><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-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="nx">func</span><span class="p">]{</span><span class="nx">topKHeap</span><span class="p">}</span>
<div class="highlight"><span class="filename">top_k.ts</span><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1" href="#__codelineno-7-1"></a><span class="cm">/* 基于堆查找数组中最大的 k 个元素 */</span>
<a id="__codelineno-7-2" name="__codelineno-7-2" href="#__codelineno-7-2"></a><span class="kd">function</span><span class="w"> </span><span class="nx">topKHeap</span><span class="p">(</span><span class="nx">nums</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[],</span><span class="w"> </span><span class="nx">k</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kt">number</span><span class="p">[]</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-3" name="__codelineno-7-3" href="#__codelineno-7-3"></a><span class="w"> </span><span class="c1">// 将堆中所有元素取反,从而用大顶堆来模拟小顶堆</span>
<a id="__codelineno-7-4" name="__codelineno-7-4" href="#__codelineno-7-4"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">invertedNums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">nums</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">num</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="o">-</span><span class="nx">num</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="c1">// 将数组的前 k 个元素入堆</span>
<a id="__codelineno-7-6" name="__codelineno-7-6" href="#__codelineno-7-6"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">heap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">MaxHeap</span><span class="p">(</span><span class="nx">invertedNums</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="nx">k</span><span class="p">));</span>
<a id="__codelineno-7-7" name="__codelineno-7-7" href="#__codelineno-7-7"></a><span class="w"> </span><span class="c1">// 从第 k+1 个元素开始,保持堆的长度为 k</span>
<a id="__codelineno-7-8" name="__codelineno-7-8" href="#__codelineno-7-8"></a><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">k</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">invertedNums</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-9" name="__codelineno-7-9" href="#__codelineno-7-9"></a><span class="w"> </span><span class="c1">// 若当前元素小于堆顶元素,则将堆顶元素出堆、当前元素入堆</span>
<a id="__codelineno-7-10" name="__codelineno-7-10" href="#__codelineno-7-10"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">invertedNums</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">peek</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-11" name="__codelineno-7-11" href="#__codelineno-7-11"></a><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
<a id="__codelineno-7-12" name="__codelineno-7-12" href="#__codelineno-7-12"></a><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">invertedNums</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
<a id="__codelineno-7-13" name="__codelineno-7-13" href="#__codelineno-7-13"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-7-14" name="__codelineno-7-14" href="#__codelineno-7-14"></a><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-7-15" name="__codelineno-7-15" href="#__codelineno-7-15"></a><span class="w"> </span><span class="c1">// 取出堆中元素</span>
<a id="__codelineno-7-16" name="__codelineno-7-16" href="#__codelineno-7-16"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">maxHeap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">heap</span><span class="p">.</span><span class="nx">getMaxHeap</span><span class="p">();</span>
<a id="__codelineno-7-17" name="__codelineno-7-17" href="#__codelineno-7-17"></a><span class="w"> </span><span class="c1">// 对堆中元素取相反数</span>
<a id="__codelineno-7-18" name="__codelineno-7-18" href="#__codelineno-7-18"></a><span class="w"> </span><span class="kd">const</span><span class="w"> </span><span class="nx">invertedMaxHeap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">maxHeap</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">num</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="o">-</span><span class="nx">num</span><span class="p">);</span>
<a id="__codelineno-7-19" name="__codelineno-7-19" href="#__codelineno-7-19"></a><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">invertedMaxHeap</span><span class="p">;</span>
<a id="__codelineno-7-20" name="__codelineno-7-20" href="#__codelineno-7-20"></a><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">

View file

@ -3423,8 +3423,8 @@
<hr />
<h2 align="center"></h2>
<p>两年前,我在力扣上分享了《剑指 Offer》系列题解受到了许多同学的喜爱和支持。在与读者的交流期间最常收到的一个问题是“如何入门学习算法”。逐渐地,我对这个问题产生了浓厚的兴趣。</p>
<p>两眼一抹黑地刷题似乎是最受欢迎的方法,简单直接且有效。然而刷题就如同玩“扫雷”游戏,自学能力强的同学能够顺利地将地雷逐个排掉,而基础不足的同学很可能被炸的满头是包,并在挫折中步步退缩。通读教材书籍也是一种常见做法,但对于面向求职的同学来说,毕业季、投递简历、准备笔试面试已经占据了大部分精力,厚重的书籍往往变成了一项艰巨的挑战。</p>
<p>两年前,我在力扣上分享了《剑指 Offer》系列题解受到了许多同学的喜爱和支持。在与读者的交流期间最常收到的一个问题是“如何入门学习算法”。逐渐对这个问题产生了浓厚的兴趣。</p>
<p>两眼一抹黑地刷题似乎是最受欢迎的方法,简单直接且有效。刷题就如同玩“扫雷”游戏,自学能力强的同学能够顺利地将地雷逐个排掉,而基础不足的同学很可能被炸的满头是包,并在挫折中步步退缩。通读教材书籍也是一种常见做法,但对于面向求职的同学来说,毕业季、投递简历、准备笔试面试已经占据了大部分精力,厚重的书籍往往变成了一项艰巨的挑战。</p>
<p>如果你也面临类似的困扰,那么很幸运这本书找到了你。本书是我对此问题的给出的答案,即使不是最优解,也至少是一次积极的尝试。这本书虽然不足以让你直接拿到 Offer ,但会引导你探索数据结构与算法的“知识地图”,带你了解不同“地雷”的形状大小和分布位置,让你掌握各种“排雷方法”。有了这些本领,相信你可以更加自如地应对刷题和阅读文献,逐步构建起完整的知识体系。</p>
<h3 align="left"> 作者简介 </h3>

File diff suppressed because one or more lines are too long

Binary file not shown.