Skip to content

Commit 4d5f7f3

Browse files
committed
deploy: 7840036
1 parent 6f5efa5 commit 4d5f7f3

File tree

295 files changed

+7855
-7811
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

295 files changed

+7855
-7811
lines changed

en/lc/1130/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77612,15 +77612,15 @@ <h3 id="solution-1-memoization-search">Solution 1: Memoization Search</h3>
7761277612
<p>In summary, we can get:</p>
7761377613
<p>$$
7761477614
dfs(i, j) = \begin{cases}
77615-
0, &amp; \text{if } i = j \
77616-
\min_{i \leq k &lt; j} {dfs(i, k) + dfs(k + 1, j) + \max_{i \leq t \leq k} {arr[t]} \max_{k &lt; t \leq j} {arr[t]}}, &amp; \text{if } i &lt; j
77615+
0, &amp; \textit{if } i = j \
77616+
\min_{i \leq k &lt; j} {dfs(i, k) + dfs(k + 1, j) + \max_{i \leq t \leq k} {arr[t]} \max_{k &lt; t \leq j} {arr[t]}}, &amp; \textit{if } i &lt; j
7761777617
\end{cases}
7761877618
$$</p>
7761977619
<p>In the above recursive process, we can use the method of memoization search to avoid repeated calculations. Additionally, we can use an array $g$ to record the maximum value of all leaf nodes in the index range $[i, j]$ of the array $arr$. This allows us to optimize the calculation process of $dfs(i, j)$:</p>
7762077620
<p>$$
7762177621
dfs(i, j) = \begin{cases}
77622-
0, &amp; \text{if } i = j \
77623-
\min_{i \leq k &lt; j} {dfs(i, k) + dfs(k + 1, j) + g[i][k] \cdot g[k + 1][j]}, &amp; \text{if } i &lt; j
77622+
0, &amp; \textit{if } i = j \
77623+
\min_{i \leq k &lt; j} {dfs(i, k) + dfs(k + 1, j) + g[i][k] \cdot g[k + 1][j]}, &amp; \textit{if } i &lt; j
7762477624
\end{cases}
7762577625
$$</p>
7762677626
<p>Finally, we return $dfs(0, n - 1)$.</p>
@@ -77908,8 +77908,8 @@ <h3 id="solution-2-dynamic-programming">Solution 2: Dynamic Programming</h3>
7790877908
<p>Define $f[i][j]$ to represent the minimum possible sum of all non-leaf node values in the index range $[i, j]$ of the array $arr$, and $g[i][j]$ to represent the maximum value of all leaf nodes in the index range $[i, j]$ of the array $arr$. Then, the state transition equation is:</p>
7790977909
<p>$$
7791077910
f[i][j] = \begin{cases}
77911-
0, &amp; \text{if } i = j \
77912-
\min_{i \leq k &lt; j} {f[i][k] + f[k + 1][j] + g[i][k] \cdot g[k + 1][j]}, &amp; \text{if } i &lt; j
77911+
0, &amp; \textit{if } i = j \
77912+
\min_{i \leq k &lt; j} {f[i][k] + f[k + 1][j] + g[i][k] \cdot g[k + 1][j]}, &amp; \textit{if } i &lt; j
7791377913
\end{cases}
7791477914
$$</p>
7791577915
<p>Finally, we return $f[0][n - 1]$.</p>

en/lc/1143/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77576,8 +77576,8 @@ <h3 id="solution-1-dynamic-programming">Solution 1: Dynamic Programming</h3>
7757677576
<p>$$
7757777577
f[i][j] =
7757877578
\begin{cases}
77579-
f[i - 1][j - 1] + 1, &amp; \text{if } text1[i - 1] = text2[j - 1] \
77580-
\max(f[i - 1][j], f[i][j - 1]), &amp; \text{if } text1[i - 1] \neq text2[j - 1]
77579+
f[i - 1][j - 1] + 1, &amp; \textit{if } text1[i - 1] = text2[j - 1] \
77580+
\max(f[i - 1][j], f[i][j - 1]), &amp; \textit{if } text1[i - 1] \neq text2[j - 1]
7758177581
\end{cases}
7758277582
$$</p>
7758377583
<p>The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the lengths of $text1$ and $text2$, respectively.</p>

