Skip to content

Commit 7dc75ac

Browse files
committed
deploy: e47bfba
1 parent 500b28e commit 7dc75ac

File tree

12 files changed

+7702
-7537
lines changed

12 files changed

+7702
-7537
lines changed

en/lc/1039/index.html

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30389,27 +30389,27 @@
3038930389
<ul class="md-nav__list">
3039030390

3039130391
<li class="md-nav__item">
30392-
<a href="#solution-1" class="md-nav__link">
30392+
<a href="#solution-1-memoization" class="md-nav__link">
3039330393
<span class="md-ellipsis">
30394-
Solution 1
30394+
Solution 1: Memoization
3039530395
</span>
3039630396
</a>
3039730397

3039830398
</li>
3039930399

3040030400
<li class="md-nav__item">
30401-
<a href="#solution-2" class="md-nav__link">
30401+
<a href="#solution-2-dynamic-programming" class="md-nav__link">
3040230402
<span class="md-ellipsis">
30403-
Solution 2
30403+
Solution 2: Dynamic Programming
3040430404
</span>
3040530405
</a>
3040630406

3040730407
</li>
3040830408

3040930409
<li class="md-nav__item">
30410-
<a href="#solution-3" class="md-nav__link">
30410+
<a href="#solution-3-dynamic-programming-alternative-implementation" class="md-nav__link">
3041130411
<span class="md-ellipsis">
30412-
Solution 3
30412+
Solution 3: Dynamic Programming (Alternative Implementation)
3041330413
</span>
3041430414
</a>
3041530415

@@ -87336,7 +87336,16 @@ <h2 id="description">Description</h2>
8733687336
<h2 id="solutions">Solutions</h2>
8733787337
<!-- solution:start -->
8733887338

87339-
<h3 id="solution-1">Solution 1</h3>
87339+
<h3 id="solution-1-memoization">Solution 1: Memoization</h3>
87340+
<p>We design a function <span class="arithmatex">\(\text{dfs}(i, j)\)</span>, which represents the minimum score after triangulating the polygon from vertex <span class="arithmatex">\(i\)</span> to <span class="arithmatex">\(j\)</span>. The answer is <span class="arithmatex">\(\text{dfs}(0, n - 1)\)</span>.</p>
87341+
<p>The calculation process of <span class="arithmatex">\(\text{dfs}(i, j)\)</span> is as follows:</p>
87342+
<ul>
87343+
<li>If <span class="arithmatex">\(i + 1 = j\)</span>, it means the polygon has only two vertices and cannot be triangulated, so we return <span class="arithmatex">\(0\)</span>;</li>
87344+
<li>Otherwise, we enumerate a vertex <span class="arithmatex">\(k\)</span> between <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span>, i.e., <span class="arithmatex">\(i \lt k \lt j\)</span>. Triangulating the polygon from vertex <span class="arithmatex">\(i\)</span> to <span class="arithmatex">\(j\)</span> can be divided into two subproblems: triangulating the polygon from vertex <span class="arithmatex">\(i\)</span> to <span class="arithmatex">\(k\)</span> and triangulating the polygon from vertex <span class="arithmatex">\(k\)</span> to <span class="arithmatex">\(j\)</span>. The minimum scores of these two subproblems are <span class="arithmatex">\(\text{dfs}(i, k)\)</span> and <span class="arithmatex">\(\text{dfs}(k, j)\)</span>, respectively. The score of the triangle formed by vertices <span class="arithmatex">\(i\)</span>, <span class="arithmatex">\(j\)</span>, and <span class="arithmatex">\(k\)</span> is <span class="arithmatex">\(\text{values}[i] \times \text{values}[k] \times \text{values}[j]\)</span>. Thus, the minimum score for this triangulation is <span class="arithmatex">\(\text{dfs}(i, k) + \text{dfs}(k, j) + \text{values}[i] \times \text{values}[k] \times \text{values}[j]\)</span>. We take the minimum value of all possibilities, which is the value of <span class="arithmatex">\(\text{dfs}(i, j)\)</span>.</li>
87345+
</ul>
87346+
<p>To avoid repeated calculations, we can use memoization, i.e., use a hash table or an array to store the already computed function values.</p>
87347+
<p>Finally, we return <span class="arithmatex">\(\text{dfs}(0, n - 1)\)</span>.</p>
87348+
<p>The time complexity is <span class="arithmatex">\(O(n^3)\)</span>, and the space complexity is <span class="arithmatex">\(O(n^2)\)</span>, where <span class="arithmatex">\(n\)</span> is the number of vertices in the polygon.</p>
8734087349
<div class="tabbed-set tabbed-alternate" data-tabs="1:5"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label></div>
8734187350
<div class="tabbed-content">
8734287351
<div class="tabbed-block">
@@ -87551,7 +87560,31 @@ <h3 id="solution-1">Solution 1</h3>
8755187560

