mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-18 06:20:58 +00:00
deploy
This commit is contained in:
@@ -4700,7 +4700,7 @@
|
||||
</ul>
|
||||
<p>In other words, each round of decision (edit operation) we make on string <span class="arithmatex">\(s\)</span> will change the remaining characters to be matched in <span class="arithmatex">\(s\)</span> and <span class="arithmatex">\(t\)</span>. Therefore, the state is the <span class="arithmatex">\(i\)</span>-th and <span class="arithmatex">\(j\)</span>-th characters currently being considered in <span class="arithmatex">\(s\)</span> and <span class="arithmatex">\(t\)</span>, denoted as <span class="arithmatex">\([i, j]\)</span>.</p>
|
||||
<p>State <span class="arithmatex">\([i, j]\)</span> corresponds to the subproblem: <strong>the minimum number of edits required to change the first <span class="arithmatex">\(i\)</span> characters of <span class="arithmatex">\(s\)</span> into the first <span class="arithmatex">\(j\)</span> characters of <span class="arithmatex">\(t\)</span></strong>.</p>
|
||||
<p>From this, we obtain a two-dimensional <span class="arithmatex">\(dp\)</span> table of size <span class="arithmatex">\((i+1) \times (j+1)\)</span>.</p>
|
||||
<p>From this, we obtain a two-dimensional <span class="arithmatex">\(dp\)</span> table of size <span class="arithmatex">\((n+1) \times (m+1)\)</span>.</p>
|
||||
<p><strong>Step 2: Identify the optimal substructure, and then derive the state transition equation</strong></p>
|
||||
<p>Consider subproblem <span class="arithmatex">\(dp[i, j]\)</span>, where the tail characters of the corresponding two strings are <span class="arithmatex">\(s[i-1]\)</span> and <span class="arithmatex">\(t[j-1]\)</span>, which can be divided into the three cases shown in Figure 14-29 based on different edit operations.</p>
|
||||
<ol>
|
||||
|
||||
@@ -5694,7 +5694,7 @@ dp[i, c] = \max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1])
|
||||
<p align="center"> Figure 14-20 Dynamic programming process for 0-1 knapsack problem </p>
|
||||
|
||||
<h3 id="4-space-optimization">4. Space Optimization<a class="headerlink" href="#4-space-optimization" title="Permanent link">¶</a></h3>
|
||||
<p>Since each state is only related to the state in the row above it, we can use two arrays rolling forward to reduce the space complexity from <span class="arithmatex">\(O(n^2)\)</span> to <span class="arithmatex">\(O(n)\)</span>.</p>
|
||||
<p>Since each state is only related to the state in the row above it, we can use two arrays rolling forward to reduce the space complexity from <span class="arithmatex">\(O(n \times cap)\)</span> to <span class="arithmatex">\(O(cap)\)</span>.</p>
|
||||
<p>Further thinking, can we achieve space optimization using just one array? Observing, we can see that each state is transferred from the cell directly above or the cell in the upper-left. If there is only one array, when we start traversing row <span class="arithmatex">\(i\)</span>, that array still stores the state of row <span class="arithmatex">\(i-1\)</span>.</p>
|
||||
<ul>
|
||||
<li>If using forward traversal, then when traversing to <span class="arithmatex">\(dp[i, j]\)</span>, the values in the upper-left <span class="arithmatex">\(dp[i-1, 1]\)</span> ~ <span class="arithmatex">\(dp[i-1, j-1]\)</span> may have already been overwritten, thus preventing correct state transition.</li>
|
||||
|
||||
@@ -4651,7 +4651,7 @@
|
||||
<p><strong>Edit distance problem</strong></p>
|
||||
<ul>
|
||||
<li>Edit distance (Levenshtein distance) is used to measure the similarity between two strings, defined as the minimum number of edit steps from one string to another, with edit operations including insert, delete, and replace.</li>
|
||||
<li>The state definition for the edit distance problem is the minimum number of edit steps required to change the first <span class="arithmatex">\(i\)</span> characters of <span class="arithmatex">\(s\)</span> into the first <span class="arithmatex">\(j\)</span> characters of <span class="arithmatex">\(t\)</span>. When <span class="arithmatex">\(s[i] \ne t[j]\)</span>, there are three decisions: insert, delete, replace, each with corresponding remaining subproblems. From this, the optimal substructure can be identified and the state transition equation constructed. When <span class="arithmatex">\(s[i] = t[j]\)</span>, no edit is required for the current character.</li>
|
||||
<li>The state definition for the edit distance problem is the minimum number of edit steps required to change the first <span class="arithmatex">\(i\)</span> characters of <span class="arithmatex">\(s\)</span> into the first <span class="arithmatex">\(j\)</span> characters of <span class="arithmatex">\(t\)</span>. When <span class="arithmatex">\(s[i-1] \ne t[j-1]\)</span>, there are three decisions: insert, delete, replace, each with corresponding remaining subproblems. From this, the optimal substructure can be identified and the state transition equation constructed. When <span class="arithmatex">\(s[i-1] = t[j-1]\)</span>, no edit is required for the current character.</li>
|
||||
<li>In edit distance, the state depends on the state directly above, directly to the left, and to the upper-left, so after space optimization, neither forward nor reverse traversal can correctly perform state transitions. For this reason, we use a variable to temporarily store the upper-left state, thus transforming to a situation equivalent to the unbounded knapsack problem, allowing for forward traversal after space optimization.</li>
|
||||
</ul>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user