This commit is contained in:
krahets
2026-11-04 05:26:02 +08:00
parent 10963b9c01
commit 81e6fc70d7
30312 changed files with 24023 additions and 14508 deletions

View File

@@ -47,7 +47,7 @@
set(key, value, storage, key === "__palette" ? shared : scope);
})();
</script>
<link href="/assets/figure-svg/css/5632068fdac847e7b03f598e.css" rel="stylesheet"/><link href="/assets/figure-svg/css/320d37a71f8e9962e76c8fc3.css" rel="stylesheet"/><link href="/assets/figure-svg/css/c053e5d2507ba0f764c3d1cf.css" rel="stylesheet"/></head>
<link href="/assets/figure-svg/css/5632068fdac847e7b03f598e.css" rel="stylesheet"/><link href="/assets/figure-svg/css/c053e5d2507ba0f764c3d1cf.css" rel="stylesheet"/></head>
<body data-md-color-accent="teal" data-md-color-primary="white" data-md-color-scheme="default" dir="ltr">
<input autocomplete="off" class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
<input autocomplete="off" class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
@@ -1926,7 +1926,7 @@
<p>Given a staircase, you can climb <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(2\)</span> steps at a time. Each step has a nonnegative integer indicating the cost paid on that step. A nonnegative integer array <span class="arithmatex">\(cost\)</span> gives the costs: <span class="arithmatex">\(cost[i]\)</span> is the cost of step <span class="arithmatex">\(i\)</span>, and <span class="arithmatex">\(cost[0]\)</span> refers to the ground (the starting point). What is the minimum cost of reaching the top?</p>
</div>
<p>As shown in Figure 14-6, if steps <span class="arithmatex">\(1\)</span>, <span class="arithmatex">\(2\)</span>, and <span class="arithmatex">\(3\)</span> cost <span class="arithmatex">\(1\)</span>, <span class="arithmatex">\(10\)</span>, and <span class="arithmatex">\(1\)</span>, respectively, the minimum cost of reaching step <span class="arithmatex">\(3\)</span> from the ground is <span class="arithmatex">\(2\)</span>.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.png" data-dark-src="/assets/figure-svg/figures/eb9cc507a2980328e9d307a7.json" data-src="/assets/figure-svg/figures/6dbb38bdac9fd99917c640b2.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Minimum cost of reaching step 3</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Minimum cost of reaching step 3</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.png" data-dark-src="/assets/figure-svg/figures/c62ed5cccc2f5759c728fc35.json" data-src="/assets/figure-svg/figures/0b08ddb2d53496a1c69ea629.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Minimum cost of reaching step 3</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Minimum cost of reaching step 3</object></ha-svg-figure></p>
<p align="center"> Figure 14-6   Minimum cost of reaching step 3 </p>
<p>Let <span class="arithmatex">\(dp[i]\)</span> be the total cost of reaching step <span class="arithmatex">\(i\)</span>. Since step <span class="arithmatex">\(i\)</span> can only be reached from step <span class="arithmatex">\(i - 1\)</span> or <span class="arithmatex">\(i - 2\)</span>, <span class="arithmatex">\(dp[i]\)</span> must be either <span class="arithmatex">\(dp[i - 1] + cost[i]\)</span> or <span class="arithmatex">\(dp[i - 2] + cost[i]\)</span>. To minimize the cost, choose the smaller of the two:</p>
<div class="arithmatex">\[
@@ -2198,7 +2198,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20min_cost_climbing_stairs_dp%28cost%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Minimum%20cost%20to%20climb%20stairs%3A%20dynamic%20programming%22%22%22%0A%20%20%20%20n%20%3D%20len%28cost%29%20-%201%0A%20%20%20%20if%20n%20%3D%3D%201%20or%20n%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20cost%5Bn%5D%0A%20%20%20%20%23%20Initialize%20the%20dp%20table%20to%20store%20subproblem%20solutions%0A%20%20%20%20dp%20%3D%20%5B0%5D%20%2A%20%28n%20%2B%201%29%0A%20%20%20%20%23%20Initial%20states%3A%20set%20the%20solutions%20to%20the%20smallest%20subproblems%0A%20%20%20%20dp%5B1%5D,%20dp%5B2%5D%20%3D%20cost%5B1%5D,%20cost%5B2%5D%0A%20%20%20%20%23%20State%20transitions%3A%20solve%20progressively%20larger%20subproblems%20using%20smaller%20ones%0A%20%20%20%20for%20i%20in%20range%283,%20n%20%2B%201%29%3A%0A%20%20%20%20%20%20%20%20dp%5Bi%5D%20%3D%20min%28dp%5Bi%20-%201%5D,%20dp%5Bi%20-%202%5D%29%20%2B%20cost%5Bi%5D%0A%20%20%20%20return%20dp%5Bn%5D%0A%0A%0A%22%22%22Driver%20code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20cost%20%3D%20%5B0,%201,%2010,%201,%201,%201,%2010,%201,%201,%2010,%201%5D%0A%20%20%20%20print%28f%22Input%20list%20of%20stair%20costs%3A%20%7Bcost%7D%22%29%0A%0A%20%20%20%20res%20%3D%20min_cost_climbing_stairs_dp%28cost%29%0A%20%20%20%20print%28f%22Minimum%20cost%20to%20finish%20climbing%20the%20stairs%3A%20%7Bres%7D%22%29&amp;codeDivHeight=800&amp;codeDivWidth=600&amp;cumulative=false&amp;curInstr=4&amp;heapPrimitives=nevernest&amp;origin=opt-frontend.js&amp;py=311&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false" rel="noopener noreferrer" target="_blank">View full screen &gt;</a></div></p>
</details>
<p>Figure 14-7 illustrates this dynamic programming process.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.png" data-dark-src="/assets/figure-svg/figures/372a529e1caa90b284896ce5.json" data-src="/assets/figure-svg/figures/20457f455c6d54e69cad6b4a.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Dynamic programming for the minimum climbing cost</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Dynamic programming for the minimum climbing cost</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.png" data-dark-src="/assets/figure-svg/figures/32752408150c70bf8aa5af43.json" data-src="/assets/figure-svg/figures/1d43977d1ed5edb21a8e8ddd.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Dynamic programming for the minimum climbing cost</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Dynamic programming for the minimum climbing cost</object></ha-svg-figure></p>
<p align="center"> Figure 14-7   Dynamic programming for the minimum climbing cost </p>
<p>We can also reduce the state storage from a one-dimensional array to scalar variables, lowering the space complexity from <span class="arithmatex">\(O(n)\)</span> to <span class="arithmatex">\(O(1)\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="2:13"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio"/><input id="__tabbed_2_2" name="__tabbed_2" type="radio"/><input id="__tabbed_2_3" name="__tabbed_2" type="radio"/><input id="__tabbed_2_4" name="__tabbed_2" type="radio"/><input id="__tabbed_2_5" name="__tabbed_2" type="radio"/><input id="__tabbed_2_6" name="__tabbed_2" type="radio"/><input id="__tabbed_2_7" name="__tabbed_2" type="radio"/><input id="__tabbed_2_8" name="__tabbed_2" type="radio"/><input id="__tabbed_2_9" name="__tabbed_2" type="radio"/><input id="__tabbed_2_10" name="__tabbed_2" type="radio"/><input id="__tabbed_2_11" name="__tabbed_2" type="radio"/><input id="__tabbed_2_12" name="__tabbed_2" type="radio"/><input id="__tabbed_2_13" name="__tabbed_2" type="radio"/><div class="tabbed-labels"><label for="__tabbed_2_1">Python</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Java</label><label for="__tabbed_2_4">C#</label><label for="__tabbed_2_5">Go</label><label for="__tabbed_2_6">Swift</label><label for="__tabbed_2_7">JS</label><label for="__tabbed_2_8">TS</label><label for="__tabbed_2_9">Dart</label><label for="__tabbed_2_10">Rust</label><label for="__tabbed_2_11">C</label><label for="__tabbed_2_12">Kotlin</label><label for="__tabbed_2_13">Ruby</label></div>
@@ -2443,7 +2443,7 @@ dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]
<p>Given a staircase with <span class="arithmatex">\(n\)</span> steps, you may climb <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(2\)</span> steps at a time, <strong>but may not make two consecutive moves of <span class="arithmatex">\(1\)</span> step</strong>. How many ways are there to reach the top?</p>
</div>
<p>As shown in Figure 14-8, only <span class="arithmatex">\(2\)</span> ways remain to reach step <span class="arithmatex">\(3\)</span>: taking <span class="arithmatex">\(1\)</span> step three times in a row violates the constraint and is discarded.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.png" data-dark-src="/assets/figure-svg/figures/375606590f505a68d89f5816.json" data-src="/assets/figure-svg/figures/d2ad068c9a7ad0a61062e4ad.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3 with the constraint</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3 with the constraint</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.png" data-dark-src="/assets/figure-svg/figures/7c3e36a8e6223a895a791021.json" data-src="/assets/figure-svg/figures/d9d2230fd2e40bbdd28f0ed2.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3 with the constraint</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3 with the constraint</object></ha-svg-figure></p>
<p align="center"> Figure 14-8   Number of ways to reach step 3 with the constraint </p>
<p>If the previous move climbed <span class="arithmatex">\(1\)</span> step, the next must climb <span class="arithmatex">\(2\)</span> steps. Thus, <strong>the next choice cannot be determined by the current state (the current step number) alone; it also depends on the preceding state (the previous step number)</strong>.</p>
<p>The no-aftereffect property no longer holds with this state definition, and <span class="arithmatex">\(dp[i] = dp[i-1] + dp[i-2]\)</span> is no longer valid. Using <span class="arithmatex">\(dp[i-1]\)</span> means taking <span class="arithmatex">\(1\)</span> step now, but it includes many ways in which the preceding move also took <span class="arithmatex">\(1\)</span> step. We cannot simply add all of <span class="arithmatex">\(dp[i-1]\)</span> to <span class="arithmatex">\(dp[i]\)</span> without violating the constraint.</p>
@@ -2459,7 +2459,7 @@ dp[i, 1] = dp[i-1, 2] \\
dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
\end{cases}
\]</div>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.png" data-dark-src="/assets/figure-svg/figures/ddcda1a14448ceed3ffa3ad5.json" data-src="/assets/figure-svg/figures/8dd3da8d2a4f9aee17f42ab0.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recurrence with the constraint</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recurrence with the constraint</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.png" data-dark-src="/assets/figure-svg/figures/eb28968b1b0343c6c6da6ec5.json" data-src="/assets/figure-svg/figures/526078e41bc5bc8620e36b18.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recurrence with the constraint</object><object data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recurrence with the constraint</object></ha-svg-figure></p>
<p align="center"> Figure 14-9   Recurrence with the constraint </p>
<p>Finally, return <span class="arithmatex">\(dp[n, 1] + dp[n, 2]\)</span>, the total number of ways to reach step <span class="arithmatex">\(n\)</span>:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="3:13"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio"/><input id="__tabbed_3_2" name="__tabbed_3" type="radio"/><input id="__tabbed_3_3" name="__tabbed_3" type="radio"/><input id="__tabbed_3_4" name="__tabbed_3" type="radio"/><input id="__tabbed_3_5" name="__tabbed_3" type="radio"/><input id="__tabbed_3_6" name="__tabbed_3" type="radio"/><input id="__tabbed_3_7" name="__tabbed_3" type="radio"/><input id="__tabbed_3_8" name="__tabbed_3" type="radio"/><input id="__tabbed_3_9" name="__tabbed_3" type="radio"/><input id="__tabbed_3_10" name="__tabbed_3" type="radio"/><input id="__tabbed_3_11" name="__tabbed_3" type="radio"/><input id="__tabbed_3_12" name="__tabbed_3" type="radio"/><input id="__tabbed_3_13" name="__tabbed_3" type="radio"/><div class="tabbed-labels"><label for="__tabbed_3_1">Python</label><label for="__tabbed_3_2">C++</label><label for="__tabbed_3_3">Java</label><label for="__tabbed_3_4">C#</label><label for="__tabbed_3_5">Go</label><label for="__tabbed_3_6">Swift</label><label for="__tabbed_3_7">JS</label><label for="__tabbed_3_8">TS</label><label for="__tabbed_3_9">Dart</label><label for="__tabbed_3_10">Rust</label><label for="__tabbed_3_11">C</label><label for="__tabbed_3_12">Kotlin</label><label for="__tabbed_3_13">Ruby</label></div>
@@ -2887,5 +2887,5 @@ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2]
<script src="../../javascripts/mathjax.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.min.js"></script>
<script src="../../javascripts/animation_player.js"></script>
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/d706a8d4337d8499273c0243.js"></script></body>
<script defer="" src="/assets/figure-svg/js/bcdea376f01bc2bc0cac7c6c.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/99a707e9f0a14d8280f16808.js"></script><script defer="" src="/assets/figure-svg/js/855fb5bb73c22cf5c2cea474.js"></script></body>
</html>