Skip to content

Commit 350b2df

Browse files
committed
deploy: ad49ba9
1 parent 6fa61dd commit 350b2df

File tree

11 files changed

+249
-101
lines changed

11 files changed

+249
-101
lines changed

en/lc/965/index.html

Lines changed: 62 additions & 38 deletions
Large diffs are not rendered by default.

en/lc/966/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27656,9 +27656,9 @@
2765627656
<ul class="md-nav__list">
2765727657

2765827658
<li class="md-nav__item">
27659-
<a href="#solution-1" class="md-nav__link">
27659+
<a href="#solution-1-hash-table" class="md-nav__link">
2766027660
<span class="md-ellipsis">
27661-
Solution 1
27661+
Solution 1: Hash Table
2766227662
</span>
2766327663
</a>
2766427664

@@ -81955,7 +81955,11 @@ <h2 id="description">Description</h2>
8195581955
<h2 id="solutions">Solutions</h2>
8195681956
<!-- solution:start -->
8195781957

81958-
<h3 id="solution-1">Solution 1</h3>
81958+
<h3 id="solution-1-hash-table">Solution 1: Hash Table</h3>
81959+
<p>We traverse the $\textit{wordlist}$ and store the words in hash tables $\textit{low}$ and $\textit{pat}$ according to case-insensitive and vowel-insensitive rules, respectively. The key of $\textit{low}$ is the lowercase form of the word, and the key of $\textit{pat}$ is the string obtained by replacing the vowels of the word with <code>*</code>, with the value being the word itself. We use the hash table $\textit{s}$ to store the words in $\textit{wordlist}$.</p>
81960+
<p>We traverse $\textit{queries}$, for each word $\textit{q}$, if $\textit{q}$ is in $\textit{s}$, it means $\textit{q}$ is in $\textit{wordlist}$, and we directly add $\textit{q}$ to the answer array $\textit{ans}$; otherwise, if the lowercase form of $\textit{q}$ is in $\textit{low}$, it means $\textit{q}$ is in $\textit{wordlist}$ and is case-insensitive, and we add $\textit{low}[q.\text{lower}()]$ to the answer array $\textit{ans}$; otherwise, if the string obtained by replacing the vowels of $\textit{q}$ with <code>*</code> is in $\textit{pat}$, it means $\textit{q}$ is in $\textit{wordlist}$ and is vowel-insensitive, and we add $\textit{pat}[f(q)]$ to the answer array $\textit{ans}$; otherwise, it means $\textit{q}$ is not in $\textit{wordlist}$, and we add an empty string to the answer array $\textit{ans}$.</p>
81961+
<p>Finally, we return the answer array $\textit{ans}$.</p>
81962+
<p>The time complexity is $O(n + m)$, and the space complexity is $O(n)$, where $n$ and $m$ are the lengths of $\textit{wordlist}$ and $\textit{queries}$, respectively.</p>
8195981963
<div class="tabbed-set tabbed-alternate" data-tabs="1:4"><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" /><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></div>
8196081964
<div class="tabbed-content">
8196181965
<div class="tabbed-block">

en/lc/968/index.html

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27698,9 +27698,9 @@
2769827698
<ul class="md-nav__list">
2769927699

2770027700
<li class="md-nav__item">
27701-
<a href="#solution-1" class="md-nav__link">
27701+
<a href="#solution-1-dynamic-programming-tree-dp" class="md-nav__link">
2770227702
<span class="md-ellipsis">
27703-
Solution 1
27703+
Solution 1: Dynamic Programming (Tree DP)
2770427704
</span>
2770527705
</a>
2770627706

@@ -81938,7 +81938,24 @@ <h2 id="description">Description</h2>
8193881938
<h2 id="solutions">Solutions</h2>
8193981939
<!-- solution:start -->
8194081940

