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"/>
@@ -1955,7 +1955,7 @@
<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. How many ways are there to reach the top?</p>
</div>
<p>As shown in Figure 14-1, a staircase with <span class="arithmatex">\(3\)</span> steps has <span class="arithmatex">\(3\)</span> possible ways to reach the top.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.png" data-dark-src="/assets/figure-svg/figures/f7e5b3a99d38517d6f922e1a.json" data-src="/assets/figure-svg/figures/943016f73acfb686cb138870.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.png" data-dark-src="/assets/figure-svg/figures/d0c3d4772f47d8869fd244ea.json" data-src="/assets/figure-svg/figures/4d0f7721647a1c2821cc4b92.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_example.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Number of ways to reach step 3</object></ha-svg-figure></p>
<p align="center"> Figure 14-1   Number of ways to reach step 3 </p>
<p>The goal is to count solutions, so <strong>we can use backtracking to enumerate every possibility</strong>. View climbing as a sequence of choices: starting from the ground, choose to climb <span class="arithmatex">\(1\)</span> or <span class="arithmatex">\(2\)</span> steps each time. Add <span class="arithmatex">\(1\)</span> to the count whenever we reach the top, and prune any branch that goes beyond it. The code is as follows:</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:13"><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"/><input id="__tabbed_1_12" name="__tabbed_1" type="radio"/><input id="__tabbed_1_13" name="__tabbed_1" type="radio"/><div class="tabbed-labels"><label for="__tabbed_1_1">Python</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Java</label><label for="__tabbed_1_4">C#</label><label for="__tabbed_1_5">Go</label><label for="__tabbed_1_6">Swift</label><label for="__tabbed_1_7">JS</label><label for="__tabbed_1_8">TS</label><label for="__tabbed_1_9">Dart</label><label for="__tabbed_1_10">Rust</label><label for="__tabbed_1_11">C</label><label for="__tabbed_1_12">Kotlin</label><label for="__tabbed_1_13">Ruby</label></div>
@@ -2353,7 +2353,7 @@ dp[i-1], dp[i-2], \dots, dp[2], dp[1]
dp[i] = dp[i-1] + dp[i-2]
\]</div>
<p>Thus, the subproblems are linked by a recurrence: <strong>the solution to the original problem can be constructed from subproblem solutions</strong>. Figure 14-2 illustrates this relationship.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.png" data-dark-src="/assets/figure-svg/figures/0fe61808afb6de4ba0813441.json" data-src="/assets/figure-svg/figures/291fcfd8c6a1dc407dce2594.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recurrence for the number of ways</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recurrence for the number of ways</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.png" data-dark-src="/assets/figure-svg/figures/5002ac641e808bab77966425.json" data-src="/assets/figure-svg/figures/8715a040b0aed7825a3a871c.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recurrence for the number of ways</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_state_transfer.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recurrence for the number of ways</object></ha-svg-figure></p>
<p align="center"> Figure 14-2   Recurrence for the number of ways </p>
<p>The recurrence gives a brute-force search solution. Starting at <span class="arithmatex">\(dp[n]\)</span>, <strong>recursively express a larger problem as the sum of two smaller ones</strong> until reaching <span class="arithmatex">\(dp[1]\)</span> or <span class="arithmatex">\(dp[2]\)</span>. Their solutions are known: <span class="arithmatex">\(dp[1] = 1\)</span> and <span class="arithmatex">\(dp[2] = 2\)</span>, meaning steps <span class="arithmatex">\(1\)</span> and <span class="arithmatex">\(2\)</span> can be reached in <span class="arithmatex">\(1\)</span> and <span class="arithmatex">\(2\)</span> ways, respectively.</p>
<p>Like the standard backtracking code, the following code performs depth-first search, but is more concise:</p>
@@ -2583,7 +2583,7 @@ dp[i] = dp[i-1] + dp[i-2]
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Search%22%22%22%0A%20%20%20%20%23%20The%20values%20of%20dp%5B1%5D%20and%20dp%5B2%5D%20are%20known%3B%20return%20the%20corresponding%20value%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201%29%20%2B%20dfs%28i%20-%202%29%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Climbing%20stairs%3A%20search%22%22%22%0A%20%20%20%20return%20dfs%28n%29%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%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs%28n%29%0A%20%20%20%20print%28f%22Number%20of%20ways%20to%20climb%20%7Bn%7D%20stairs%3A%20%7Bres%7D%22%29&amp;codeDivHeight=800&amp;codeDivWidth=600&amp;cumulative=false&amp;curInstr=5&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-3 shows the recursion tree for brute-force search. For <span class="arithmatex">\(dp[n]\)</span>, its depth is <span class="arithmatex">\(n\)</span>, and the time complexity is <span class="arithmatex">\(O(2^n)\)</span>. Exponential growth is explosive, so a relatively large <span class="arithmatex">\(n\)</span> leads to a very long wait.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.png" data-dark-src="/assets/figure-svg/figures/68ea837602dd2aa9e883dafd.json" data-src="/assets/figure-svg/figures/c4978ed3c6fdda68e2b530e7.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recursion tree for climbing stairs</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recursion tree for climbing stairs</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.png" data-dark-src="/assets/figure-svg/figures/6cdd7dadd8db29e08bfb8ba2.json" data-src="/assets/figure-svg/figures/851c562e8064b749fd96f3cb.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recursion tree for climbing stairs</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recursion tree for climbing stairs</object></ha-svg-figure></p>
<p align="center"> Figure 14-3   Recursion tree for climbing stairs </p>
<p>As Figure 14-3 shows, <strong>overlapping subproblems cause the exponential time complexity</strong>. For example, <span class="arithmatex">\(dp[9]\)</span> splits into <span class="arithmatex">\(dp[8]\)</span> and <span class="arithmatex">\(dp[7]\)</span>, while <span class="arithmatex">\(dp[8]\)</span> splits into <span class="arithmatex">\(dp[7]\)</span> and <span class="arithmatex">\(dp[6]\)</span>. Both contain <span class="arithmatex">\(dp[7]\)</span>.</p>
<p>These subproblems in turn contain smaller overlapping subproblems, and the pattern repeats at deeper levels of the recursion tree. Most of the computation is wasted on solving the same subproblems repeatedly.</p>
@@ -2921,7 +2921,7 @@ dp[i] = dp[i-1] + dp[i-2]
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20dfs%28i%3A%20int,%20mem%3A%20list%5Bint%5D%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Memoized%20search%22%22%22%0A%20%20%20%20%23%20The%20values%20of%20dp%5B1%5D%20and%20dp%5B2%5D%20are%20known%3B%20return%20the%20corresponding%20value%0A%20%20%20%20if%20i%20%3D%3D%201%20or%20i%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20return%20i%0A%20%20%20%20%23%20If%20dp%5Bi%5D%20has%20been%20recorded,%20return%20it%20directly%0A%20%20%20%20if%20mem%5Bi%5D%20%21%3D%20-1%3A%0A%20%20%20%20%20%20%20%20return%20mem%5Bi%5D%0A%20%20%20%20%23%20dp%5Bi%5D%20%3D%20dp%5Bi-1%5D%20%2B%20dp%5Bi-2%5D%0A%20%20%20%20count%20%3D%20dfs%28i%20-%201,%20mem%29%20%2B%20dfs%28i%20-%202,%20mem%29%0A%20%20%20%20%23%20Record%20dp%5Bi%5D%0A%20%20%20%20mem%5Bi%5D%20%3D%20count%0A%20%20%20%20return%20count%0A%0A%0Adef%20climbing_stairs_dfs_mem%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Climbing%20stairs%3A%20memoized%20search%22%22%22%0A%20%20%20%20%23%20mem%5Bi%5D%20stores%20the%20number%20of%20ways%20to%20reach%20step%20i%3B%20-1%20means%20no%20result%20has%20been%20recorded%0A%20%20%20%20mem%20%3D%20%5B-1%5D%20%2A%20%28n%20%2B%201%29%0A%20%20%20%20return%20dfs%28n,%20mem%29%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%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dfs_mem%28n%29%0A%20%20%20%20print%28f%22Number%20of%20ways%20to%20climb%20%7Bn%7D%20stairs%3A%20%7Bres%7D%22%29&amp;codeDivHeight=800&amp;codeDivWidth=600&amp;cumulative=false&amp;curInstr=5&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>As shown in Figure 14-4, <strong>memoization makes every overlapping subproblem require only one computation, reducing the time complexity to <span class="arithmatex">\(O(n)\)</span></strong>: a major improvement.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.png" data-dark-src="/assets/figure-svg/figures/b7144b14ba2304d44ec6011d.json" data-src="/assets/figure-svg/figures/38fce2f470d659f38cec0b8c.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recursion tree for memoized search</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recursion tree for memoized search</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.png" data-dark-src="/assets/figure-svg/figures/81a693c2f81780f53930803f.json" data-src="/assets/figure-svg/figures/33806f0c0d0e2db5f5638556.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Recursion tree for memoized search</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dfs_memo_tree.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Recursion tree for memoized search</object></ha-svg-figure></p>
<p align="center"> Figure 14-4   Recursion tree for memoized search </p>
<h2 id="1413-method-3-dynamic-programming">14.1.3   Method 3: Dynamic programming<a class="headerlink" href="#1413-method-3-dynamic-programming" title="Permanent link"></a></h2>
<p><strong>Memoized search is a top-down approach</strong>: start with the original problem (the root), recursively divide larger subproblems into smaller ones until reaching the smallest subproblems with known solutions (the leaves), then collect their solutions as recursion returns to construct the solution to the original problem.</p>
@@ -3168,7 +3168,7 @@ dp[i] = dp[i-1] + dp[i-2]
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=def%20climbing_stairs_dp%28n%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Climbing%20stairs%3A%20dynamic%20programming%22%22%22%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%20n%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%201,%202%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%20dp%5Bi%20-%201%5D%20%2B%20dp%5Bi%20-%202%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%20n%20%3D%209%0A%0A%20%20%20%20res%20%3D%20climbing_stairs_dp%28n%29%0A%20%20%20%20print%28f%22Number%20of%20ways%20to%20climb%20%7Bn%7D%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-5 traces the code's execution.</p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.png" data-dark-src="/assets/figure-svg/figures/a86e8ac5374488c7a0ceefa1.json" data-src="/assets/figure-svg/figures/82ebe1415c76c440594fc498.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Dynamic programming for climbing stairs</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Dynamic programming for climbing stairs</object></ha-svg-figure></p>
<p><ha-svg-figure class="animation-figure" data-book-key="en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.png" data-dark-src="/assets/figure-svg/figures/89137b1726045f60e0f93b89.json" data-src="/assets/figure-svg/figures/cc29e45fec0f9c177ae1b1c3.json" style=";--figure-ratio:960.0/540.0"><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.svg" data-theme="light" tabindex="-1" type="image/svg+xml">Dynamic programming for climbing stairs</object><object data="/en/chapter_dynamic_programming/intro_to_dynamic_programming.assets/climbing_stairs_dp.dark.svg" data-theme="dark" tabindex="-1" type="image/svg+xml">Dynamic programming for climbing stairs</object></ha-svg-figure></p>
<p align="center"> Figure 14-5   Dynamic programming for climbing stairs </p>
<p>Like backtracking, dynamic programming uses states to represent stages of solving a problem. Each state corresponds to a subproblem and its locally optimal solution. For climbing stairs, the state is the current step number <span class="arithmatex">\(i\)</span>.</p>
<p>We can now summarize some common dynamic programming terminology.</p>
@@ -3499,5 +3499,5 @@ dp[i] = dp[i-1] + dp[i-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>