<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Posts on WangScape</title>
        <link>https://w2343419-del.github.io/WangScape/en/post/</link>
        <description>Recent content in Posts on WangScape</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en</language>
        <lastBuildDate>Sun, 15 Mar 2026 22:06:00 +0800</lastBuildDate><atom:link href="https://w2343419-del.github.io/WangScape/en/post/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>High-precision calculations in C</title>
        <link>https://w2343419-del.github.io/WangScape/en/p/high-precision-calculations-in-c/</link>
        <pubDate>Sun, 15 Mar 2026 22:06:00 +0800</pubDate>
        
        <guid>https://w2343419-del.github.io/WangScape/en/p/high-precision-calculations-in-c/</guid>
        <description>&lt;p&gt;The maximum built-in integer &lt;code&gt;unsigned long long&lt;/code&gt; in C is about $1.8\ times 10 ^ {19} $, which will overflow once the number exceeds this range. The high-precision calculation uses an integer array to store each bit of the large number, and cooperates with the loop simulation vertical operation to break the limit of the number of bits.&lt;/p&gt;
&lt;h1 id=&#34;questions&#34;&gt;Questions
&lt;/h1&gt;&lt;h2 id=&#34;background&#34;&gt;Background
&lt;/h2&gt;&lt;p&gt;Standard shapes are often inadequate in competition and engineering, and typical scenarios include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Calculate $100! $ (~ 158 digits)&lt;/li&gt;
&lt;li&gt;Large power operations (e.g. RSA key generation)&lt;/li&gt;
&lt;li&gt;Financial calculations requiring precise results&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;core-issues&#34;&gt;Core issues
&lt;/h2&gt;&lt;p&gt;For two non-negative integers of arbitrary length, add, subtract, multiply, and divide four operations are implemented, and the results are accurate.&lt;/p&gt;
&lt;h2 id=&#34;binding-effect&#34;&gt;BINDING EFFECT
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Up to $10 ^ 3$ digits (adjustable &lt;code&gt;MAXN&lt;/code&gt; extension)&lt;/li&gt;
&lt;li&gt;This article only deals with non-negative integers; negative numbers require the addition of symbol bits&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;idea-analysis&#34;&gt;Idea Analysis
&lt;/h1&gt;&lt;h2 id=&#34;core-ideas&#34;&gt;Core Ideas
&lt;/h2&gt;&lt;p&gt;Save each digit of the large number * * in reverse order * * into the &lt;code&gt;int&#39; array: &lt;/code&gt;d [0]&lt;code&gt;for one digit,&lt;/code&gt;d [1]` for ten digits, and so on. The advantage of reverse order is that the carry direction (low → high) is consistent with the growth direction of the array subscript, and the loop is the most natural to write.&lt;/p&gt;
&lt;h2 id=&#34;data-structures&#34;&gt;Data Structures
&lt;/h2&gt;&lt;p&gt;__ code_block_0 __&lt;/p&gt;
&lt;p&gt;Layout of the number &lt;code&gt;12345&lt;/code&gt; in the array:&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;Subscript&lt;/th&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;d [0]&lt;/th&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;d [1]&lt;/th&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;d [2]&lt;/th&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;d [3]&lt;/th&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;d [4]&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;Value&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;5&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;4&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;3&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;2&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;1&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&#34;key-points-of-each-operation&#34;&gt;Key points of each operation
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Addition * *: add bit by bit, record the carry with the variable &lt;code&gt;carry&lt;/code&gt;, and loop until the highest carry is also processed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Subtraction * *: Subtract bit by bit, record the borrow with &lt;code&gt;borrow&lt;/code&gt;, and guarantee $ a\ geq b $ before calling.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Multiplication * *: double loop, the result of &lt;code&gt;a [i] * b [j]&lt;/code&gt; is accumulated to the &amp;lsquo;i + j&lt;code&gt;bit of the result, and finally the carry is processed uniformly. Intermediate results are spill-proof with&lt;/code&gt;long long`.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Divide by a small integer * *: starting from the highest position, maintain the remainder &lt;code&gt;r&lt;/code&gt;, &lt;code&gt;r = r * 10 + d [i]&lt;/code&gt; at each step, the quotient is&lt;code&gt;r/b&lt;/code&gt;, and the remainder is updated to&lt;code&gt;r % b&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&#34;code-implementation&#34;&gt;Code Implementation
&lt;/h1&gt;&lt;h2 id=&#34;initialization-and-io&#34;&gt;Initialization and I/O
&lt;/h2&gt;&lt;p&gt;__ code_block_1 __&lt;/p&gt;
&lt;h2 id=&#34;addition-and-subtraction&#34;&gt;Addition and Subtraction
&lt;/h2&gt;&lt;p&gt;__ code_block_2 __&lt;/p&gt;
&lt;h2 id=&#34;multiplication&#34;&gt;Multiplication
&lt;/h2&gt;&lt;p&gt;__ code_block_3 __&lt;/p&gt;
&lt;h2 id=&#34;divide-by-small-integer&#34;&gt;Divide by small integer
&lt;/h2&gt;&lt;p&gt;__ code_block_4 __&lt;/p&gt;
&lt;h2 id=&#34;full-demo-program&#34;&gt;Full Demo Program
&lt;/h2&gt;&lt;p&gt;__ code_block_5 __&lt;/p&gt;
&lt;h2 id=&#34;application-calculating-factors&#34;&gt;Application: Calculating Factors
&lt;/h2&gt;&lt;p&gt;__ code_block_6 __&lt;/p&gt;
&lt;h1 id=&#34;complexity-and-advantages-and-disadvantages&#34;&gt;Complexity and Advantages and Disadvantages
&lt;/h1&gt;&lt;h2 id=&#34;time-complexity&#34;&gt;Time complexity
&lt;/h2&gt;&lt;p&gt;Set large digits to $ n $:&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;Operation&lt;/th&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;This article implements&lt;/th&gt;
          &lt;th style=&#34;text-align: center&#34;&gt;Optimization caps&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;Add/Subtract&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;$ O (n) $&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;—&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;Multiplication&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;$ O (n ^ 2) $&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;$ O (n\ log n) $ (FFT)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;Divide by small integer&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;$ O (n) $&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;—&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;Factor $ n! $&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;$ O (n ^ 2\ cdot\ log n) $&lt;/td&gt;
          &lt;td style=&#34;text-align: center&#34;&gt;—&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id=&#34;spatial-complexity&#34;&gt;Spatial Complexity
&lt;/h2&gt;&lt;p&gt;$ O (n) $, which is the size of the &lt;code&gt;d [MAXN]&lt;/code&gt; array in the structure.&lt;/p&gt;
&lt;h2 id=&#34;pros&#34;&gt;Pros
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;The principle is intuitive, fully corresponds to the manual vertical, easy to understand and debug&lt;/li&gt;
&lt;li&gt;Pure C implementation without any external dependencies&lt;/li&gt;
&lt;li&gt;Addition and subtraction $ O (n) $, fully adequate for medium size ($\ leq 10 ^ 4$ bits)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;disadvantages&#34;&gt;disadvantages
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Multiplication $ O (n ^ 2) $, slower for very large numbers ($ &amp;gt; 10 ^ 5$ bits)&lt;/li&gt;
&lt;li&gt;Only 1 bit per lattice, the constant factor is too large; it can be changed to 10,000 (4 bits per lattice) to increase the speed by about 4 times&lt;/li&gt;
&lt;li&gt;Negative numbers are not supported for the time being, additional symbolic bit processing needs to be introduced&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;introduction-to-perpetual-optimization&#34;&gt;Introduction to Perpetual Optimization
&lt;/h2&gt;&lt;p&gt;Change &lt;code&gt;base&lt;/code&gt; from 10 to 10000 and store 4 decimal digits per array element:&lt;/p&gt;
&lt;p&gt;__ code_block_7 __&lt;/p&gt;
&lt;p&gt;The logic of addition, subtraction and multiplication is exactly the same, just change all &lt;code&gt;% 10&lt;/code&gt; to &lt;code&gt;% base&lt;/code&gt; and &lt;code&gt;/10&lt;/code&gt; to &lt;code&gt;/base&lt;/code&gt;.&lt;/p&gt;
</description>
        </item>
        <item>
        <title>Time and Space Complexity</title>
        <link>https://w2343419-del.github.io/WangScape/en/p/time-and-space-complexity/</link>
        <pubDate>Mon, 09 Mar 2026 10:32:00 +0800</pubDate>
        
        <guid>https://w2343419-del.github.io/WangScape/en/p/time-and-space-complexity/</guid>
        <description>&lt;h1 id=&#34;complete-guide-to-algorithmic-complexity---time-space-and-asymptotic-time-complexity&#34;&gt;Complete Guide to Algorithmic Complexity - Time, Space, and Asymptotic Time Complexity
&lt;/h1&gt;&lt;p&gt;Complexity analysis is a core tool for measuring algorithmic efficiency and helps us anticipate performance bottlenecks before we write code. This article will systematically explain the time complexity, space complexity, and the meaning and application of the three progressive symbols $ O $, $\ Omega $, and $\ Theta $, and explain them with a complete example.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;what-is-complexity&#34;&gt;What is Complexity
&lt;/h2&gt;&lt;p&gt;When we evaluate an algorithm, we can&amp;rsquo;t just look at whether it can get the right results, but also how it performs when * * the amount of data increases * *. Complexity is a mathematical tool used to describe &amp;ldquo;the changing trend of the resources required by the algorithm as it grows with the input size $ n $&amp;rdquo;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: how many steps does the algorithm take?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: How much additional memory does the algorithm take up?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both are expressed using * * progressive symbols * * - ignoring constant coefficients and focusing only on growth trends. The calculation rules are as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keep only the highest sub-item: $3n ^ 2 + 2n + 1\ Rightarrow O (n ^ 2) $&lt;/li&gt;
&lt;li&gt;Ignore constant coefficient: $5n\ Rightarrow O (n) $&lt;/li&gt;
&lt;li&gt;Loop nested multiplication: $ n $ runs on both levels $\ Rightarrow O (n ^ 2) $&lt;/li&gt;
&lt;li&gt;Max sequential structure: $ O (n) + O (n ^ 2)\ Rightarrow O (n ^ 2) $&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&#34;three-asymptotic-time-complexities&#34;&gt;Three Asymptotic Time Complexities
&lt;/h2&gt;&lt;p&gt;The same algorithm may behave very differently under different inputs. The three asymptotic time complexity describes the behavioral boundaries of the algorithm from three angles: * * upper bound, lower bound, and tight bound * *.&lt;/p&gt;
&lt;h3 id=&#34;big-o-symbol-upper-bound-worst-case&#34;&gt;Big O symbol (upper bound, worst case)
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Mathematical definition * *: there are constants $ c &amp;gt; 0$ and $ n_0$, when $ n\ geq n_0$, there is always:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;$ $ f (n)\ leq c\ cdot g (n) $ $&lt;/p&gt;
&lt;p&gt;The running time of the algorithm * * up to * * is a constant multiple of $ g (n) $, which is the * * cap commitment * * of the growth rate - guarantee not to be slower than this. The most widely used in everyday development, saying &amp;ldquo;this algorithm is $ O (n ^ 2) $&amp;rdquo; usually refers to the worst-case scenario.&lt;/p&gt;
&lt;p&gt;__ code_block_0 __&lt;/p&gt;
&lt;h3 id=&#34;large-ω-symbol-lower-bound-best-case&#34;&gt;Large Ω symbol (lower bound, best case)
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Mathematical definition * *: there are constants $ c &amp;gt; 0$ and $ n_0$, when $ n\ geq n_0$, there is always:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;$ $ f (n)\ geq c\ cdot g (n) $ $&lt;/p&gt;
&lt;p&gt;The running time of the algorithm * * at least * * is a constant multiple of $ g (n) $, which is the * * lower bound commitment * * of the growth rate - guaranteed not to be faster than this.&lt;/p&gt;
&lt;p&gt;__ code_block_1 __&lt;/p&gt;
&lt;p&gt;Classic conclusion: Any * * comparison-based sorting algorithm * *, the lower bound is $\ Omega (n\ log n) $, which is the mathematically provable limit and cannot be broken.&lt;/p&gt;
&lt;h3 id=&#34;large-θ-symbol-exact-bounds-precise-description&#34;&gt;Large θ symbol (exact bounds, precise description)
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Mathematical definition * *: there are constants $ c_1, c_2 &amp;gt; 0$ and $ n_0$, when $ n\ geq n_0$, there is always:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;$ $ c_1\ cdot g (n)\ leq f (n)\ leq c_2\ cdot g (n) $ $&lt;/p&gt;
&lt;p&gt;The algorithm is clamped by $ g (n) $ from the * * top and bottom sides at the same time * *, which is the most accurate description. $\ Theta $ holds if and only if $ O $ and $\ Omega $ hold together and have the same order.&lt;/p&gt;
&lt;p&gt;__ code_block_2 __&lt;/p&gt;
&lt;h3 id=&#34;comparison-of-the-three-symbols&#34;&gt;Comparison of the three symbols
&lt;/h3&gt;&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Symbols&lt;/th&gt;
          &lt;th&gt;Meaning&lt;/th&gt;
          &lt;th&gt;Intuitive Memory&lt;/th&gt;
          &lt;th&gt;Linear Find Example&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (g) $&lt;/td&gt;
          &lt;td&gt;Upper bound&lt;/td&gt;
          &lt;td&gt;Slowest not exceeding this speed&lt;/td&gt;
          &lt;td&gt;$ O (n) $, worst traversal all&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$\ Omega (g) $&lt;/td&gt;
          &lt;td&gt;Lower bound&lt;/td&gt;
          &lt;td&gt;Fastest not less than this speed&lt;/td&gt;
          &lt;td&gt;$\ Omega (1) $, best found first&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$\ Theta (g) $&lt;/td&gt;
          &lt;td&gt;Exact&lt;/td&gt;
          &lt;td&gt;This speed&lt;/td&gt;
          &lt;td&gt;Does not exist (upper and lower levels)&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;There is no $\ Theta $ for linear search, because it is better to be different from the worst case order, and the upper and lower bounds cannot be closed.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;time-complexity&#34;&gt;Time complexity
&lt;/h2&gt;&lt;p&gt;Time complexity description algorithm * * Number of steps executed * * Growth trend with input size $ n $.&lt;/p&gt;
&lt;h3 id=&#34;common-order-comparison&#34;&gt;Common Order Comparison
&lt;/h3&gt;&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Complexity&lt;/th&gt;
          &lt;th&gt;Name&lt;/th&gt;
          &lt;th&gt;Typical Scenario&lt;/th&gt;
          &lt;th&gt;$ n = magnitude at 10 ^ 6$&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (1) $&lt;/td&gt;
          &lt;td&gt;Constant time&lt;/td&gt;
          &lt;td&gt;Array access by subscript, hash table lookup&lt;/td&gt;
          &lt;td&gt;1 time&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (\ log n) $&lt;/td&gt;
          &lt;td&gt;Logarithmic time&lt;/td&gt;
          &lt;td&gt;Binary lookup, balanced binary tree operations&lt;/td&gt;
          &lt;td&gt;~ 20 times&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (n) $&lt;/td&gt;
          &lt;td&gt;Linear time&lt;/td&gt;
          &lt;td&gt;Traversal array, linear lookup&lt;/td&gt;
          &lt;td&gt;$10 ^ 6$ times&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (n\ log n) $&lt;/td&gt;
          &lt;td&gt;Linear logarithm&lt;/td&gt;
          &lt;td&gt;Merge sort, heap sort&lt;/td&gt;
          &lt;td&gt;~ $2\ times 10 ^ 7$ times&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (n ^ 2) $&lt;/td&gt;
          &lt;td&gt;square time&lt;/td&gt;
          &lt;td&gt;bubbling sort, select sort&lt;/td&gt;
          &lt;td&gt;$10 ^ {12} $ times ⚠️&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (2 ^ n) $&lt;/td&gt;
          &lt;td&gt;Exponential time&lt;/td&gt;
          &lt;td&gt;Violent recursive subset enumeration&lt;/td&gt;
          &lt;td&gt;Not acceptable 🚫&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Growth rate: $ O (1) &amp;lt; O (\ log n) &amp;lt; O (n) &amp;lt; O (n\ log n) &amp;lt; O (n ^ 2) &amp;lt; O (2 ^ n) $&lt;/p&gt;
&lt;h3 id=&#34;code-examples&#34;&gt;Code Examples
&lt;/h3&gt;&lt;p&gt;__ code_block_3 __&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;spatial-complexity&#34;&gt;Spatial Complexity
&lt;/h2&gt;&lt;p&gt;The spatial complexity describes the growth trend of the * * additional memory usage * * with the input scale (excluding the input data itself) when the algorithm is running.&lt;/p&gt;
&lt;h3 id=&#34;common-order-comparison-1&#34;&gt;Common Order Comparison
&lt;/h3&gt;&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Complexity&lt;/th&gt;
          &lt;th&gt;Meaning&lt;/th&gt;
          &lt;th&gt;Typical Scenario&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (1) $&lt;/td&gt;
          &lt;td&gt;Fixed space&lt;/td&gt;
          &lt;td&gt;Sort in place, with a few temporary variables&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (\ log n) $&lt;/td&gt;
          &lt;td&gt;Logarithmic space&lt;/td&gt;
          &lt;td&gt;Recursive call stack (binary, fast average)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (n) $&lt;/td&gt;
          &lt;td&gt;Linear space&lt;/td&gt;
          &lt;td&gt;Copy array, hash table, BFS queue&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;$ O (n ^ 2) $&lt;/td&gt;
          &lt;td&gt;Square Space&lt;/td&gt;
          &lt;td&gt;Create $ n\ times n $ Matrix, Adjacency Matrix&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&#34;code-examples-1&#34;&gt;Code Examples
&lt;/h3&gt;&lt;p&gt;__ code_block_4 __&lt;/p&gt;
&lt;p&gt;Each recursive function call allocates a stack frame on the call stack, and the * * recursive depth is the space complexity * *. Deep recursion can lead to stack overflow in extreme cases.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;integrated-sample-analysis-sum-of-two-numbers&#34;&gt;Integrated Sample Analysis: Sum of Two Numbers
&lt;/h2&gt;&lt;p&gt;Complete the analysis of the three progressive symbols and spatio-temporal complexity with a classical problem.&lt;/p&gt;
&lt;h2 id=&#34;questions&#34;&gt;Questions
&lt;/h2&gt;&lt;h3 id=&#34;description-of-problem&#34;&gt;Description of problem
&lt;/h3&gt;&lt;p&gt;Given the integer array &lt;code&gt;arr&lt;/code&gt; and the target value &lt;code&gt;target&lt;/code&gt;, find the subscript * * for the two numbers in the array * * and &lt;code&gt;target&lt;/code&gt;. There is only one answer per input and the same element cannot be used twice.&lt;/p&gt;
&lt;h3 id=&#34;inputoutput&#34;&gt;Input/Output
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;Input: &lt;code&gt;arr = [2, 7, 11, 15]&lt;/code&gt;, &lt;code&gt;target = 9&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Output: &lt;code&gt;[0, 1]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;binding-effect&#34;&gt;BINDING EFFECT
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;$2\ leq n\ leq 10 ^ 4$&lt;/li&gt;
&lt;li&gt;$ -10 ^ 9\ leq arr [i]\ leq10 ^ 9$&lt;/li&gt;
&lt;li&gt;Guaranteed and only one answer&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;idea-analysis&#34;&gt;Idea Analysis
&lt;/h2&gt;&lt;h3 id=&#34;solution-1-violent-enumeration&#34;&gt;Solution 1: Violent Enumeration
&lt;/h3&gt;&lt;p&gt;Enumerate all pairs of numbers $ (i, j) $ and check if &lt;code&gt;arr [i] + arr [j] = = target&lt;/code&gt; is met. Direct thinking, no extra space needed, but time inefficient.&lt;/p&gt;
&lt;h3 id=&#34;solution-2-hashtable-optimization&#34;&gt;Solution 2: Hashtable optimization
&lt;/h3&gt;&lt;p&gt;When an array is traversed, values that have already been seen are stored in the hash table. For each element, check if its * * complement * * (&lt;code&gt;target - arr [i]&lt;/code&gt;) is already in the table. Returns directly if hit, otherwise table the current element.&lt;/p&gt;
&lt;p&gt;This is typical * * space for time * *: $ O (n) $ extra space, reducing time from $ O (n ^ 2) $ to $ O (n) $.&lt;/p&gt;
&lt;h2 id=&#34;code-implementation&#34;&gt;Code Implementation
&lt;/h2&gt;&lt;h3 id=&#34;solution-1-violent-enumeration-1&#34;&gt;Solution 1: Violent Enumeration
&lt;/h3&gt;&lt;p&gt;__ code_block_5 __&lt;/p&gt;
&lt;h3 id=&#34;solution-2-hashtable&#34;&gt;Solution 2: Hashtable
&lt;/h3&gt;&lt;p&gt;__ code_block_6 __&lt;/p&gt;
&lt;h2 id=&#34;complexity-and-advantages-and-disadvantages&#34;&gt;Complexity and Advantages and Disadvantages
&lt;/h2&gt;&lt;h3 id=&#34;solution-1-violent-enumeration-2&#34;&gt;Solution 1: Violent Enumeration
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time: $ O (n ^ 2) $ (worst), $\ Omega (1) $ (best, hit first pair), none $\ Theta $&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space: $\ Theta (1) $&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;No extra space, memory friendly&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Simple implementation, no hash function required&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Poor time efficiency, $ n = 10 ^ 4$ has 100 million operations&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Not suitable for large scale data&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;solution-2-hashtable-1&#34;&gt;Solution 2: Hashtable
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Time: $\ Theta (n) $ (must be traversed once, hash lookup is $ O (1) $)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Space: $\ Theta (n) $ (maximum of $ n $ elements in hash table)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Time efficient, linear scan once&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Suitable for large scale data&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Extra $ O (n) $ memory required&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Hash collisions can degenerate in extreme cases&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;comparative-summary&#34;&gt;Comparative Summary
&lt;/h3&gt;&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;&lt;/th&gt;
          &lt;th&gt;Time (Worst)&lt;/th&gt;
          &lt;th&gt;Time (Best)&lt;/th&gt;
          &lt;th&gt;Time ($\ Theta $)&lt;/th&gt;
          &lt;th&gt;Space&lt;/th&gt;
          &lt;th&gt;Suggested Scenarios&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;Violent Enumeration&lt;/td&gt;
          &lt;td&gt;$ O (n ^ 2) $&lt;/td&gt;
          &lt;td&gt;$\ Omega (1) $&lt;/td&gt;
          &lt;td&gt;—&lt;/td&gt;
          &lt;td&gt;$ O (1) $&lt;/td&gt;
          &lt;td&gt;Extremely Limited Memory&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Hashtable&lt;/td&gt;
          &lt;td&gt;$ O (n) $&lt;/td&gt;
          &lt;td&gt;$\ Omega (n) $&lt;/td&gt;
          &lt;td&gt;$\ Theta (n) $&lt;/td&gt;
          &lt;td&gt;$ O (n) $&lt;/td&gt;
          &lt;td&gt;General Business Systems&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&#34;time-and-space-trade-offs&#34;&gt;Time and space trade-offs
&lt;/h2&gt;&lt;p&gt;In actual engineering, time and space are often * * not optimal * * at the same time, and trade-offs need to be made according to the scene.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Spaces for time * * (most common): hash tables, caches, dynamically planned memorized arrays.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Swap time for space * *: Read large files line by line while streaming, avoiding loading all data into memory at once.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Scenarios&lt;/th&gt;
          &lt;th&gt;Recommendation Strategies&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;Real-time response, high concurrency systems&lt;/td&gt;
          &lt;td&gt;Sacrificing space, optimizing time&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Embedded Devices, Memory-Constrained Environments&lt;/td&gt;
          &lt;td&gt;Sacrifice Time, Save Space&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;General business systems&lt;/td&gt;
          &lt;td&gt;Prioritize optimizing time, space is sufficient&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&#34;quick-check-of-common-algorithmic-complexity&#34;&gt;Quick Check of Common Algorithmic Complexity
&lt;/h2&gt;&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Algorithm&lt;/th&gt;
          &lt;th&gt;Time ($O$)&lt;/th&gt;
          &lt;th&gt;Time ($\Omega$)&lt;/th&gt;
          &lt;th&gt;Time ($\Theta$)&lt;/th&gt;
          &lt;th&gt;Space&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;Array access&lt;/td&gt;
          &lt;td&gt;$O(1)$&lt;/td&gt;
          &lt;td&gt;$\Omega(1)$&lt;/td&gt;
          &lt;td&gt;$\Theta(1)$&lt;/td&gt;
          &lt;td&gt;$O(1)$&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Linear search&lt;/td&gt;
          &lt;td&gt;$O(n)$&lt;/td&gt;
          &lt;td&gt;$\Omega(1)$&lt;/td&gt;
          &lt;td&gt;—&lt;/td&gt;
          &lt;td&gt;$O(1)$&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Binary search&lt;/td&gt;
          &lt;td&gt;$O(\log n)$&lt;/td&gt;
          &lt;td&gt;$\Omega(1)$&lt;/td&gt;
          &lt;td&gt;—&lt;/td&gt;
          &lt;td&gt;$O(1)$&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Bubble sort&lt;/td&gt;
          &lt;td&gt;$O(n^2)$&lt;/td&gt;
          &lt;td&gt;$\Omega(n)$&lt;/td&gt;
          &lt;td&gt;$\Theta(n^2)$&lt;/td&gt;
          &lt;td&gt;$O(1)$&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Merge sort&lt;/td&gt;
          &lt;td&gt;$O(n \log n)$&lt;/td&gt;
          &lt;td&gt;$\Omega(n \log n)$&lt;/td&gt;
          &lt;td&gt;$\Theta(n \log n)$&lt;/td&gt;
          &lt;td&gt;$O(n)$&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Quick sort&lt;/td&gt;
          &lt;td&gt;$O(n^2)$&lt;/td&gt;
          &lt;td&gt;$\Omega(n \log n)$&lt;/td&gt;
          &lt;td&gt;—&lt;/td&gt;
          &lt;td&gt;$O(\log n)$&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Hash table lookup&lt;/td&gt;
          &lt;td&gt;$O(n)$&lt;/td&gt;
          &lt;td&gt;$\Omega(1)$&lt;/td&gt;
          &lt;td&gt;—&lt;/td&gt;
          &lt;td&gt;$O(n)$&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;There is no $\ Theta $ for quick sorting and linear searching, because it is better to be different from the worst case order, and the upper and lower bounds cannot be closed.&lt;/p&gt;
&lt;hr&gt;
</description>
        </item>
        <item>
        <title>State Transition Equations and Dynamic Programming</title>
        <link>https://w2343419-del.github.io/WangScape/en/p/state-transition-equations-and-dynamic-programming/</link>
        <pubDate>Mon, 02 Mar 2026 13:57:00 +0800</pubDate>
        
        <guid>https://w2343419-del.github.io/WangScape/en/p/state-transition-equations-and-dynamic-programming/</guid>
        <description>&lt;p&gt;In algorithmic problems, we can often see the shadow of dynamic programming, so here is a summary of dynamic programming (DP) and a very important part of it - the state transition equation.&lt;/p&gt;
&lt;h2 id=&#34;i-what-is-dynamic-planning&#34;&gt;I. What is Dynamic Planning
&lt;/h2&gt;&lt;p&gt;Dynamic Programming (DP) is an algorithmic idea that solves the original problem by decomposing it into sub-problems.&lt;/p&gt;
&lt;p&gt;Dynamic planning is not some specific data structure, but a way of thinking.&lt;/p&gt;
&lt;p&gt;DP needs to meet the following two properties:&lt;/p&gt;
&lt;h3 id=&#34;1-optimal-substructure&#34;&gt;1. Optimal Substructure
&lt;/h3&gt;&lt;p&gt;The optimal solution of the original problem contains the optimal solution of the subproblem.&lt;/p&gt;
&lt;h3 id=&#34;2-overlapping-subquestions&#34;&gt;2. Overlapping subquestions
&lt;/h3&gt;&lt;p&gt;Subquestions are computed iteratively and can be cached to avoid duplication.&lt;/p&gt;
&lt;h2 id=&#34;ii-what-is-the-state-transition-equation&#34;&gt;II. What is the state transition equation?
&lt;/h2&gt;&lt;p&gt;To understand the state transition equation, you should first know what a &amp;ldquo;state&amp;rdquo; is.&lt;/p&gt;
&lt;h3 id=&#34;1--state&#34;&gt;1- STATE
&lt;/h3&gt;&lt;p&gt;Status is a description of the problem at a certain stage, usually denoted by &lt;code&gt;dp [i]&lt;/code&gt; or &lt;code&gt;dp [i] [j]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;dp [i]&lt;/code&gt; = optimal solution for the first i elements&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dp [i] [j]&lt;/code&gt; = optimal solution from position i to position j&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dp [i] [w]&lt;/code&gt; = optimal solution considering the first i items with remaining capacity w&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;2-state-transition-equation&#34;&gt;2. State transition equation
&lt;/h3&gt;&lt;p&gt;The state transition equation can be roughly written as:&lt;/p&gt;
&lt;p&gt;__ code_block_0 __&lt;/p&gt;
&lt;p&gt;What the state transition equation does is determine what options are available for that step, as well as the sub-problems behind the choices (which can be understood as recursive).&lt;/p&gt;
&lt;h2 id=&#34;iii-typical-examples&#34;&gt;III. Typical Examples
&lt;/h2&gt;&lt;h3 id=&#34;example-1-linear-dp---climbing-stairs&#34;&gt;Example 1: Linear DP - climbing stairs
&lt;/h3&gt;&lt;h4 id=&#34;questions&#34;&gt;Questions
&lt;/h4&gt;&lt;p&gt;How many ways can you climb 1 or 2 steps at a time to reach step n?&lt;/p&gt;
&lt;h4 id=&#34;idea-analysis&#34;&gt;Idea Analysis
&lt;/h4&gt;&lt;ol&gt;
&lt;li&gt;Define status: &lt;code&gt;dp [i]&lt;/code&gt; = Number of methods to climb to level i&lt;/li&gt;
&lt;li&gt;Last step analysis: to reach step i, you can only come from step i-1 (step 1) or step i-2 (step 2)&lt;/li&gt;
&lt;li&gt;Based on the analysis, we can obtain such a state transition equation:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;$ $ dp [i] = dp [i-1] + dp [i-2] $ $&lt;/p&gt;
&lt;p&gt;Note: There are two more boundary states in this state transition equation: &lt;code&gt;dp [1] = 1&lt;/code&gt;, &lt;code&gt;dp [2] = 2&lt;/code&gt;&lt;/p&gt;
&lt;ol start=&#34;4&#34;&gt;
&lt;li&gt;Illustration:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;__ code_block_1 __&lt;/p&gt;
&lt;h4 id=&#34;code-implementation&#34;&gt;Code Implementation
&lt;/h4&gt;&lt;p&gt;__ code_block_2 __&lt;/p&gt;
&lt;h4 id=&#34;time-complexity-and-advantages-and-disadvantages&#34;&gt;Time Complexity and Advantages and Disadvantages
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (n) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (n) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;The question is simple and easy to understand&lt;/li&gt;
&lt;li&gt;Status definition is intuitive&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Values can only be calculated in one fixed place and need to be recursed multiple times to calculate multiple times&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&#34;example-2-linear-dp---burglary&#34;&gt;Example 2: Linear DP - Burglary
&lt;/h3&gt;&lt;h4 id=&#34;questions-1&#34;&gt;Questions
&lt;/h4&gt;&lt;p&gt;A row of houses can not rob neighbors, ask for the maximum amount. The given array &lt;code&gt;nums&lt;/code&gt; represents the amounts for each house.&lt;/p&gt;
&lt;h4 id=&#34;idea-analysis-1&#34;&gt;Idea Analysis
&lt;/h4&gt;&lt;ol&gt;
&lt;li&gt;Define status: &lt;code&gt;dp [i]&lt;/code&gt; = the maximum amount you can get for grabbing room i&lt;/li&gt;
&lt;li&gt;Last step analysis: room i, either rob or not rob
&lt;ul&gt;
&lt;li&gt;Grab this room: get &lt;code&gt;nums [i] + dp [i-2]&lt;/code&gt; (up to i-2 in front)&lt;/li&gt;
&lt;li&gt;Don&amp;rsquo;t grab this room: get &lt;code&gt;dp [i-1]&lt;/code&gt; (grab the maximum between i-1)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;State transition equation:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;$ $ dp [i] =\ max (dp [i-1], dp [i-2] + nums [i]) $ $&lt;/p&gt;
&lt;ol start=&#34;4&#34;&gt;
&lt;li&gt;Illustration:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;__ code_block_3 __&lt;/p&gt;
&lt;h4 id=&#34;code-implementation-1&#34;&gt;Code Implementation
&lt;/h4&gt;&lt;p&gt;__ code_block_4 __&lt;/p&gt;
&lt;h4 id=&#34;time-complexity-and-advantages-and-disadvantages-1&#34;&gt;Time Complexity and Advantages and Disadvantages
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (n) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (n) $, optimized for $ O (1) $ (only the first two are retained)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;✅ Similar to climbing stairs, clear thinking&lt;/li&gt;
&lt;li&gt;✅ Optimizes space to O (1)&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;❌ No direct backtracking on which houses were robbed&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&#34;example-3-backpack-dp---01-backpack&#34;&gt;Example 3: Backpack DP - 0/1 Backpack
&lt;/h3&gt;&lt;h4 id=&#34;questions-2&#34;&gt;Questions
&lt;/h4&gt;&lt;p&gt;n items, weight &lt;code&gt;w []&lt;/code&gt;, value &lt;code&gt;v []&lt;/code&gt;, back capacity W, for maximum value.&lt;/p&gt;
&lt;h4 id=&#34;idea-analysis-2&#34;&gt;Idea Analysis
&lt;/h4&gt;&lt;ol&gt;
&lt;li&gt;Define status: &lt;code&gt;dp [i] [j]&lt;/code&gt; = maximum value considering the first i items, capacity j&lt;/li&gt;
&lt;li&gt;Last step analysis: the ith item, put or not put
&lt;ul&gt;
&lt;li&gt;Do not put: &lt;code&gt;dp [i] [j] = dp [i-1] [j]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Placement: &lt;code&gt;dp [i] [j] = dp [i-1] [j-w [i]] + v [i]&lt;/code&gt; for $ j\ geq w [i] $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;State transition equation:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;$ $ dp [i] [j] =\ max (dp [i-1] [j], dp [i-1] [j-w [i]] + v [i]) $ $&lt;/p&gt;
&lt;ol start=&#34;4&#34;&gt;
&lt;li&gt;Illustration:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Items: (w = 2, v = 3), (w = 3, v = 4), (w = 4, v = 5) Back carrying capacity W = 5&lt;/p&gt;
&lt;p&gt;__ code_block_5 __&lt;/p&gt;
&lt;h4 id=&#34;code-implementation-2&#34;&gt;Code Implementation
&lt;/h4&gt;&lt;p&gt;__ code_block_6 __&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Spatial optimization * *: The two-dimensional dp can be compressed into one dimension, and the inner loop * * must be reversed * * to prevent the same item from being repeatedly placed:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;__ code_block_7 __&lt;/p&gt;
&lt;h4 id=&#34;time-complexity-and-advantages-and-disadvantages-2&#34;&gt;Time Complexity and Advantages and Disadvantages
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (nW) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (nW) $, optimized to $ O (W) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;DP framework is classic and easy to expand&lt;/li&gt;
&lt;li&gt;Optimal values for all capacities can be solved at once&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;When the number or capacity of items is large, the pressure in time and space is high&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&#34;example-4-sequence-dp---longest-common-subsequence-lcs&#34;&gt;Example 4: Sequence DP - Longest Common Subsequence (LCS)
&lt;/h3&gt;&lt;h4 id=&#34;questions-3&#34;&gt;Questions
&lt;/h4&gt;&lt;p&gt;Two strings for the length of the longest common subsequence. Example: &lt;code&gt;&amp;quot;abcde&amp;quot;&lt;/code&gt; and &lt;code&gt;&amp;quot;ace&amp;quot;&lt;/code&gt; → length is 3 (&lt;code&gt;ace&lt;/code&gt;)&lt;/p&gt;
&lt;h4 id=&#34;idea-analysis-3&#34;&gt;Idea Analysis
&lt;/h4&gt;&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Define the status: &lt;code&gt;dp [i] [j]&lt;/code&gt; = LCS length of the first i characters of s1 and the first j characters of s2&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Last step analysis: Are s1 [i] and s2 [j] equal:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Equals: &lt;code&gt;dp [i] [j] = dp [i-1] [j-1] + 1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;unequal: &lt;code&gt;dp [i] [j] = max (dp [i-1] [j], dp [i] [j-1])&lt;/code&gt; (remove s1 [i] or s2 [j] to see which is better)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;State transition equation: &amp;lsquo;dp [i] [j] = dp [i-1] [j-1] + 1&lt;code&gt;when s1 [i-1] = = s2 [j-1]; otherwise&lt;/code&gt;dp [i] [j] = max (dp [i-1] [j], dp [i] [j-1])`&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Or as a piecewise function:&lt;/p&gt;
&lt;p&gt;$ $ dp [i] [j] =\ begin {cases}
\ text {dp [i-1] [j-1] + 1} &amp;amp;\ text {when equal}\
\ text {max (dp [i-1] [j], dp [i] [j-1])} &amp;amp;\ text {when not equal}
\ end {cases} $ $&lt;/p&gt;
&lt;ol start=&#34;4&#34;&gt;
&lt;li&gt;Illustration:&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;s1 = &amp;ldquo;abcde&amp;rdquo; s2 = &amp;ldquo;ace&amp;rdquo;&lt;/p&gt;
&lt;p&gt;__ code_block_8 __&lt;/p&gt;
&lt;h4 id=&#34;code-implementation-3&#34;&gt;Code Implementation
&lt;/h4&gt;&lt;p&gt;__ code_block_9 __&lt;/p&gt;
&lt;h4 id=&#34;time-complexity-and-advantages-and-disadvantages-3&#34;&gt;Time Complexity and Advantages and Disadvantages
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (mn) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (mn) $, optimized for $ O (\ min (m, n)) $ (scrolling array)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Frame for sequence alignment problems&lt;/li&gt;
&lt;li&gt;Scalable to LCS specific characters (backtracking)&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;When m and n are large, the spatial pressure is large&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3 id=&#34;example-5-interval-dp---poke-balloon&#34;&gt;Example 5: Interval DP - Poke Balloon
&lt;/h3&gt;&lt;h4 id=&#34;questions-4&#34;&gt;Questions
&lt;/h4&gt;&lt;p&gt;Poke the balloon i score = &lt;code&gt;nums [i-1] * nums [i] * nums [i +1]&lt;/code&gt; to get the maximum total score.&lt;/p&gt;
&lt;h4 id=&#34;idea-analysis-4&#34;&gt;Idea Analysis
&lt;/h4&gt;&lt;ol&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Key ideas * *: Do not want to &amp;ldquo;poke which first&amp;rdquo;, but &amp;ldquo;poke which last * * in the interval (i, j)&amp;rdquo;, so that the boundaries on both sides are known to avoid confusion&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Define status: &lt;code&gt;dp [i] [j]&lt;/code&gt; = Maximum score for all balloons in punctured open interval (i, j)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Based on the analysis, we can obtain such a state transition equation:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;$ $ dp [i] [j] =\ max_{i &amp;lt; k &amp;lt; j} (dp [i] [k] + dp [k] [j] + nums [i]\ nums [k]\ times nums [j]) $ $&lt;/p&gt;
&lt;p&gt;where k is the last poked balloon in interval (i, j).&lt;/p&gt;
&lt;h4 id=&#34;code-implementation-4&#34;&gt;Code Implementation
&lt;/h4&gt;&lt;p&gt;__ code_block_10 __&lt;/p&gt;
&lt;h4 id=&#34;time-complexity-and-advantages-and-disadvantages-4&#34;&gt;Time Complexity and Advantages and Disadvantages
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (n ^ 3) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (n ^ 2) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Classic example of an interval DP&lt;/li&gt;
&lt;li&gt;The idea of the &amp;ldquo;last one&amp;rdquo; is very enlightening&lt;/li&gt;
&lt;li&gt;The order in which specific stamps can be obtained by backtracking&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;The idea is relatively complex and confusing to the first contact&lt;/li&gt;
&lt;li&gt;Time complexity is cubic&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&#34;iv-dp-model-summary&#34;&gt;IV. DP Model Summary
&lt;/h2&gt;&lt;p&gt;DP Model Comparison Summary:&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Type&lt;/th&gt;
          &lt;th&gt;State Transition Equation&lt;/th&gt;
          &lt;th&gt;Time Complexity&lt;/th&gt;
          &lt;th&gt;Space Complexity&lt;/th&gt;
          &lt;th&gt;Represents Problem&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;* * Linear DP * *&lt;/td&gt;
          &lt;td&gt;Recursive Relationship&lt;/td&gt;
          &lt;td&gt;$ O (n) $&lt;/td&gt;
          &lt;td&gt;$ O (n) $&lt;/td&gt;
          &lt;td&gt;Climbing Stairs, Burglary&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;* * Backpack DP * *&lt;/td&gt;
          &lt;td&gt;Choose max&lt;/td&gt;
          &lt;td&gt;$ O (nW) $&lt;/td&gt;
          &lt;td&gt;$ O (nW) $&lt;/td&gt;
          &lt;td&gt;0/1 Backpack, full backpack&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;* * Sequence DP * *&lt;/td&gt;
          &lt;td&gt;Two Pointer Recursion&lt;/td&gt;
          &lt;td&gt;$ O (mn) $&lt;/td&gt;
          &lt;td&gt;$ O (mn) $&lt;/td&gt;
          &lt;td&gt;LCS, Edit Distance&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;* * Interval DP * *&lt;/td&gt;
          &lt;td&gt;Interval Split Recursion&lt;/td&gt;
          &lt;td&gt;$ O (n ^ 3) $&lt;/td&gt;
          &lt;td&gt;$ O (n ^ 2) $&lt;/td&gt;
          &lt;td&gt;Balloon Poke, Matrix Chain Multiplication&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id=&#34;summary--suggestions&#34;&gt;Summary &amp;amp; Suggestions
