Skip to content

Commit

Permalink
chore: update lc problems (doocs#3314)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Jul 24, 2024
1 parent d5d3ad8 commit 7840036
Show file tree
Hide file tree
Showing 291 changed files with 787 additions and 747 deletions.
6 changes: 3 additions & 3 deletions lcci/03.01.Three in One/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/03.01.Three%20in%20On

### 方法一:数组模拟

我们使用一个变量 $cap$ 来表示每个栈的大小,使用一个长度为 $3 \times \text{cap} + 3$ 的数组 $stk$ 来模拟三个栈,数组的前 $3 \times \text{cap}$ 个元素用来存储栈的元素,数组的后三个元素用来存储每个栈的元素个数。
我们使用一个变量 $cap$ 来表示每个栈的大小,使用一个长度为 $3 \times \textit{cap} + 3$ 的数组 $stk$ 来模拟三个栈,数组的前 $3 \times \textit{cap}$ 个元素用来存储栈的元素,数组的后三个元素用来存储每个栈的元素个数。

对于 `push` 操作,我们首先判断栈是否已满,如果未满,则将元素压入栈中,并更新栈的元素个数。

对于 `pop` 操作,我们首先判断栈是否为空,如果不为空,则更新栈的元素个数,并返回栈顶元素。

对于 `peek` 操作,我们首先判断栈是否为空,如果不为空,则返回栈顶元素。

对于 `isEmpty` 操作,我们直接判断栈是否为空即可。对于栈 $i$,我们只需要判断 $stk[\text{cap} \times 3 + i]$ 是否为 $0$ 即可。
对于 `isEmpty` 操作,我们直接判断栈是否为空即可。对于栈 $i$,我们只需要判断 $stk[\textit{cap} \times 3 + i]$ 是否为 $0$ 即可。

时间复杂度上,每个操作的时间复杂度均为 $O(1)$。空间复杂度为 $O(\text{cap})$,其中 $\text{cap}$ 为栈的大小。
时间复杂度上,每个操作的时间复杂度均为 $O(1)$。空间复杂度为 $O(\textit{cap})$,其中 $\textit{cap}$ 为栈的大小。

<!-- tabs:start -->

Expand Down
6 changes: 3 additions & 3 deletions lcci/03.01.Three in One/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/03.01.Three%20in%20On

### Solution 1: Array Simulation

We use a variable $cap$ to represent the size of each stack, and use an array $stk$ of length $3 \times \text{cap} + 3$ to simulate three stacks. The first $3 \times \text{cap}$ elements of the array are used to store the elements of the stack, and the last three elements are used to store the number of elements in each stack.
We use a variable $cap$ to represent the size of each stack, and use an array $stk$ of length $3 \times \textit{cap} + 3$ to simulate three stacks. The first $3 \times \textit{cap}$ elements of the array are used to store the elements of the stack, and the last three elements are used to store the number of elements in each stack.

For the `push` operation, we first check whether the stack is full. If it is not full, we push the element into the stack and update the number of elements in the stack.

For the `pop` operation, we first check whether the stack is empty. If it is not empty, we update the number of elements in the stack and return the top element of the stack.

For the `peek` operation, we first check whether the stack is empty. If it is not empty, we return the top element of the stack.

For the `isEmpty` operation, we directly check whether the stack is empty. For stack $i$, we only need to check whether $stk[\text{cap} \times 3 + i]$ is $0$.
For the `isEmpty` operation, we directly check whether the stack is empty. For stack $i$, we only need to check whether $stk[\textit{cap} \times 3 + i]$ is $0$.

In terms of time complexity, the time complexity of each operation is $O(1)$. The space complexity is $O(\text{cap})$, where $\text{cap}$ is the size of the stack.
In terms of time complexity, the time complexity of each operation is $O(1)$. The space complexity is $O(\textit{cap})$, where $\textit{cap}$ is the size of the stack.

<!-- tabs:start -->

Expand Down
8 changes: 4 additions & 4 deletions lcci/04.02.Minimum Height Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/04.02.Minimum%20Heigh

### 方法一:递归

我们设计一个函数 $\text{dfs}(l, r)$,表示构造出从 $l$ 到 $r$ 的子树,那么答案就是 $\text{dfs}(0, \text{len}(nums) - 1)$。
我们设计一个函数 $\textit{dfs}(l, r)$,表示构造出从 $l$ 到 $r$ 的子树,那么答案就是 $\textit{dfs}(0, \textit{len}(nums) - 1)$。

函数 $\text{dfs}(l, r)$ 的执行过程如下:
函数 $\textit{dfs}(l, r)$ 的执行过程如下:

1. 如果 $l > r$,返回 $\text{None}$。
2. 否则,我们计算出中间位置 $mid = \frac{l + r}{2}$,然后构造出根节点,左子树为 $\text{dfs}(l, mid - 1)$,右子树为 $\text{dfs}(mid + 1, r)$。
1. 如果 $l > r$,返回 $\textit{None}$。
2. 否则,我们计算出中间位置 $mid = \frac{l + r}{2}$,然后构造出根节点,左子树为 $\textit{dfs}(l, mid - 1)$,右子树为 $\textit{dfs}(mid + 1, r)$。
3. 最后返回根节点。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。
Expand Down
2 changes: 1 addition & 1 deletion lcci/04.05.Legal Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/04.05.Legal%20Binary%

我们可以对二叉树进行递归中序遍历,如果遍历到的结果是严格升序的,那么这棵树就是一个二叉搜索树。

因此,我们使用一个变量 $\textit{prev}$ 来保存上一个遍历到的节点,初始时 $\textit{prev} = -\infty$,然后我们递归遍历左子树,如果左子树不是二叉搜索树,直接返回 $\text{False}$,否则判断当前节点的值是否大于 $\textit{prev}$,如果不是,返回 $\text{False}$,否则更新 $\textit{prev}$ 为当前节点的值,然后递归遍历右子树。
因此,我们使用一个变量 $\textit{prev}$ 来保存上一个遍历到的节点,初始时 $\textit{prev} = -\infty$,然后我们递归遍历左子树,如果左子树不是二叉搜索树,直接返回 $\textit{False}$,否则判断当前节点的值是否大于 $\textit{prev}$,如果不是,返回 $\textit{False}$,否则更新 $\textit{prev}$ 为当前节点的值,然后递归遍历右子树。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。

Expand Down
8 changes: 4 additions & 4 deletions lcci/05.02.Binary Number to String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/05.02.Binary%20Number

$$
\begin{aligned}
0.8125 \times 2 &= 1.625 \quad \text{取整数部分} \quad 1 \\
0.625 \times 2 &= 1.25 \quad \text{取整数部分} \quad 1 \\
0.25 \times 2 &= 0.5 \quad \text{取整数部分} \quad 0 \\
0.5 \times 2 &= 1 \quad \text{取整数部分} \quad 1 \\
0.8125 \times 2 &= 1.625 \quad \textit{取整数部分} \quad 1 \\
0.625 \times 2 &= 1.25 \quad \textit{取整数部分} \quad 1 \\
0.25 \times 2 &= 0.5 \quad \textit{取整数部分} \quad 0 \\
0.5 \times 2 &= 1 \quad \textit{取整数部分} \quad 1 \\
\end{aligned}
$$

Expand Down
8 changes: 4 additions & 4 deletions lcci/05.02.Binary Number to String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ Let's take an example, suppose we want to convert $0.8125$ to a binary fraction,

$$
\begin{aligned}
0.8125 \times 2 &= 1.625 \quad \text{take the integer part} \quad 1 \\
0.625 \times 2 &= 1.25 \quad \text{take the integer part} \quad 1 \\
0.25 \times 2 &= 0.5 \quad \text{take the integer part} \quad 0 \\
0.5 \times 2 &= 1 \quad \text{take the integer part} \quad 1 \\
0.8125 \times 2 &= 1.625 \quad \textit{take the integer part} \quad 1 \\
0.625 \times 2 &= 1.25 \quad \textit{take the integer part} \quad 1 \\
0.25 \times 2 &= 0.5 \quad \textit{take the integer part} \quad 0 \\
0.5 \times 2 &= 1 \quad \textit{take the integer part} \quad 1 \\
\end{aligned}
$$

Expand Down
2 changes: 1 addition & 1 deletion lcci/05.07.Exchange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/05.07.Exchange/README

### 方法一:位运算

我们可以将 $\text{num}$ 与 $\text{0x55555555}$ 进行与运算,得到的结果是 $\text{num}$ 的偶数位,然后将其左移一位。再将 $\text{num}$ 与 $\text{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\text{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。
我们可以将 $\textit{num}$ 与 $\textit{0x55555555}$ 进行与运算,得到的结果是 $\textit{num}$ 的偶数位,然后将其左移一位。再将 $\textit{num}$ 与 $\textit{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\textit{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

Expand Down
6 changes: 3 additions & 3 deletions lcci/10.09.Sorted Matrix Search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ class Solution {

这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]``target` 的大小关系:

- 若 $\text{matrix}[i][j] = \text{target}$,说明找到了目标值,直接返回 `true`
- 若 $\text{matrix}[i][j] > \text{target}$,说明这一列从当前位置开始往上的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
- 若 $\text{matrix}[i][j] < \text{target}$,说明这一行从当前位置开始往右的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
- 若 $\textit{matrix}[i][j] = \textit{target}$,说明找到了目标值,直接返回 `true`
- 若 $\textit{matrix}[i][j] > \textit{target}$,说明这一列从当前位置开始往上的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
- 若 $\textit{matrix}[i][j] < \textit{target}$,说明这一行从当前位置开始往右的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。

若搜索结束依然找不到 `target`,返回 `false`

Expand Down
6 changes: 3 additions & 3 deletions lcci/10.09.Sorted Matrix Search/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ class Solution {

Here, we start searching from the bottom left corner and move towards the top right direction, comparing the current element `matrix[i][j]` with `target`:

- If $\text{matrix}[i][j] = \text{target}$, it means the target value has been found, and we directly return `true`.
- If $\text{matrix}[i][j] > \text{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
- If $\text{matrix}[i][j] < \text{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
- If $\textit{matrix}[i][j] = \textit{target}$, it means the target value has been found, and we directly return `true`.
- If $\textit{matrix}[i][j] > \textit{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
- If $\textit{matrix}[i][j] < \textit{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.

If the search ends and the `target` is still not found, return `false`.

Expand Down
2 changes: 1 addition & 1 deletion lcci/16.25.LRU Cache/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ When accessing a node, if the node exists, we delete it from its original positi

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.

The time complexity is $O(1)$, and the space complexity is $O(\text{capacity})$.
The time complexity is $O(1)$, and the space complexity is $O(\textit{capacity})$.

<!-- tabs:start -->

Expand Down
6 changes: 3 additions & 3 deletions lcci/17.20.Continuous Median/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ findMedian() -&gt; 2

### 方法一:大小根堆(优先队列)

我们可以使用两个堆来维护所有的元素,一个小根堆 $\text{minQ}$ 和一个大根堆 $\text{maxQ}$,其中小根堆 $\text{minQ}$ 存储较大的一半,大根堆 $\text{maxQ}$ 存储较小的一半。
我们可以使用两个堆来维护所有的元素,一个小根堆 $\textit{minQ}$ 和一个大根堆 $\textit{maxQ}$,其中小根堆 $\textit{minQ}$ 存储较大的一半,大根堆 $\textit{maxQ}$ 存储较小的一半。

调用 `addNum` 方法时,我们首先将元素加入到大根堆 $\text{maxQ}$,然后将 $\text{maxQ}$ 的堆顶元素弹出并加入到小根堆 $\text{minQ}$。如果此时 $\text{minQ}$ 的大小与 $\text{maxQ}$ 的大小差值大于 $1$,我们就将 $\text{minQ}$ 的堆顶元素弹出并加入到 $\text{maxQ}$。时间复杂度为 $O(\log n)$。
调用 `addNum` 方法时,我们首先将元素加入到大根堆 $\textit{maxQ}$,然后将 $\textit{maxQ}$ 的堆顶元素弹出并加入到小根堆 $\textit{minQ}$。如果此时 $\textit{minQ}$ 的大小与 $\textit{maxQ}$ 的大小差值大于 $1$,我们就将 $\textit{minQ}$ 的堆顶元素弹出并加入到 $\textit{maxQ}$。时间复杂度为 $O(\log n)$。

调用 `findMedian` 方法时,如果 $\text{minQ}$ 的大小等于 $\text{maxQ}$ 的大小,说明元素的总数为偶数,我们就可以返回 $\text{minQ}$ 的堆顶元素与 $\text{maxQ}$ 的堆顶元素的平均值;否则,我们返回 $\text{minQ}$ 的堆顶元素。时间复杂度为 $O(1)$。
调用 `findMedian` 方法时,如果 $\textit{minQ}$ 的大小等于 $\textit{maxQ}$ 的大小,说明元素的总数为偶数,我们就可以返回 $\textit{minQ}$ 的堆顶元素与 $\textit{maxQ}$ 的堆顶元素的平均值;否则,我们返回 $\textit{minQ}$ 的堆顶元素。时间复杂度为 $O(1)$。

空间复杂度为 $O(n)$。其中 $n$ 为元素的个数。

Expand Down
6 changes: 3 additions & 3 deletions lcci/17.20.Continuous Median/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ findMedian() -&gt; 2

### Solution 1: Min Heap and Max Heap (Priority Queue)

We can use two heaps to maintain all the elements, a min heap $\text{minQ}$ and a max heap $\text{maxQ}$, where the min heap $\text{minQ}$ stores the larger half, and the max heap $\text{maxQ}$ stores the smaller half.
We can use two heaps to maintain all the elements, a min heap $\textit{minQ}$ and a max heap $\textit{maxQ}$, where the min heap $\textit{minQ}$ stores the larger half, and the max heap $\textit{maxQ}$ stores the smaller half.

When calling the `addNum` method, we first add the element to the max heap $\text{maxQ}$, then pop the top element of $\text{maxQ}$ and add it to the min heap $\text{minQ}$. If at this time the size difference between $\text{minQ}$ and $\text{maxQ}$ is greater than $1$, we pop the top element of $\text{minQ}$ and add it to $\text{maxQ}$. The time complexity is $O(\log n)$.
When calling the `addNum` method, we first add the element to the max heap $\textit{maxQ}$, then pop the top element of $\textit{maxQ}$ and add it to the min heap $\textit{minQ}$. If at this time the size difference between $\textit{minQ}$ and $\textit{maxQ}$ is greater than $1$, we pop the top element of $\textit{minQ}$ and add it to $\textit{maxQ}$. The time complexity is $O(\log n)$.

When calling the `findMedian` method, if the size of $\text{minQ}$ is equal to the size of $\text{maxQ}$, it means the total number of elements is even, and we can return the average value of the top elements of $\text{minQ}$ and $\text{maxQ}$; otherwise, we return the top element of $\text{minQ}$. The time complexity is $O(1)$.
When calling the `findMedian` method, if the size of $\textit{minQ}$ is equal to the size of $\textit{maxQ}$, it means the total number of elements is even, and we can return the average value of the top elements of $\textit{minQ}$ and $\textit{maxQ}$; otherwise, we return the top element of $\textit{minQ}$. The time complexity is $O(1)$.

The space complexity is $O(n)$, where $n$ is the number of elements.

Expand Down
12 changes: 6 additions & 6 deletions lcci/17.22.Word Transformer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quo

我们定义一个答案数组 $\textit{ans}$,初始时只包含 $\textit{beginWord}$。然后我们定义一个数组 $\textit{vis}$,用来标记 $\textit{wordList}$ 中的单词是否被访问过。

接下来,我们设计一个函数 $\text{dfs}(s)$,表示从 $\textit{s}$ 出发,尝试将 $\textit{s}$ 转换为 $\textit{endWord}$,是否能够成功。如果能够成功,返回 $\text{True}$,否则返回 $\text{False}$。
接下来,我们设计一个函数 $\textit{dfs}(s)$,表示从 $\textit{s}$ 出发,尝试将 $\textit{s}$ 转换为 $\textit{endWord}$,是否能够成功。如果能够成功,返回 $\textit{True}$,否则返回 $\textit{False}$。

函数 $\text{dfs}(s)$ 的具体实现如下:
函数 $\textit{dfs}(s)$ 的具体实现如下:

1. 如果 $\textit{s}$ 等于 $\textit{endWord}$,说明转换成功,返回 $\text{True}$;
2. 否则,我们遍历 $\textit{wordList}$ 中的每个单词 $\textit{t}$,如果 $\textit{t}$ 没有被访问过且 $\textit{s}$ 和 $\textit{t}$ 之间只有一个字符不同,那么我们将 $\textit{t}$ 标记为已访问,并将 $\textit{t}$ 加入到 $\textit{ans}$ 中,然后递归调用 $\text{dfs}(t)$,如果返回 $\text{True}$,说明转换成功,我们返回 $\text{True}$,否则我们将 $\textit{t}$ 从 $\textit{ans}$ 中移除,继续遍历下一个单词;
3. 如果遍历完 $\textit{wordList}$ 中的所有单词都没有找到可以转换的单词,说明转换失败,我们返回 $\text{False}$。
1. 如果 $\textit{s}$ 等于 $\textit{endWord}$,说明转换成功,返回 $\textit{True}$;
2. 否则,我们遍历 $\textit{wordList}$ 中的每个单词 $\textit{t}$,如果 $\textit{t}$ 没有被访问过且 $\textit{s}$ 和 $\textit{t}$ 之间只有一个字符不同,那么我们将 $\textit{t}$ 标记为已访问,并将 $\textit{t}$ 加入到 $\textit{ans}$ 中,然后递归调用 $\textit{dfs}(t)$,如果返回 $\textit{True}$,说明转换成功,我们返回 $\textit{True}$,否则我们将 $\textit{t}$ 从 $\textit{ans}$ 中移除,继续遍历下一个单词;
3. 如果遍历完 $\textit{wordList}$ 中的所有单词都没有找到可以转换的单词,说明转换失败,我们返回 $\textit{False}$。

最后,我们调用 $\text{dfs}(\textit{beginWord})$,如果返回 $\text{True}$,说明转换成功,我们返回 $\textit{ans}$,否则返回空数组。
最后,我们调用 $\textit{dfs}(\textit{beginWord})$,如果返回 $\textit{True}$,说明转换成功,我们返回 $\textit{ans}$,否则返回空数组。

<!-- tabs:start -->

Expand Down
8 changes: 4 additions & 4 deletions lcci/17.23.Max Black Square/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/17.23.Max%20Black%20S

$$
down[i][j] = \begin{cases}
down[i + 1][j] + 1, & matrix[i][j] = 0 \text{ 且 } i + 1 < n \\
1, & matrix[i][j] = 0 \text{ 且 } i + 1 = n \\
down[i + 1][j] + 1, & matrix[i][j] = 0 \textit{ 且 } i + 1 < n \\
1, & matrix[i][j] = 0 \textit{ 且 } i + 1 = n \\
0, & matrix[i][j] = 1
\end{cases}
$$

$$
right[i][j] = \begin{cases}
right[i][j + 1] + 1, & matrix[i][j] = 0 \text{ 且 } j + 1 < n \\
1, & matrix[i][j] = 0 \text{ 且 } j + 1 = n \\
right[i][j + 1] + 1, & matrix[i][j] = 0 \textit{ 且 } j + 1 < n \\
1, & matrix[i][j] = 0 \textit{ 且 } j + 1 = n \\
0, & matrix[i][j] = 1
\end{cases}
$$
Expand Down
Loading

0 comments on commit 7840036

Please sign in to comment.