8755287561
<!-- solution:start -->
8755387562

87554-
<h3 id="solution-2">Solution 2</h3>
87563+
<h3 id="solution-2-dynamic-programming">Solution 2: Dynamic Programming</h3>
87564+
<p>We can convert the memoization approach in Solution 1 into a dynamic programming approach.</p>
87565+
<p>Define <span class="arithmatex">\(f[i][j]\)</span> as the minimum score after triangulating the polygon from vertex <span class="arithmatex">\(i\)</span> to <span class="arithmatex">\(j\)</span>. Initially, <span class="arithmatex">\(f[i][j] = 0\)</span>, and the answer is <span class="arithmatex">\(f[0][n-1]\)</span>.</p>
87566+
<p>For <span class="arithmatex">\(f[i][j]\)</span> (where <span class="arithmatex">\(i + 1 \lt j\)</span>), we first initialize <span class="arithmatex">\(f[i][j]\)</span> to <span class="arithmatex">\(\infty\)</span>.</p>
87567+
<p>We enumerate a vertex <span class="arithmatex">\(k\)</span> between <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span>, i.e., <span class="arithmatex">\(i \lt k \lt j\)</span>. Triangulating the polygon from vertex <span class="arithmatex">\(i\)</span> to <span class="arithmatex">\(j\)</span> can be divided into two subproblems: triangulating the polygon from vertex <span class="arithmatex">\(i\)</span> to <span class="arithmatex">\(k\)</span> and triangulating the polygon from vertex <span class="arithmatex">\(k\)</span> to <span class="arithmatex">\(j\)</span>. The minimum scores of these two subproblems are <span class="arithmatex">\(f[i][k]\)</span> and <span class="arithmatex">\(f[k][j]\)</span>, respectively. The score of the triangle formed by vertices <span class="arithmatex">\(i\)</span>, <span class="arithmatex">\(j\)</span>, and <span class="arithmatex">\(k\)</span> is <span class="arithmatex">\(\text{values}[i] \times \text{values}[k] \times \text{values}[j]\)</span>. Thus, the minimum score for this triangulation is <span class="arithmatex">\(f[i][k] + f[k][j] + \text{values}[i] \times \text{values}[k] \times \text{values}[j]\)</span>. We take the minimum value of all possibilities, which becomes the value of <span class="arithmatex">\(f[i][j]\)</span>.</p>
87568+
<p>In summary, we can derive the state transition equation:</p>
87569+
<div class="arithmatex">\[
87570+
f[i][j]=
87571+
\begin{cases}
87572+
0, &amp; i+1=j \\
87573+
\infty, &amp; i+1&lt;j \\
87574+
\min_{i&lt;k&lt;j} \{f[i][k]+f[k][j]+\text{values}[i] \times \text{values}[k] \times \text{values}[j]\}, &amp; i+1&lt;j
87575+
\end{cases}
87576+
\]</div>
87577+
<p>Note that when enumerating <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span>, there are two possible enumeration strategies:</p>
87578+
<ol>
87579+
<li>Enumerate <span class="arithmatex">\(i\)</span> from large to small and <span class="arithmatex">\(j\)</span> from small to large. This ensures that when calculating the state <span class="arithmatex">\(f[i][j]\)</span>, the states <span class="arithmatex">\(f[i][k]\)</span> and <span class="arithmatex">\(f[k][j]\)</span> have already been computed.</li>
87580+
<li>Enumerate the interval length <span class="arithmatex">\(l\)</span> from small to large, where <span class="arithmatex">\(3 \leq l \leq n\)</span>. Then enumerate the left endpoint <span class="arithmatex">\(i\)</span> of the interval, and the right endpoint can be calculated as <span class="arithmatex">\(j = i + l - 1\)</span>. This also ensures that when calculating the larger interval <span class="arithmatex">\(f[i][j]\)</span>, the smaller intervals <span class="arithmatex">\(f[i][k]\)</span> and <span class="arithmatex">\(f[k][j]\)</span> have already been computed.</li>
87581+
</ol>
87582+
<p>Finally, we return <span class="arithmatex">\(f[0][n-1]\)</span>.</p>
87583+
<p>The time complexity is <span class="arithmatex">\(O(n^3)\)</span>, and the space complexity is <span class="arithmatex">\(O(n^2)\)</span>, where <span class="arithmatex">\(n\)</span> is the number of vertices in the polygon.</p>
87584+
<p>Related problems:</p>
87585+
<ul>
87586+
<li><a href="https://github.com/doocs/leetcode/blob/main/solution/1300-1399/1312.Minimum%20Insertion%20Steps%20to%20Make%20a%20String%20Palindrome/README.md">1312. Minimum Insertion Steps to Make a String Palindrome</a></li>
87587+
</ul>
8755587588
<div class="tabbed-set tabbed-alternate" data-tabs="2:5"><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" /><div class="tabbed-labels"><label for="__tabbed_2_1">Python3</label><label for="__tabbed_2_2">Java</label><label for="__tabbed_2_3">C++</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">TypeScript</label></div>
8755687589
<div class="tabbed-content">
8755787590
<div class="tabbed-block">
@@ -87707,10 +87740,11 @@ <h3 id="solution-2">Solution 2</h3>
8770787740
</div>
8770887741
</div>
8770987742
<!-- solution:end -->
87710-
8771187743
<!-- solution:start -->
8771287744

