mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-23 12:12:01 +08:00
Release reviewed gh-pages candidate
This commit is contained in:
@@ -1926,8 +1926,8 @@
|
||||
<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/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><ha-svg-figure aria-label="Minimum cost to climb a three-step staircase" 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-figure-accessible="" data-src="/assets/figure-svg/figures/0b08ddb2d53496a1c69ea629.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Minimum cost of reaching step 3</object><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_example.dark.svg" data-theme="dark" inert="" 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><div class="ha-sr-only figure-description"><h3>Description of Figure 14-6: Minimum cost to climb a three-step staircase</h3><p>With costs 0, 1, 10, and 1 on the ground and steps 1 through 3, stepping over cost-10 step 2 along 0→1→3 achieves the minimum total cost of 2.</p><p>Each move climbs 1 or 2 steps and pays the cost of every step landed on; the figure labels step numbers as n and step costs as cost[n]. Ground start has cost[0] = 0, and the goal is to land on step 3 and pay step 3's cost.</p><p>Aligned beneath the staircase are step numbers n = 0, 1, 2, 3 and costs cost[n] = 0, 1, 10, 1, with step 2's tread and cost 10 highlighted in red and target step 3's tread highlighted in green. Above the steps, a solid cyan curve traces 0→1→2→3, a blue dotted curve traces 0→2→3, and an orange dashed curve traces 0→1→3. The bottom note states that among these 3 routes, 0→1→3 has the minimum cost of 2.</p><p>Single-step costs in the table below come from cost in the figure, while the sums and totals 12 and 11 are computed by adding the steps visited rather than printed in the figure.</p><table><caption>Costs of the three routes (computed from cost in the figure)</caption><thead><tr><th scope="col">Route</th><th scope="col">Cost calculation</th><th scope="col">Total cost</th></tr></thead><tbody><tr><th scope="row">0→1→2→3</th><td>1+10+1</td><td>12</td></tr><tr><th scope="row">0→2→3</th><td>10+1</td><td>11</td></tr><tr><th scope="row">0→1→3</th><td>1+1</td><td>2</td></tr></tbody></table><p>Both of the first two routes land on step 2 of cost 10; only 0→1→3 steps over it, achieving the lowest total cost.</p></div>
|
||||
<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">\[
|
||||
dp[i] = \min(dp[i-1], dp[i-2]) + cost[i]
|
||||
@@ -2198,8 +2198,8 @@ 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&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=4&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" rel="noopener noreferrer" target="_blank">View full screen ></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/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><ha-svg-figure aria-label="State transitions for minimum climbing cost" 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-figure-accessible="" data-src="/assets/figure-svg/figures/1d43977d1ed5edb21a8e8ddd.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Dynamic programming for the minimum climbing cost</object><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/min_cost_cs_dp.dark.svg" data-theme="dark" inert="" 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><div class="ha-sr-only figure-description"><h3>Description of Figure 14-7: State transitions for minimum climbing cost</h3><p>Each state takes the smaller cumulative minimum cost of its two predecessors and adds cost[i], the cost of the current step.</p><p>Each move climbs 1 or 2 steps, cost[i] is the nonnegative cost paid upon landing on step i, and starting on the ground is free. Let dp[i] denote the minimum cumulative cost to reach step i.</p><p>Green blocks dp[1] through dp[9] run horizontally from left to right; odd-indexed cost labels sit above and even-indexed ones sit below, pointing into their respective dp blocks. States dp[1] and dp[2] receive only their own cost arrows; starting at dp[3], brackets and min markers combine the two preceding states into the next state, with a plus sign beside the incoming cost arrow for paying the current step's cost. Below the diagram appear the initial states and general recurrence dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i] without concrete cost numbers.</p><p>The final move onto step i must come from either step i - 1 or step i - 2. Because landing on step i pays cost[i] regardless of predecessor, compare predecessor costs dp[i - 1] and dp[i - 2] to pick the smaller cumulative cost and then add cost[i].</p><p>From the ground one can step directly onto step 1 or step 2, giving dp[1] = cost[1]. Because cost[1] is nonnegative, passing through step 1 on the way to step 2 can never cost less than stepping straight onto step 2, so dp[2] = cost[2]. The table below lists steps 1 through 9 individually for lookup; the "Ground" origin and "no worse than via step 1" notes are supplied from the problem rules rather than a separate ground node drawn in the figure.</p><table><caption>Initial values and predecessor comparisons</caption><thead><tr><th scope="col">State</th><th scope="col">Initial source or compared predecessors</th><th scope="col">Cost paid at step</th><th scope="col">Formula</th></tr></thead><tbody><tr><th scope="row">dp[1]</th><td>Ground</td><td>cost[1]</td><td>dp[1] = cost[1]</td></tr><tr><th scope="row">dp[2]</th><td>Two-step jump directly from ground (no worse than via step 1)</td><td>cost[2]</td><td>dp[2] = cost[2]</td></tr><tr><th scope="row">dp[3]</th><td>dp[2] or dp[1]</td><td>cost[3]</td><td>min(dp[2], dp[1]) + cost[3]</td></tr><tr><th scope="row">dp[4]</th><td>dp[3] or dp[2]</td><td>cost[4]</td><td>min(dp[3], dp[2]) + cost[4]</td></tr><tr><th scope="row">dp[5]</th><td>dp[4] or dp[3]</td><td>cost[5]</td><td>min(dp[4], dp[3]) + cost[5]</td></tr><tr><th scope="row">dp[6]</th><td>dp[5] or dp[4]</td><td>cost[6]</td><td>min(dp[5], dp[4]) + cost[6]</td></tr><tr><th scope="row">dp[7]</th><td>dp[6] or dp[5]</td><td>cost[7]</td><td>min(dp[6], dp[5]) + cost[7]</td></tr><tr><th scope="row">dp[8]</th><td>dp[7] or dp[6]</td><td>cost[8]</td><td>min(dp[7], dp[6]) + cost[8]</td></tr><tr><th scope="row">dp[9]</th><td>dp[8] or dp[7]</td><td>cost[9]</td><td>min(dp[8], dp[7]) + cost[9]</td></tr></tbody></table><p>Evaluating sequentially up to the target step yields the minimum cumulative cost in that step's dp entry.</p></div>
|
||||
<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>
|
||||
<div class="tabbed-content">
|
||||
@@ -2430,8 +2430,8 @@ 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/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><ha-svg-figure aria-label="Ways to reach step 3 when two consecutive 1-step moves are forbidden" 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-figure-accessible="" data-src="/assets/figure-svg/figures/d9d2230fd2e40bbdd28f0ed2.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3 with the constraint</object><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_example.dark.svg" data-theme="dark" inert="" 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><div class="ha-sr-only figure-description"><h3>Description of Figure 14-8: Ways to reach step 3 when two consecutive 1-step moves are forbidden</h3><p>Adding the constraint against two consecutive 1-step moves discards 0→1→2→3, leaving 2 valid ways to reach step 3.</p><p>Starting from ground level 0, each move may still climb 1 or 2 steps toward target step 3, under the added constraint that two consecutive moves may not both climb 1 step.</p><p>The staircase labels steps 0 through 6 with target step 3 shaded green. Route 0→1→2→3 is drawn as a gray dotted curve with the callout above "Because two consecutive one-step moves are forbidden, the route 0 → 1 → 2 → 3 is discarded"; the solid blue curve 0→2→3 and solid orange curve 0→1→3 remain highlighted, and the bottom caption reads "There are 2 ways to reach step 3: 0 → 2 → 3, 0 → 1 → 3".</p><p>Route 0→1→2→3 takes three consecutive 1-step moves, violating the ban on two consecutive 1-step moves in its very first two moves, so it is grayed out and discarded. Blue route 0→2→3 climbs 2 steps then 1 step, and orange route 0→1→3 climbs 1 step then 2 steps; neither contains adjacent 1-step moves, so both remain valid.</p></div>
|
||||
<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>
|
||||
<p>We therefore expand the state definition: <strong>state <span class="arithmatex">\([i, j]\)</span> means we are on step <span class="arithmatex">\(i\)</span> and the previous move climbed <span class="arithmatex">\(j\)</span> steps</strong>, where <span class="arithmatex">\(j \in \{1, 2\}\)</span>. This distinguishes whether the preceding move climbed <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(2\)</span> steps, allowing us to determine how the current state could have been reached.</p>
|
||||
@@ -2446,8 +2446,8 @@ 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/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><ha-svg-figure aria-label="Extending the dynamic programming state with the previous step size" 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-figure-accessible="" data-src="/assets/figure-svg/figures/526078e41bc5bc8620e36b18.json" role="img" style=";--figure-ratio:960.0/540.0"><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.svg" data-theme="light" inert="" tabindex="-1" type="image/svg+xml">Recurrence with the constraint</object><object aria-hidden="true" data="/en/chapter_dynamic_programming/dp_problem_features.assets/climbing_stairs_constraint_state_transfer.dark.svg" data-theme="dark" inert="" 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><div class="ha-sr-only figure-description"><h3>Description of Figure 14-9: Extending the dynamic programming state with the previous step size</h3><p>Recording whether the last move climbed 1 or 2 steps in the two-dimensional state dp[i, j] excludes consecutive 1-step moves during transitions.</p><p>Each move climbs 1 or 2 steps, with two consecutive 1-step moves forbidden. Because whether a 1-step move is currently allowed depends on the previous step size, extend the state to two-dimensional dp[i, j]: the number of ways to reach step i whose last move climbed j steps (for j = 1 or 2).</p><p>On the top step, the figure labels two target states: cyan dp[i, 1] and blue dp[i, 2]; on step i - 1, cyan dp[i - 1, 2] and gray dp[i - 1, 1]; and on step i - 2, blue dp[i - 2, 1] and blue dp[i - 2, 2]. A solid cyan arc spans 1 step from dp[i - 1, 2] to dp[i, 1], whereas a gray dotted arc from gray dp[i - 1, 1] to dp[i, 1] carries the note "Two consecutive one-step moves are forbidden, so this term is discarded"; from dp[i - 2, 1] and dp[i - 2, 2], two solid blue arcs each span 2 steps into dp[i, 2].</p><p>When the final move climbs 1 step from step i - 1, the move that reached step i - 1 must have climbed 2 steps, giving dp[i, 1] = dp[i - 1, 2]. When the final move climbs 2 steps from step i - 2, the move that reached step i - 2 could have climbed either 1 or 2 steps, giving dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2].</p><p>The figure draws only symbolic transitions without numeric initial values. The table below supplies the initial states at steps 1 and 2 from the chapter code, from which recurrence begins at step 3.</p><table><caption>Initial states in the chapter code (supplied base cases)</caption><thead><tr><th scope="col">Step i</th><th scope="col">dp[i,1]</th><th scope="col">dp[i,2]</th></tr></thead><tbody><tr><th scope="row">1</th><td>1</td><td>0</td></tr><tr><th scope="row">2</th><td>0</td><td>1</td></tr></tbody></table><p>Step 1 has only 0→1, so dp[1, 1] = 1 and dp[1, 2] = 0. For step 2, 0→1→2 takes two consecutive 1-step moves and is invalid; only 0→2 is valid, so dp[2, 1] = 0 and dp[2, 2] = 1.</p><p>Once the state includes the last move's step size, valid subsequent moves depend only on the current state, restoring the no-aftereffect property; the total number of ways to reach step n is dp[n, 1] + dp[n, 2].</p></div>
|
||||
<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>
|
||||
<div class="tabbed-content">
|
||||
@@ -2874,5 +2874,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/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/bff17b8259339758cb1c4a06.js"></script></body>
|
||||
<script defer="" src="/assets/figure-svg/js/00174ff861e190f5c1f09e34.js"></script><script defer="" src="/assets/figure-svg/js/e84f59519170c0f5ef238548.js"></script><script defer="" src="/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js"></script><script defer="" src="/assets/figure-svg/js/70ae82c6406c9bdbda8e8362.js"></script></body>
|
||||
</html>
|
||||
Binary file not shown.
Reference in New Issue
Block a user