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
<h3 id="solution-1-two-pointers">Solution 1: Two Pointers</h3>
86413
+
<p>We can use two pointers <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span>, pointing to the beginning and end of the string, respectively, and then move towards the center, counting the number of different characters. If the number of different characters is greater than <span class="arithmatex">\(2\)</span>, return <span class="arithmatex">\(\textit{false}\)</span>; otherwise, return <span class="arithmatex">\(\textit{true}\)</span>.</p>
86414
+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(1)\)</span>. Here, <span class="arithmatex">\(n\)</span> is the length of the string <span class="arithmatex">\(s\)</span>.</p>
<h3 id="solution-1-tree-dp">Solution 1: Tree DP</h3>
86430
+
<p>We design a function <span class="arithmatex">\(dfs(i)\)</span>, which represents the maximum sum of the weights of selected edges in the subtree rooted at node <span class="arithmatex">\(i\)</span>, such that no two selected edges are adjacent. This function returns two values <span class="arithmatex">\((a, b)\)</span>. The first value <span class="arithmatex">\(a\)</span> represents the sum of the weights of selected edges when the edge between the current node <span class="arithmatex">\(i\)</span> and its parent node is selected. The second value <span class="arithmatex">\(b\)</span> represents the sum of the weights of selected edges when the edge between the current node <span class="arithmatex">\(i\)</span> and its parent node is not selected.</p>
86431
+
<p>We can observe the following for the current node <span class="arithmatex">\(i\)</span>:</p>
86432
+
<ul>
86433
+
<li>If the edge between <span class="arithmatex">\(i\)</span> and its parent node is selected, then none of the edges between <span class="arithmatex">\(i\)</span> and its child nodes can be selected. In this case, the value of <span class="arithmatex">\(a\)</span> for the current node is the sum of the <span class="arithmatex">\(b\)</span> values of all its child nodes.</li>
86434
+
<li>If the edge between <span class="arithmatex">\(i\)</span> and its parent node is not selected, then we can select at most one edge between <span class="arithmatex">\(i\)</span> and its child nodes. In this case, the value of <span class="arithmatex">\(b\)</span> for the current node is the sum of the <span class="arithmatex">\(a\)</span> values of the selected child nodes and the <span class="arithmatex">\(b\)</span> values of the unselected child nodes, plus the weight of the edge between <span class="arithmatex">\(i\)</span> and the selected child node.</li>
86435
+
</ul>
86436
+
<p>We call the function <span class="arithmatex">\(dfs(0)\)</span>, and the second value returned is the answer, which is the sum of the weights of selected edges when the edge between the root node and its parent node is not selected.</p>
86437
+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>. Here, <span class="arithmatex">\(n\)</span> is the number of nodes.</p>
<p>According to the problem description, we know that the number of 0s and 1s on the path from the top-left corner to the bottom-right corner is equal, and the total number is <span class="arithmatex">\(m + n - 1\)</span>, which means the number of 0s and 1s are both <span class="arithmatex">\((m + n - 1) / 2\)</span>.</p>
86409
+
<p>Therefore, we can use memoization search, starting from the top-left corner and moving right or down until reaching the bottom-right corner, to check if the number of 0s and 1s on the path is equal.</p>
86410
+
<p>The time complexity is <span class="arithmatex">\(O(m \times n \times (m + n))\)</span>. Here, <span class="arithmatex">\(m\)</span> and <span class="arithmatex">\(n\)</span> are the number of rows and columns of the matrix, respectively.</p>
<h3 id="solution-1-linear-sieve">Solution 1: Linear Sieve</h3>
86416
+
<p>For the given range <span class="arithmatex">\([\textit{left}, \textit{right}]\)</span>, we can use the linear sieve method to find all prime numbers. Then, we traverse the prime numbers in ascending order to find the pair of adjacent prime numbers with the smallest difference, which will be the answer.</p>
86417
+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>. Here, <span class="arithmatex">\(n = \textit{right}\)</span>.</p>
0 commit comments