87713-
<h3 id="solution-3">Solution 3</h3>
87745+
<h3 id="solution-3-dynamic-programming-alternative-implementation">Solution 3: Dynamic Programming (Alternative Implementation)</h3>
87746+
<p>In Solution 2, we mentioned two enumeration strategies. Here, we use the second strategy: enumerate the interval length <span class="arithmatex">\(l\)</span> from small to large, where <span class="arithmatex">\(3 \leq l \leq n\)</span>. Then, enumerate the left endpoint <span class="arithmatex">\(i\)</span> of the interval, and the right endpoint can be calculated as <span class="arithmatex">\(j = i + l - 1\)</span>.</p>
87747+
<p>The time complexity is <span class="arithmatex">\(O(n^3)\)</span>, and the space complexity is <span class="arithmatex">\(O(n^2)\)</span>, where <span class="arithmatex">\(n\)</span> is the number of vertices in the polygon.</p>
8771487748
<div class="tabbed-set tabbed-alternate" data-tabs="3:5"><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" /><div class="tabbed-labels"><label for="__tabbed_3_1">Python3</label><label for="__tabbed_3_2">Java</label><label for="__tabbed_3_3">C++</label><label for="__tabbed_3_4">Go</label><label for="__tabbed_3_5">TypeScript</label></div>
8771587749
<div class="tabbed-content">
8771687750
<div class="tabbed-block">

en/lc/1041/index.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30433,9 +30433,9 @@
3043330433
<ul class="md-nav__list">
3043430434

3043530435
<li class="md-nav__item">
30436-
<a href="#solution-1" class="md-nav__link">
30436+
<a href="#solution-1-simulation" class="md-nav__link">
3043730437
<span class="md-ellipsis">
30438-
Solution 1
30438+
Solution 1: Simulation
3043930439
</span>
3044030440
</a>
3044130441

@@ -87345,7 +87345,19 @@ <h2 id="description">Description</h2>
8734587345
<h2 id="solutions">Solutions</h2>
8734687346
<!-- solution:start -->
8734787347

