You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<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>
<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, & i+1=j \\
87573
+
\infty, & i+1<j \\
87574
+
\min_{i<k<j} \{f[i][k]+f[k][j]+\text{values}[i] \times \text{values}[k] \times \text{values}[j]\}, & i+1<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>
<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>
<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>
0 commit comments