&lt;/h2&gt;&lt;ol&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;From the question * *: Determine if DP can be used (with optimal sub-structure and overlapping sub-problems)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Define status * *: clearly define what &lt;code&gt;dp [...]&lt;/code&gt; means&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Write out the transfer equation * *: Determine the relationship between the states&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Determination of initial conditions * *: treatment of boundary situations&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Implementation and optimization * *: Code implementation, then consider space + time optimization&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
</description>
        </item>
        <item>
        <title>P1004 [NOIP 2000 improvement group] grid score analysis and summary</title>
        <link>https://w2343419-del.github.io/WangScape/en/p/p1004-noip-2000-improvement-group-grid-score-analysis-and-summary/</link>
        <pubDate>Sat, 28 Feb 2026 11:31:00 +0800</pubDate>
        
        <guid>https://w2343419-del.github.io/WangScape/en/p/p1004-noip-2000-improvement-group-grid-score-analysis-and-summary/</guid>
        <description>&lt;p&gt;This is a classic but difficult chessboard problem. Although it is the topic of NOIP in 2000, it is still quite difficult for first-time contacts as the final topic. This paper summarizes three different solutions: Dynamic Planning, DFS Memorization, and Minimum Cost Maximum Flow, which unfolds gradually from easy to difficult.&lt;/p&gt;