87348-
<h3 id="solution-1">Solution 1</h3>
87348+
<h3 id="solution-1-simulation">Solution 1: Simulation</h3>
87349+
<p>We can simulate the robot's movement. Use a variable <span class="arithmatex">\(k\)</span> to represent the robot's direction, initialized to <span class="arithmatex">\(0\)</span>, which means the robot is facing north. The variable <span class="arithmatex">\(k\)</span> can take values in the range <span class="arithmatex">\([0, 3]\)</span>, representing the robot facing north, west, south, and east, respectively. Additionally, we use an array <span class="arithmatex">\(dist\)</span> of length <span class="arithmatex">\(4\)</span> to record the distance the robot travels in the four directions, initialized to <span class="arithmatex">\([0, 0, 0, 0]\)</span>.</p>
87350+
<p>Traverse the instruction string <span class="arithmatex">\(\textit{instructions}\)</span>. If the current instruction is <code>'L'</code>, the robot turns west, i.e., <span class="arithmatex">\(k = (k + 1) \bmod 4\)</span>; if the instruction is <code>'R'</code>, the robot turns east, i.e., <span class="arithmatex">\(k = (k + 3) \bmod 4\)</span>; otherwise, the robot moves one step in the current direction, i.e., <span class="arithmatex">\(dist[k]++\)</span>.</p>
87351+
<p>If the given instruction string <span class="arithmatex">\(\textit{instructions}\)</span> is executed once and the robot returns to the origin, i.e., <span class="arithmatex">\(dist[0] = dist[2]\)</span> and <span class="arithmatex">\(dist[1] = dist[3]\)</span>, then the robot will definitely enter a loop. This is because no matter how many times the instructions are repeated, the robot always returns to the origin, so it must enter a loop.</p>
87352+
<p>If the given instruction string <span class="arithmatex">\(\textit{instructions}\)</span> is executed once and the robot does not return to the origin, suppose the robot is at <span class="arithmatex">\((x, y)\)</span> and its direction is <span class="arithmatex">\(k\)</span>.</p>
87353+
<ul>
87354+
<li>If <span class="arithmatex">\(k=0\)</span>, i.e., the robot is facing north, then after executing the instructions a second time, the coordinate change is <span class="arithmatex">\((x, y)\)</span>; after executing the instructions a third time, the coordinate change is still <span class="arithmatex">\((x, y)\)</span>... Accumulating these changes, the robot will eventually reach <span class="arithmatex">\((n \times x, n \times y)\)</span>, where <span class="arithmatex">\(n\)</span> is a positive integer. Since the robot does not return to the origin, i.e., <span class="arithmatex">\(x \neq 0\)</span> or <span class="arithmatex">\(y \neq 0\)</span>, it follows that <span class="arithmatex">\(n \times x \neq 0\)</span> or <span class="arithmatex">\(n \times y \neq 0\)</span>, so the robot will not enter a loop;</li>
87355+
<li>If <span class="arithmatex">\(k=1\)</span>, i.e., the robot is facing west, then after executing the instructions a second time, the coordinate change is <span class="arithmatex">\((-y, x)\)</span>; after executing the instructions a third time, the coordinate change is <span class="arithmatex">\((-x, -y)\)</span>; after executing the instructions a fourth time, the coordinate change is <span class="arithmatex">\((y, -x)\)</span>. Accumulating these coordinate changes, we find that the robot will eventually return to the origin <span class="arithmatex">\((0, 0)\)</span>;</li>
87356+
<li>If <span class="arithmatex">\(k=2\)</span>, i.e., the robot is facing south, then after executing the instructions a second time, the coordinate change is <span class="arithmatex">\((-x, -y)\)</span>. Accumulating these two coordinate changes, we find that the robot will eventually return to the origin <span class="arithmatex">\((0, 0)\)</span>;</li>
87357+
<li>If <span class="arithmatex">\(k=3\)</span>, i.e., the robot is facing east, then after executing the instructions a second time, the coordinate change is <span class="arithmatex">\((y, -x)\)</span>; after executing the instructions a third time, the coordinate change is <span class="arithmatex">\((-x, -y)\)</span>; after executing the instructions a fourth time, the coordinate change is <span class="arithmatex">\((-y, x)\)</span>. Accumulating these coordinate changes, we find that the robot will eventually return to the origin <span class="arithmatex">\((0, 0)\)</span>.</li>
87358+
</ul>
87359+
<p>In conclusion, if the given instruction string <span class="arithmatex">\(\textit{instructions}\)</span> is executed once and the robot returns to the origin, or if the robot's direction is different from the initial direction, then the robot will definitely enter a loop.</p>
87360+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(1)\)</span>, where <span class="arithmatex">\(n\)</span> is the length of the instruction string <span class="arithmatex">\(\textit{instructions}\)</span>.</p>
8734987361
<div class="tabbed-set tabbed-alternate" data-tabs="1:5"><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" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label></div>
8735087362
<div class="tabbed-content">
8735187363
<div class="tabbed-block">

0 commit comments

Comments
 (0)