81941-
<h3 id="solution-1">Solution 1</h3>
81941+
<h3 id="solution-1-dynamic-programming-tree-dp">Solution 1: Dynamic Programming (Tree DP)</h3>
81942+
<p>For each node, we define three states:</p>
81943+
<ul>
81944+
<li><code>a</code>: The current node has a camera</li>
81945+
<li><code>b</code>: The current node does not have a camera, but is monitored by its children</li>
81946+
<li><code>c</code>: The current node does not have a camera and is not monitored by its children</li>
81947+
</ul>
81948+
<p>Next, we design a function $dfs(root)$, which will return an array of length 3, representing the minimum number of cameras in the subtree rooted at <code>root</code> for the three states. The answer is $\min(dfs(root)[0], dfs(root)[1])$.</p>
81949+
<p>The calculation process of the function $dfs(root)$ is as follows:</p>
81950+
<p>If <code>root</code> is null, return $[inf, 0, 0]$, where <code>inf</code> represents a very large number, used to indicate an impossible situation.</p>
81951+
<p>Otherwise, we recursively calculate the left and right subtrees of <code>root</code>, obtaining $[la, lb, lc]$ and $[ra, rb, rc]$ respectively.</p>
81952+
<ul>
81953+
<li>If the current node has a camera, then its left and right children must be in a monitored state, i.e., $a = \min(la, lb, lc) + \min(ra, rb, rc) + 1$.</li>
81954+
<li>If the current node does not have a camera but is monitored by its children, then one or both of the children must have a camera, i.e., $b = \min(la + rb, lb + ra, la + ra)$.</li>
81955+
<li>If the current node does not have a camera and is not monitored by its children, then the children must be monitored by their children, i.e., $c = lb + rb$.</li>
81956+
</ul>
81957+
<p>Finally, we return $[a, b, c]$.</p>
81958+
<p>The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree.</p>
8194281959
<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>
8194381960
<div class="tabbed-content">
8194481961
<div class="tabbed-block">

en/lc/971/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27761,9 +27761,9 @@
2776127761
<ul class="md-nav__list">
2776227762

2776327763
<li class="md-nav__item">
27764-
<a href="#solution-1" class="md-nav__link">
27764+
<a href="#solution-1-dfs" class="md-nav__link">
2776527765
<span class="md-ellipsis">
27766-
Solution 1
27766+
Solution 1: DFS
2776727767
</span>
2776827768
</a>
2776927769

@@ -81947,7 +81947,10 @@ <h2 id="description">Description</h2>
8194781947
<h2 id="solutions">Solutions</h2>
8194881948
<!-- solution:start -->
8194981949

81950-
<h3 id="solution-1">Solution 1</h3>
81950+
<h3 id="solution-1-dfs">Solution 1: DFS</h3>
81951+
<p>We can traverse the entire tree using depth-first search, using an index $i$ to record the current node's index in the $\textit{voyage}$ array. If the value of the current node does not equal $\textit{voyage}[i]$, it means that it is impossible to match after flipping, we mark $\textit{ok}$ as <code>false</code> and return immediately. Otherwise, we increment $i$ by $1$, then check if the current node has a left child. If it does not, or if the value of the left child equals $\textit{voyage}[i]$, we recursively traverse the current left and right children; otherwise, we need to flip the current node and then recursively traverse the current right and left children.</p>
81952+
<p>After the search, if $\textit{ok}$ is <code>true</code>, it means that it is possible to match after flipping, and we return the answer array $\textit{ans}$, otherwise, we return $[-1]$.</p>
81953+
<p>The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree.</p>
8195181954
<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>
8195281955
<div class="tabbed-content">
8195381956
<div class="tabbed-block">

en/lc/978/index.html

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27908,9 +27908,9 @@
2790827908
<ul class="md-nav__list">
2790927909

2791027910
<li class="md-nav__item">
27911-
<a href="#solution-1" class="md-nav__link">
27911+
<a href="#solution-1-dynamic-programming" class="md-nav__link">
2791227912
<span class="md-ellipsis">
27913-
Solution 1
27913+
Solution 1: Dynamic Programming
2791427914
</span>
2791527915
</a>
2791627916