en/lc/1208/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77916,7 +77916,7 @@ <h3 id="solution-1-prefix-sum-binary-search">Solution 1: Prefix Sum + Binary Sea
7791677916
<!-- solution:start -->
7791777917

7791877918
<h3 id="solution-2-two-pointers">Solution 2: Two Pointers</h3>
77919-
<p>We can maintain two pointers $l$ and $r$, initially $l = r = 0$; maintain a variable $\text{cost}$, which represents the sum of the absolute values of the ASCII code differences in the index interval $[l,..r]$. In each step, we move $r$ to the right by one position, then update $\text{cost} = \text{cost} + |s[r] - t[r]|$. If $\text{cost} \gt \text{maxCost}$, then we loop to move $l$ to the right by one position, and decrease the value of $\text{cost}$, until $\text{cost} \leq \text{maxCost}$. Then we update the answer, that is, $\text{ans} = \max(\text{ans}, r - l + 1)$.</p>
77919+
<p>We can maintain two pointers $l$ and $r$, initially $l = r = 0$; maintain a variable $\textit{cost}$, which represents the sum of the absolute values of the ASCII code differences in the index interval $[l,..r]$. In each step, we move $r$ to the right by one position, then update $\textit{cost} = \textit{cost} + |s[r] - t[r]|$. If $\textit{cost} \gt \textit{maxCost}$, then we loop to move $l$ to the right by one position, and decrease the value of $\textit{cost}$, until $\textit{cost} \leq \textit{maxCost}$. Then we update the answer, that is, $\textit{ans} = \max(\textit{ans}, r - l + 1)$.</p>
7792077920
<p>Finally, return the answer.</p>
7792177921
<p>The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.</p>
7792277922
<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>
@@ -78087,7 +78087,7 @@ <h3 id="solution-2-two-pointers">Solution 2: Two Pointers</h3>
7808778087

7808878088
<h3 id="solution-3-another-way-of-using-two-pointers">Solution 3: Another Way of Using Two Pointers</h3>
7808978089
<p>In Solution 2, the interval maintained by the two pointers may become shorter or longer. Since the problem only requires the maximum length, we can maintain a monotonically increasing interval.</p>
78090-
<p>Specifically, we use two pointers $l$ and $r$ to point to the left and right endpoints of the interval, initially $l = r = 0$. In each step, we move $r$ to the right by one position, then update $\text{cost} = \text{cost} + |s[r] - t[r]|$. If $\text{cost} \gt \text{maxCost}$, then we move $l$ to the right by one position, and decrease the value of $\text{cost}$.</p>
78090+
<p>Specifically, we use two pointers $l$ and $r$ to point to the left and right endpoints of the interval, initially $l = r = 0$. In each step, we move $r$ to the right by one position, then update $\textit{cost} = \textit{cost} + |s[r] - t[r]|$. If $\textit{cost} \gt \textit{maxCost}$, then we move $l$ to the right by one position, and decrease the value of $\textit{cost}$.</p>
7809178091
<p>Finally, return $n - l$.</p>
7809278092
<p>The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the string $s$.</p>
7809378093
<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>
@@ -78275,14 +78275,14 @@ <h3 id="solution-3-another-way-of-using-two-pointers">Solution 3: Another Way of
7827578275

7827678276
<nav>
7827778277

78278-
<a href="https://github.com/rain84" class="md-author" title="@rain84">
78278+
<a href="https://github.com/yanglbme" class="md-author" title="@yanglbme">
7827978279

78280-
<img src="https://avatars.githubusercontent.com/u/1732547?v=4&size=72" alt="rain84">
78280+
<img src="https://avatars.githubusercontent.com/u/21008209?v=4&size=72" alt="yanglbme">
7828178281
</a>
7828278282

