deploy
@@ -18,7 +18,7 @@
|
||||
<link rel="prev" href="../greedy_algorithm/">
|
||||
|
||||
|
||||
<link rel="next" href="../../chapter_appendix/">
|
||||
<link rel="next" href="../max_capacity_problem/">
|
||||
|
||||
|
||||
<link rel="icon" href="../../assets/images/favicon.png">
|
||||
@@ -2896,6 +2896,8 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3013,6 +3015,34 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="../max_capacity_problem/" class="md-nav__link">
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
15.3. 最大容量问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
@@ -3246,12 +3276,7 @@
|
||||
<p><img alt="分数背包的贪心策略" src="../fractional_knapsack_problem.assets/fractional_knapsack_greedy_strategy.png" /></p>
|
||||
<p align="center"> Fig. 分数背包的贪心策略 </p>
|
||||
|
||||
<p><strong>第三步:正确性证明</strong></p>
|
||||
<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>
|
||||
<p><strong>实现代码</strong></p>
|
||||
<p>我们构建了一个物品类 <code>Item</code> ,以便将物品按照单位价值进行排序。在循环贪心选择中,分为放入整个物品或放入部分物品两种情况。当背包已满时,则跳出循环并返回解。</p>
|
||||
<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">
|
||||
<div class="tabbed-block">
|
||||
@@ -3410,12 +3435,15 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>如下图所示,如果将一个 2D 图表的横轴和纵轴分别看作物品重量和物品单位价值,则分数背包问题可被转化为“求在有限横轴区间下的最大围成面积”。这个类比可以帮助我们从几何角度清晰地看到贪心策略的有效性。</p>
|
||||
<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>
|
||||
<p><strong>第三步:正确性证明</strong></p>
|
||||
<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>
|
||||
<p>如下图所示,如果将物品重量和物品单位价值分别看作一个 2D 图表的横轴和纵轴,则分数背包问题可被转化为“求在有限横轴区间下的最大围成面积”。这个类比可以帮助我们从几何角度清晰地看到贪心策略的有效性。</p>
|
||||
<p><img alt="分数背包问题的几何表示" src="../fractional_knapsack_problem.assets/fractional_knapsack_area_chart.png" /></p>
|
||||
<p align="center"> Fig. 分数背包问题的几何表示 </p>
|
||||
|
||||
<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>
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3510,13 +3538,13 @@
|
||||
|
||||
|
||||
|
||||
<a href="../../chapter_appendix/" class="md-footer__link md-footer__link--next" aria-label="下一页: 16. &nbsp; 附录" rel="next">
|
||||
<a href="../max_capacity_problem/" class="md-footer__link md-footer__link--next" aria-label="下一页: 15.3. &nbsp; 最大容量问题" rel="next">
|
||||
<div class="md-footer__title">
|
||||
<span class="md-footer__direction">
|
||||
下一页
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
16. 附录
|
||||
15.3. 最大容量问题
|
||||
</div>
|
||||
</div>
|
||||
<div class="md-footer__button md-icon">
|
||||
|
||||
@@ -2896,6 +2896,8 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3079,6 +3081,34 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="../max_capacity_problem/" class="md-nav__link">
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
15.3. 最大容量问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
@@ -3329,7 +3359,7 @@
|
||||
<p>我们先通过例题“零钱兑换”来初步了解贪心算法的工作原理。这道题已经在动态规划章节中介绍过,相信你对它并不陌生。</p>
|
||||
<div class="admonition question">
|
||||
<p class="admonition-title">Question</p>
|
||||
<p>给定 <span class="arithmatex">\(n\)</span> 种硬币,第 <span class="arithmatex">\(i\)</span> 个硬币的面值为 <span class="arithmatex">\(coins[i - 1]\)</span> ,目标金额为 <span class="arithmatex">\(amt\)</span> ,<strong>每种硬币可以重复选取</strong>,问能够凑出目标金额的最少硬币个数。如果无法凑出目标金额则返回 <span class="arithmatex">\(-1\)</span> 。</p>
|
||||
<p>给定 <span class="arithmatex">\(n\)</span> 种硬币,第 <span class="arithmatex">\(i\)</span> 个硬币的面值为 <span class="arithmatex">\(coins[i - 1]\)</span> ,目标金额为 <span class="arithmatex">\(amt\)</span> ,每种硬币可以重复选取,问能够凑出目标金额的最少硬币个数。如果无法凑出目标金额则返回 <span class="arithmatex">\(-1\)</span> 。</p>
|
||||
</div>
|
||||
<p>贪心算法会迭代地做出一个又一个的贪心选择,每轮都将问题转化成一个规模更小的子问题,直到问题被解决。</p>
|
||||
<p>这道题的贪心策略在生活中很常见:给定目标金额,<strong>我们贪心地选择不大于且最接近它的硬币</strong>,不断循环该步骤,直至凑出目标金额为止。</p>
|
||||
@@ -3471,10 +3501,10 @@
|
||||
<li><strong>正确性证明</strong>:通常需要证明问题具有贪心选择性质和最优子结构。这个步骤可能需要使用到数学证明,例如归纳法或反证法等。</li>
|
||||
</ol>
|
||||
<p>确定贪心策略是求解问题的核心步骤,但实施起来并没有那么容易。主要有两方面原因:</p>
|
||||
<ol>
|
||||
<ul>
|
||||
<li><strong>不同问题的贪心策略的差异较大</strong>。对于许多问题来说,贪心策略都比较浅显,我们通过一些大概的思考与尝试就能得出。而对于一些复杂问题,贪心策略可能非常隐蔽,这种情况就非常考验个人的解题经验与算法能力了。</li>
|
||||
<li><strong>某些贪心策略具有较强的迷惑性</strong>。当我们满怀信心设计好贪心策略,写出解题代码并提交运行,很可能发现部分测试样例无法通过。这是因为设计的贪心策略只是“部分正确”的,上文介绍的零钱兑换就是个很好的例子。</li>
|
||||
</ol>
|
||||
</ul>
|
||||
<p>为了保证正确性,我们应该对贪心策略进行严谨的数学证明,<strong>通常需要用到反证法或数学归纳法</strong>。</p>
|
||||
<p>然而,正确性证明往往也不是一件易事。如若没有头绪,我们通常会选择面向测试用例进行 Debug ,一步步修改与验证贪心策略。</p>
|
||||
<h2 id="1514">15.1.4. 贪心典型例题<a class="headerlink" href="#1514" title="Permanent link">¶</a></h2>
|
||||
|
||||
@@ -2896,6 +2896,8 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3003,6 +3005,34 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<li class="md-nav__item">
|
||||
<a href="max_capacity_problem/" class="md-nav__link">
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
15.3. 最大容量问题
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
<span class="md-status md-status--new" title="最近添加">
|
||||
</span>
|
||||
|
||||
|
||||
|
||||
|
||||
</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
@@ -3230,6 +3260,7 @@
|
||||
<ul>
|
||||
<li><a href="https://www.hello-algo.com/chapter_greedy/greedy_algorithm/">15.1 贪心算法</a></li>
|
||||
<li><a href="https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/">15.2 分数背包问题</a></li>
|
||||
<li><a href="https://www.hello-algo.com/chapter_greedy/max_capacity_problem/">15.3 最大容量问题</a></li>
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 41 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 54 KiB |
|
After Width: | Height: | Size: 54 KiB |
|
After Width: | Height: | Size: 55 KiB |
|
After Width: | Height: | Size: 55 KiB |
|
After Width: | Height: | Size: 55 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 50 KiB |
|
After Width: | Height: | Size: 73 KiB |