&lt;h2 id=&#34;questions&#34;&gt;Questions
&lt;/h2&gt;&lt;h3 id=&#34;题目来源&#34;&gt;题目来源
&lt;/h3&gt;&lt;p&gt;NOIP 2000 提高组 T4&lt;/p&gt;
&lt;h3 id=&#34;description-of-problem&#34;&gt;Description of problem
&lt;/h3&gt;&lt;p&gt;There is a square diagram of N × N (N ≤ 9), some of which we fill in positive integers, while others put the number 0.
Someone starts at point A (0, 0) in the top left corner of the diagram and can walk down or to the right until they reach point B (N, N) in the bottom right corner.On the way he walks, he can take the number in the square (which becomes the number 0).
This person walked from point A to point B twice, trying to find 2 such paths, so that the sum of the obtained numbers is the maximum.&lt;/p&gt;
&lt;h3 id=&#34;inputoutput&#34;&gt;Input/Output
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Input format * *: The first line of input is an integer N (representing the grid diagram of N × N), and each subsequent line has three integers, the first two represent the position, and the third number is the number placed on the position. A separate line of 0 indicates the end of the input.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Output format * *: Just output an integer representing the maximum sum obtained on the 2 paths.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;example&#34;&gt;Example
&lt;/h3&gt;&lt;p&gt;Input:
__ code_block_0 __&lt;/p&gt;
&lt;p&gt;Output:
__ code_block_1 __&lt;/p&gt;
&lt;h3 id=&#34;约束条件&#34;&gt;约束条件
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;数据范围：1≤N≤9&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;problem-analysis&#34;&gt;Problem analysis
&lt;/h3&gt;&lt;p&gt;Why can&amp;rsquo;t I enumerate twice (i.e. find an optimal path first, then find the second one in the remaining cells)? Because one path changes the map (takes the number) and affects the result of the second, the two paths must be considered in conjunction and cannot be optimized independently.&lt;/p&gt;
&lt;h2 id=&#34;idea-analysis&#34;&gt;Idea Analysis
&lt;/h2&gt;&lt;p&gt;The ideas for the three solutions are as follows:&lt;/p&gt;
&lt;h3 id=&#34;solution-1-dynamic-planning&#34;&gt;Solution 1: Dynamic planning
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Core idea * *: Advance both paths simultaneously, simulating two people walking at the same time with one DP.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;State design * *: set &lt;code&gt;dp [k] [x1] [x2]&lt;/code&gt;, where:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;k is the number of steps currently taken (i.e. the value of x + y, from 2 to 2N)&lt;/li&gt;
&lt;li&gt;x1 and x2 are the line numbers where the two people are currently located&lt;/li&gt;
&lt;li&gt;y = k - x can be derived from k and x (key optimization, this step reduces the dimension), so column numbers do not need to be stored separately&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Deduplication * *: When two people are in the same cell (x1 = = x2, y1 = = y2), the cell is taken only once.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;State transition * *: Two people each can choose to move to the right or down for a total of 4 combinations. Each state dp [k] [x1] [x2] represents the sum of the maximum values at line x1 and line x2 for human 1 and 2, respectively, going to step k.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;solution-2-dfs--memory-search&#34;&gt;Solution 2: DFS + Memory Search
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Core idea * *: Similar to the DP algorithm (deep search and DP are essentially one thing after all), but adds memorized search to avoid double counting. If there is no memorized search, the amount of calculation will be an exponential explosion, about $4 ^ {16} $ times at N = 9.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Implementation * *: Starting from the initial state, all possible transitions are attempted recursively, while the calculated state is cached with the memo array, avoiding duplication. Returns 0 when the end point is reached and returns the maximum value layer by layer.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;solution-3-cost-flow-minimum-cost-maximum-flow&#34;&gt;Solution 3: Cost flow (minimum cost maximum flow)
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Core Idea * *: Convert &amp;ldquo;maximize two paths&amp;rdquo; into a network flow problem.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Modeling ideas * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Two paths from A to B = 2 flows from the source to the sink&lt;/li&gt;
&lt;li&gt;Fetch up to once per cell = Capacity limit per node&lt;/li&gt;
&lt;li&gt;Get number max = cost max (min to min)&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Split point processing * *: Each grid (i, j) is split into two nodes in and out:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;in → out capacity 1, cost - map [i] [j] (first path taken)&lt;/li&gt;
&lt;li&gt;in → out plus capacity 1, cost 0 (second path through but not counted)&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Connecting edges * *: (i, j) out connects (i +1, j) in and (i, j +1) in, capacity 2, cost 0.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;code-implementation&#34;&gt;Code Implementation
&lt;/h2&gt;&lt;h3 id=&#34;solution-1-dynamic-planning-1&#34;&gt;Solution 1: Dynamic planning
&lt;/h3&gt;&lt;p&gt;Full Code
__ code_block_2 __&lt;/p&gt;
&lt;h3 id=&#34;solution-2-dfs--memory-search-1&#34;&gt;Solution 2: DFS + Memory Search
&lt;/h3&gt;&lt;p&gt;__ code_block_3 __&lt;/p&gt;
&lt;h3 id=&#34;solution-3-cost-flow-minimum-cost-maximum-flow-1&#34;&gt;Solution 3: Cost flow (minimum cost maximum flow)
&lt;/h3&gt;&lt;p&gt;__ code_block_4 __&lt;/p&gt;
&lt;h2 id=&#34;time-complexity-and-advantages-and-disadvantages&#34;&gt;Time Complexity and Advantages and Disadvantages
&lt;/h2&gt;&lt;h3 id=&#34;solution-1-dynamic-planning-2&#34;&gt;Solution 1: Dynamic planning
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (N ^ 3) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Number of states: $ O (N ^ 2)\ times O (N ^ 2)/2 = O (N ^ 4) $, but due to the constraints of dimensionality reduction and x1 and x2, it is actually $ O (N ^ 3) $&lt;/li&gt;
&lt;li&gt;4 transitions per state&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (N ^ 3) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;dp array size is $2N\ times N\ times N $&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Clear thinking and easy to understand&lt;/li&gt;
&lt;li&gt;Problem solving in one iteration&lt;/li&gt;
&lt;li&gt;The code is relatively simple&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Large footprint (approx. 1.3MB at N = 9)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;solution-2-dfs--memory-search-2&#34;&gt;Solution 2: DFS + Memory Search
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (N ^ 3) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Same number of statuses as DP&lt;/li&gt;
&lt;li&gt;Up to one calculation per state (memorization)&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (N ^ 3) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Memo and visited arrays account for $ O (N ^ 3) $ each&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Logical and natural, thinking from top to bottom&lt;/li&gt;
&lt;li&gt;Easy to add pruning (although there are not many pruning in this question)&lt;/li&gt;
&lt;li&gt;Flexible state definition&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;The recursive call stack depth is $ O (N) $ (stack space)&lt;/li&gt;
&lt;li&gt;Same space complexity as DP&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;solution-3-expense-flow&#34;&gt;Solution 3: Expense Flow
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Time complexity * *: $ O (Flow\ times SPFA) = O (2\ times E\ log V) = O (N ^ 2\ times N ^ 2) = O (N ^ 4) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Flow is 2&lt;/li&gt;
&lt;li&gt;SPFA $ O (V\ log V) $ each time, where $ V = O (N ^ 2) $, $ E = O (N ^ 2) $&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Space complexity * *: $ O (V + E) = O (N ^ 2) $&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Storage space for diagrams&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Benefits * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Suitable for more general scenarios (multiple paths, restricted diagrams, etc.)&lt;/li&gt;
&lt;li&gt;Code frameworks can be reused for other expense stream issues&lt;/li&gt;
&lt;li&gt;Relatively sparsely occupied space&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Cons * *:&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;Highest time complexity (about $ O (N ^ 4) $ vs $ O (N ^ 3) $)&lt;/li&gt;
&lt;li&gt;The code is long, complex and error-prone&lt;/li&gt;
&lt;li&gt;Difficulty has skyrocketed beyond the limits of the competition&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;comparison-and-summary&#34;&gt;Comparison and Summary
&lt;/h3&gt;&lt;table&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Features&lt;/th&gt;
          &lt;th&gt;Solution 1 DP&lt;/th&gt;
          &lt;th&gt;Solution 2 DFS&lt;/th&gt;
          &lt;th&gt;Solution 3 Cost Flow&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      &lt;tr&gt;
          &lt;td&gt;Ease of Understanding&lt;/td&gt;
          &lt;td&gt;★★★★☆&lt;/td&gt;
          &lt;td&gt;★★★★☆&lt;/td&gt;
          &lt;td&gt;★☆☆☆☆&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Difficulty&lt;/td&gt;
          &lt;td&gt;★★☆☆☆&lt;/td&gt;
          &lt;td&gt;★★☆☆☆&lt;/td&gt;
          &lt;td&gt;★★★★★&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Time Complexity&lt;/td&gt;
          &lt;td&gt;$ O (N ^ 3) $&lt;/td&gt;
          &lt;td&gt;$ O (N ^ 3) $&lt;/td&gt;
          &lt;td&gt;$ O (N ^ 4) $&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Space Complexity&lt;/td&gt;
          &lt;td&gt;$ O (N ^ 3) $&lt;/td&gt;
          &lt;td&gt;$ O (N ^ 3) $&lt;/td&gt;
          &lt;td&gt;$ O (N ^ 2) $&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Recommendation Index&lt;/td&gt;
          &lt;td&gt;★★★★★&lt;/td&gt;
          &lt;td&gt;★★★★☆&lt;/td&gt;
          &lt;td&gt;★★☆☆☆&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;ul&gt;