78283-
<a href="https://github.com/yanglbme" class="md-author" title="@yanglbme">
78283+
<a href="https://github.com/rain84" class="md-author" title="@rain84">
7828478284

78285-
<img src="https://avatars.githubusercontent.com/u/21008209?v=4&size=72" alt="yanglbme">
78285+
<img src="https://avatars.githubusercontent.com/u/1732547?v=4&size=72" alt="rain84">
7828678286
</a>
7828778287

7828878288

en/lc/1252/index.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77595,9 +77595,9 @@ <h2 id="solutions">Solutions</h2>
7759577595
<!-- solution:start -->
7759677596

7759777597
<h3 id="solution-1-simulation">Solution 1: Simulation</h3>
77598-
<p>We create a matrix $g$ to store the result of operations. For each pair $(r_i, c_i)$ in $\text{indices}$, we add $1$ to all numbers in the $r_i$-th row of the matrix and add $1$ to all elements in the $c_i$-th column.</p>
77598+
<p>We create a matrix $g$ to store the result of operations. For each pair $(r_i, c_i)$ in $\textit{indices}$, we add $1$ to all numbers in the $r_i$-th row of the matrix and add $1$ to all elements in the $c_i$-th column.</p>
7759977599
<p>After the simulation ends, we traverse the matrix and count the number of odd numbers.</p>
77600-
<p>The time complexity is $O(k \times (m + n) + m \times n)$, and the space complexity is $O(m \times n)$. Here, $k$ is the length of $\text{indices}$.</p>
77600+
<p>The time complexity is $O(k \times (m + n) + m \times n)$, and the space complexity is $O(m \times n)$. Here, $k$ is the length of $\textit{indices}$.</p>
7760177601
<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>
7760277602
<div class="tabbed-content">
7760377603
<div class="tabbed-block">
@@ -77763,9 +77763,9 @@ <h3 id="solution-1-simulation">Solution 1: Simulation</h3>
7776377763
<!-- solution:start -->
7776477764