@@ -81957,8 +81957,12 @@ <h2 id="description">Description</h2>
8195781957
<h2 id="solutions">Solutions</h2>
8195881958
<!-- solution:start -->
8195981959

81960-
<h3 id="solution-1">Solution 1</h3>
81961-
<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>
81960+
<h3 id="solution-1-dynamic-programming">Solution 1: Dynamic Programming</h3>
81961+
<p>We define $f[i]$ as the length of the longest turbulent subarray ending at $\textit{nums}[i]$ with an increasing state, and $g[i]$ as the length of the longest turbulent subarray ending at $\textit{nums}[i]$ with a decreasing state. Initially, $f[0] = 1$, $g[0] = 1$. The answer is $\max(f[i], g[i])$.</p>
81962+
<p>For $i \gt 0$, if $\textit{nums}[i] \gt \textit{nums}[i - 1]$, then $f[i] = g[i - 1] + 1$, otherwise $f[i] = 1$; if $\textit{nums}[i] \lt \textit{nums}[i - 1]$, then $g[i] = f[i - 1] + 1$, otherwise $g[i] = 1$.</p>
81963+
<p>Since $f[i]$ and $g[i]$ are only related to $f[i - 1]$ and $g[i - 1]$, two variables can be used instead of arrays.</p>
81964+
<p>The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.</p>
81965+
<div class="tabbed-set tabbed-alternate" data-tabs="1:6"><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" /><input id="__tabbed_1_6" 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><label for="__tabbed_1_6">Rust</label></div>
8196281966
<div class="tabbed-content">
8196381967
<div class="tabbed-block">
8196481968
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal">1</span>
@@ -82098,6 +82102,42 @@ <h3 id="solution-1">Solution 1</h3>
8209882102
<span class="p">}</span>
8209982103
</code></pre></div></td></tr></table></div>
8210082104
</div>
82105+
<div class="tabbed-block">
82106+
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
82107+
<span class="normal"> 2</span>
82108+
<span class="normal"> 3</span>
82109+
<span class="normal"> 4</span>
82110+
<span class="normal"> 5</span>
82111+
<span class="normal"> 6</span>
82112+
<span class="normal"> 7</span>
82113+
<span class="normal"> 8</span>
82114+
<span class="normal"> 9</span>
82115+
<span class="normal">10</span>
82116+
<span class="normal">11</span>
82117+
<span class="normal">12</span>
82118+
<span class="normal">13</span>
82119+
<span class="normal">14</span>
82120+
<span class="normal">15</span>
82121+
<span class="normal">16</span>
82122+
<span class="normal">17</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Solution</span><span class="w"> </span><span class="p">{</span>
82123+
<span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="nf">max_turbulence_size</span><span class="p">(</span><span class="n">arr</span><span class="p">:</span><span class="w"> </span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span>
82124+
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ans</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
82125+
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
82126+
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
82127+
82128+
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">1</span><span class="o">..</span><span class="n">arr</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
82129+
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">ff</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">};</span>
82130+
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">gg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">};</span>
82131+
<span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ff</span><span class="p">;</span>
82132+
<span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">gg</span><span class="p">;</span>
82133+
<span class="w"> </span><span class="n">ans</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ans</span><span class="p">.</span><span class="n">max</span><span class="p">(</span><span class="n">f</span><span class="p">.</span><span class="n">max</span><span class="p">(</span><span class="n">g</span><span class="p">));</span>
82134+
<span class="w"> </span><span class="p">}</span>
82135+
82136+
<span class="w"> </span><span class="n">ans</span>
82137+
<span class="w"> </span><span class="p">}</span>
82138+
<span class="p">}</span>
82139+
</code></pre></div></td></tr></table></div>
82140+
</div>
8210182141
</div>
8210282142
</div>
8210382143
<!-- solution:end -->

en/search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)