&lt;li&gt;Conclusion * *: For this question, * * Solution 1 (DP) * * is the best choice, which is clear, efficient and not overly complicated. Solution 2 is for students who want to practice DFS. Solution 3 Although elegant, it is not as efficient as DP for the scale of N ≤ 9, and only extends knowledge.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
</description>
        </item>
        <item>
        <title>Alpine flowing water, searching for sound</title>
        <link>https://w2343419-del.github.io/WangScape/en/p/alpine-flowing-water-searching-for-sound/</link>
        <pubDate>Tue, 03 Feb 2026 13:28:00 +0800</pubDate>
        
        <guid>https://w2343419-del.github.io/WangScape/en/p/alpine-flowing-water-searching-for-sound/</guid>
        <description>&lt;p&gt;After nearly a day of half-day self-doubt, half-day struggle with AI (doge) and all-day hardness ratio with head and table&amp;hellip;&lt;/p&gt;
&lt;p&gt;January 21, 2026 finally became an extraordinary day for me.&lt;/p&gt;
&lt;p&gt;My personal blog is finally live!!!&lt;/p&gt;
&lt;h2 id=&#34;blog-goals&#34;&gt;Blog Goals
&lt;/h2&gt;&lt;p&gt;I will record here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Summary of difficulties, gains and knowledge points encountered in programming
-Learning experience and algorithmic analysis&lt;/li&gt;
&lt;li&gt;Occasional complaints and book reviews&lt;/li&gt;
&lt;li&gt;Favorite poems and other cultural content&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Maybe this blog will be my all-disciplinary notebook? (at least during university)&lt;/p&gt;
&lt;p&gt;In addition to code, it may also involve artificial intelligence, games, music, movies and other fields. I do my best to keep my blog focused on learning.&lt;/p&gt;
&lt;h2 id=&#34;final-words&#34;&gt;Final Words
&lt;/h2&gt;&lt;p&gt;Although I don&amp;rsquo;t know if I can always maintain this blog (a bit lazy after all), I will do my best.&lt;/p&gt;
&lt;p&gt;I hope my update frequency is not too low&amp;hellip;&lt;/p&gt;
&lt;p&gt;(ps. Blogs are rough now, but better later!)&lt;/p&gt;
&lt;hr&gt;
&lt;ul&gt;
&lt;li&gt;Edited February 3, 2026 *&lt;/li&gt;
&lt;/ul&gt;
</description>
        </item>
        
    </channel>
</rss>