7776577765
<h3 id="solution-2-space-optimization">Solution 2: Space Optimization</h3>
77766-
<p>We can use a row array $\text{row}$ and a column array $\text{col}$ to record the number of times each row and column is incremented. For each pair $(r_i, c_i)$ in $\text{indices}$, we add $1$ to $\text{row}[r_i]$ and $\text{col}[c_i]$ respectively.</p>
77767-
<p>After the operations are completed, the count at position $(i, j)$ can be calculated as $\text{row}[i] + \text{col}[j]$. We traverse the matrix and count the number of odd numbers.</p>
77768-
<p>The time complexity is $O(k + m \times n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\text{indices}$.</p>
77766+
<p>We can use a row array $\textit{row}$ and a column array $\textit{col}$ to record the number of times each row and column is incremented. For each pair $(r_i, c_i)$ in $\textit{indices}$, we add $1$ to $\textit{row}[r_i]$ and $\textit{col}[c_i]$ respectively.</p>
77767+
<p>After the operations are completed, the count at position $(i, j)$ can be calculated as $\textit{row}[i] + \textit{col}[j]$. We traverse the matrix and count the number of odd numbers.</p>
77768+
<p>The time complexity is $O(k + m \times n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\textit{indices}$.</p>
7776977769
<div class="tabbed-set tabbed-alternate" data-tabs="2:4"><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" /><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></div>
7777077770
<div class="tabbed-content">
7777177771
<div class="tabbed-block">
@@ -77905,9 +77905,9 @@ <h3 id="solution-2-space-optimization">Solution 2: Space Optimization</h3>
7790577905
<!-- solution:start -->
7790677906

7790777907
<h3 id="solution-3-mathematical-optimization">Solution 3: Mathematical Optimization</h3>
77908-
<p>We notice that a number at position $(i, j)$ in the matrix will be odd only when exactly one of $\text{row}[i]$ and $\text{col}[j]$ is odd and the other is even.</p>
77909-
<p>We count the number of odd numbers in $\text{row}$, denoted as $\text{cnt1}$, and the number of odd numbers in $\text{col}$, denoted as $\text{cnt2}$. Therefore, the final count of odd numbers is $\text{cnt1} \times (n - \text{cnt2}) + \text{cnt2} \times (m - \text{cnt1})$.</p>
77910-
<p>The time complexity is $O(k + m + n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\text{indices}$.</p>
77908+
<p>We notice that a number at position $(i, j)$ in the matrix will be odd only when exactly one of $\textit{row}[i]$ and $\textit{col}[j]$ is odd and the other is even.</p>
77909+
<p>We count the number of odd numbers in $\textit{row}$, denoted as $\textit{cnt1}$, and the number of odd numbers in $\textit{col}$, denoted as $\textit{cnt2}$. Therefore, the final count of odd numbers is $\textit{cnt1} \times (n - \textit{cnt2}) + \textit{cnt2} \times (m - \textit{cnt1})$.</p>
77910+
<p>The time complexity is $O(k + m + n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\textit{indices}$.</p>
7791177911
<div class="tabbed-set tabbed-alternate" data-tabs="3:4"><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" /><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></div>
7791277912
<div class="tabbed-content">
7791377913
<div class="tabbed-block">

en/lc/1411/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77714,9 +77714,9 @@ <h3 id="solution-2-state-compression-dynamic-programming">Solution 2: State Comp
7771477714
<p>We notice that the grid only has $3$ columns, so there are at most $3^3=27$ different coloring schemes in a row.</p>
7771577715
<p>Therefore, we define $f[i][j]$ to represent the number of schemes in the first $i$ rows, where the coloring state of the $i$th row is $j$. The state $f[i][j]$ is transferred from $f[i - 1][k]$, where $k$ is the coloring state of the $i - 1$th row, and $k$ and $j$ meet the requirement of different colors being adjacent. That is:</p>
7771677716
<p>$$
77717-
f[i][j] = \sum_{k \in \text{valid}(j)} f[i - 1][k]
77717+
f[i][j] = \sum_{k \in \textit{valid}(j)} f[i - 1][k]
7771877718
$$</p>
77719-
<p>where $\text{valid}(j)$ represents all legal predecessor states of state $j$.</p>
77719+
<p>where $\textit{valid}(j)$ represents all legal predecessor states of state $j$.</p>
7772077720
<p>The final answer is the sum of $f[n][j]$, where $j$ is any legal state.</p>
7772177721
<p>We notice that $f[i][j]$ is only related to $f[i - 1][k]$, so we can use a rolling array to optimize the space complexity.</p>
7772277722
<p>The time complexity is $O((m + n) \times 3^{2m})$, and the space complexity is $O(3^m)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.</p>

en/lc/146/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77592,7 +77592,7 @@ <h3 id="solution-1-hash-table-doubly-linked-list">Solution 1: Hash Table + Doubl
7759277592
</ul>
7759377593
<p>When accessing a node, if the node exists, we delete it from its original position and reinsert it at the head of the list. This ensures that the node stored at the tail of the list is the least recently used node. When the number of nodes exceeds the maximum cache space, we eliminate the node at the tail of the list.</p>
7759477594
<p>When inserting a node, if the node exists, we delete it from its original position and reinsert it at the head of the list. If it does not exist, we first check if the cache is full. If it is full, we delete the node at the tail of the list and insert the new node at the head of the list.</p>
77595-
<p>The time complexity is $O(1)$, and the space complexity is $O(\text{capacity})$.</p>
77595+
<p>The time complexity is $O(1)$, and the space complexity is $O(\textit{capacity})$.</p>
7759677596
<div class="tabbed-set tabbed-alternate" data-tabs="1:7"><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" /><input id="__tabbed_1_7" 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><label for="__tabbed_1_7">C#</label></div>
7759777597
<div class="tabbed-content">
7759877598
<div class="tabbed-block">

0 commit comments

Comments
 (0)