diff --git a/lcci/03.01.Three in One/README.md b/lcci/03.01.Three in One/README.md index e9e9bd2ee6f2c..3a2604f22caf0 100644 --- a/lcci/03.01.Three in One/README.md +++ b/lcci/03.01.Three in One/README.md @@ -47,7 +47,7 @@ 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` 操作,我们首先判断栈是否已满,如果未满,则将元素压入栈中,并更新栈的元素个数。 @@ -55,9 +55,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/03.01.Three%20in%20On 对于 `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}$ 为栈的大小。 diff --git a/lcci/03.01.Three in One/README_EN.md b/lcci/03.01.Three in One/README_EN.md index 61347236f71d8..e3a4801150493 100644 --- a/lcci/03.01.Three in One/README_EN.md +++ b/lcci/03.01.Three in One/README_EN.md @@ -62,7 +62,7 @@ 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. @@ -70,9 +70,9 @@ For the `pop` operation, we first check whether the stack is empty. If it is not 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. diff --git a/lcci/04.02.Minimum Height Tree/README.md b/lcci/04.02.Minimum Height Tree/README.md index dedb278968fca..3fccbc8f93760 100644 --- a/lcci/04.02.Minimum Height Tree/README.md +++ b/lcci/04.02.Minimum Height Tree/README.md @@ -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$ 为数组的长度。 diff --git a/lcci/04.05.Legal Binary Search Tree/README.md b/lcci/04.05.Legal Binary Search Tree/README.md index 8c738774f7d24..290f758b7d25f 100644 --- a/lcci/04.05.Legal Binary Search Tree/README.md +++ b/lcci/04.05.Legal Binary Search Tree/README.md @@ -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$ 是二叉树的节点个数。 diff --git a/lcci/05.02.Binary Number to String/README.md b/lcci/05.02.Binary Number to String/README.md index e65e49dbeebdc..2064ff9d442ee 100644 --- a/lcci/05.02.Binary Number to String/README.md +++ b/lcci/05.02.Binary Number to String/README.md @@ -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} $$ diff --git a/lcci/05.02.Binary Number to String/README_EN.md b/lcci/05.02.Binary Number to String/README_EN.md index c54d0c6cad2b3..39edca43b3d3b 100644 --- a/lcci/05.02.Binary Number to String/README_EN.md +++ b/lcci/05.02.Binary Number to String/README_EN.md @@ -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} $$ diff --git a/lcci/05.07.Exchange/README.md b/lcci/05.07.Exchange/README.md index ea45ce6241d41..a6c96240f2569 100644 --- a/lcci/05.07.Exchange/README.md +++ b/lcci/05.07.Exchange/README.md @@ -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)$。 diff --git a/lcci/10.09.Sorted Matrix Search/README.md b/lcci/10.09.Sorted Matrix Search/README.md index f1678a642d65f..0cf5d1ed6b3b8 100644 --- a/lcci/10.09.Sorted Matrix Search/README.md +++ b/lcci/10.09.Sorted Matrix Search/README.md @@ -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`。 diff --git a/lcci/10.09.Sorted Matrix Search/README_EN.md b/lcci/10.09.Sorted Matrix Search/README_EN.md index 737900835597c..9cf63c625e0c6 100644 --- a/lcci/10.09.Sorted Matrix Search/README_EN.md +++ b/lcci/10.09.Sorted Matrix Search/README_EN.md @@ -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`. diff --git a/lcci/16.25.LRU Cache/README_EN.md b/lcci/16.25.LRU Cache/README_EN.md index 615b6cc4fdf04..afedd5b11b2af 100644 --- a/lcci/16.25.LRU Cache/README_EN.md +++ b/lcci/16.25.LRU Cache/README_EN.md @@ -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})$. diff --git a/lcci/17.20.Continuous Median/README.md b/lcci/17.20.Continuous Median/README.md index dd85a2ce6a2a9..10ae1f356a397 100644 --- a/lcci/17.20.Continuous Median/README.md +++ b/lcci/17.20.Continuous Median/README.md @@ -48,11 +48,11 @@ findMedian() -> 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$ 为元素的个数。 diff --git a/lcci/17.20.Continuous Median/README_EN.md b/lcci/17.20.Continuous Median/README_EN.md index 132be30ebad8f..6c8ca71a339c8 100644 --- a/lcci/17.20.Continuous Median/README_EN.md +++ b/lcci/17.20.Continuous Median/README_EN.md @@ -55,11 +55,11 @@ findMedian() -> 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. diff --git a/lcci/17.22.Word Transformer/README.md b/lcci/17.22.Word Transformer/README.md index 3955d805a6aed..6d3d28ed495e4 100644 --- a/lcci/17.22.Word Transformer/README.md +++ b/lcci/17.22.Word Transformer/README.md @@ -50,15 +50,15 @@ wordList = ["hot","dot","dog","lot",&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}$,否则返回空数组。 diff --git a/lcci/17.23.Max Black Square/README.md b/lcci/17.23.Max Black Square/README.md index a109449e1845d..be1392c6bbfc4 100644 --- a/lcci/17.23.Max Black Square/README.md +++ b/lcci/17.23.Max Black Square/README.md @@ -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} $$ diff --git "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" index a098001e65705..d82afb7073ebb 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" @@ -54,15 +54,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9 ### 方法一:递归 -我们设计一个函数 $\text{dfs}(A, B)$,用于判断树 A 中以节点 A 为根节点的子树是否包含树 B。 +我们设计一个函数 $\textit{dfs}(A, B)$,用于判断树 A 中以节点 A 为根节点的子树是否包含树 B。 -函数 $\text{dfs}(A, B)$ 的执行步骤如下: +函数 $\textit{dfs}(A, B)$ 的执行步骤如下: 1. 如果树 B 为空,则树 B 是树 A 的子结构,返回 `true`; 2. 如果树 A 为空,或者树 A 的根节点的值不等于树 B 的根节点的值,则树 B 不是树 A 的子结构,返回 `false`; -3. 判断树 A 的左子树是否包含树 B,即调用 $\text{dfs}(A.left, B)$,并且判断树 A 的右子树是否包含树 B,即调用 $\text{dfs}(A.right, B)$。如果其中有一个函数返回 `false`,则树 B 不是树 A 的子结构,返回 `false`;否则,返回 `true`。 +3. 判断树 A 的左子树是否包含树 B,即调用 $\textit{dfs}(A.left, B)$,并且判断树 A 的右子树是否包含树 B,即调用 $\textit{dfs}(A.right, B)$。如果其中有一个函数返回 `false`,则树 B 不是树 A 的子结构,返回 `false`;否则,返回 `true`。 -在函数 `isSubStructure` 中,我们首先判断树 A 和树 B 是否为空,如果其中有一个为空,则树 B 不是树 A 的子结构,返回 `false`。然后,我们调用 $\text{dfs}(A, B)$,判断树 A 是否包含树 B。如果是,则返回 `true`;否则,递归判断树 A 的左子树是否包含树 B,以及树 A 的右子树是否包含树 B。如果其中有一个返回 `true`,则树 B 是树 A 的子结构,返回 `true`;否则,返回 `false`。 +在函数 `isSubStructure` 中,我们首先判断树 A 和树 B 是否为空,如果其中有一个为空,则树 B 不是树 A 的子结构,返回 `false`。然后,我们调用 $\textit{dfs}(A, B)$,判断树 A 是否包含树 B。如果是,则返回 `true`;否则,递归判断树 A 的左子树是否包含树 B,以及树 A 的右子树是否包含树 B。如果其中有一个返回 `true`,则树 B 是树 A 的子结构,返回 `true`;否则,返回 `false`。 时间复杂度 $O(n \times m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是树 A 和树 B 的节点个数。 diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" index 6b34dbdd86dc6..6e04b4fed4014 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" @@ -60,11 +60,11 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9 ### 方法一:大小根堆(优先队列) -我们可以使用两个堆来维护所有的元素,一个小根堆 $\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$ 为元素的个数。 diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 094. \346\234\200\345\260\221\345\233\236\346\226\207\345\210\206\345\211\262/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 094. \346\234\200\345\260\221\345\233\236\346\226\207\345\210\206\345\211\262/README.md" index 3147f7ba4e499..bb55fa65ac8e7 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 094. \346\234\200\345\260\221\345\233\236\346\226\207\345\210\206\345\211\262/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 094. \346\234\200\345\260\221\345\233\236\346\226\207\345\210\206\345\211\262/README.md" @@ -71,7 +71,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof2/%E5%89%91%E6%8C%87%2 接下来,我们考虑 $f[i]$ 如何进行状态转移。我们可以枚举上一个分割点 $j$,如果子串 $s[j..i]$ 是一个回文串,那么 $f[i]$ 就可以从 $f[j]$ 转移而来。如果 $j=0$,那么说明 $s[0..i]$ 本身就是一个回文串,此时不需要进行分割,即 $f[i]=0$。因此,状态转移方程如下: $$ -f[i]=\min_{0\leq j \leq i}\begin{cases} f[j-1]+1, & \text{if}\ g[j][i]=\text{True} \\ 0, & \text{if}\ g[0][i]=\text{True} \end{cases} +f[i]=\min_{0\leq j \leq i}\begin{cases} f[j-1]+1, & \textit{if}\ g[j][i]=\textit{True} \\ 0, & \textit{if}\ g[0][i]=\textit{True} \end{cases} $$ 答案即为 $f[n]$,其中 $n$ 是字符串 $s$ 的长度。 diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" index 1bdb9ae5b776e..7303593e9a389 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" @@ -304,13 +304,13 @@ public class Solution { $$ f[i][j] = \begin{cases} -f[i - 1][j] & \text{if } s_1[i - 1] = s_3[i + j - 1] \\ -\text{or } f[i][j - 1] & \text{if } s_2[j - 1] = s_3[i + j - 1] \\ -\text{false} & \text{otherwise} +f[i - 1][j] & \textit{if } s_1[i - 1] = s_3[i + j - 1] \\ +\textit{or } f[i][j - 1] & \textit{if } s_2[j - 1] = s_3[i + j - 1] \\ +\textit{false} & \textit{otherwise} \end{cases} $$ -其中 $f[0][0] = \text{true}$ 表示空串是两个空串的交错字符串。 +其中 $f[0][0] = \textit{true}$ 表示空串是两个空串的交错字符串。 答案即为 $f[m][n]$。 diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" index 805e968332fcc..cb03911942f59 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" @@ -85,7 +85,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcof2/%E5%89%91%E6%8C%87%2 $$ f[i][j] = \begin{cases} 1 & i = 0, j = 0 \\ -f[i - 1][j] + f[i][j - 1] & \text{otherwise} +f[i - 1][j] + f[i][j - 1] & \textit{otherwise} \end{cases} $$ diff --git a/solution/0000-0099/0032.Longest Valid Parentheses/README.md b/solution/0000-0099/0032.Longest Valid Parentheses/README.md index 6f29dbc123039..41b8be83eb9e6 100644 --- a/solution/0000-0099/0032.Longest Valid Parentheses/README.md +++ b/solution/0000-0099/0032.Longest Valid Parentheses/README.md @@ -77,9 +77,9 @@ tags: $$ \begin{cases} -f[i] = 0, & \text{if } s[i-1] = '(',\\ -f[i] = f[i-2] + 2, & \text{if } s[i-1] = ')' \text{ and } s[i-2] = '(',\\ -f[i] = f[i-1] + 2 + f[i-f[i-1]-2], & \text{if } s[i-1] = ')' \text{ and } s[i-2] = ')' \text{ and } s[i-f[i-1]-2] = '(',\\ +f[i] = 0, & \textit{if } s[i-1] = '(',\\ +f[i] = f[i-2] + 2, & \textit{if } s[i-1] = ')' \textit{ and } s[i-2] = '(',\\ +f[i] = f[i-1] + 2 + f[i-f[i-1]-2], & \textit{if } s[i-1] = ')' \textit{ and } s[i-2] = ')' \textit{ and } s[i-f[i-1]-2] = '(',\\ \end{cases} $$ diff --git a/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md b/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md index c6dba25b590ca..3b724ae45f680 100644 --- a/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md +++ b/solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md @@ -75,9 +75,9 @@ Therefore, we can get the state transition equation: $$ \begin{cases} -f[i] = 0, & \text{if } s[i-1] = '(',\\ -f[i] = f[i-2] + 2, & \text{if } s[i-1] = ')' \text{ and } s[i-2] = '(',\\ -f[i] = f[i-1] + 2 + f[i-f[i-1]-2], & \text{if } s[i-1] = ')' \text{ and } s[i-2] = ')' \text{ and } s[i-f[i-1]-2] = '(',\\ +f[i] = 0, & \textit{if } s[i-1] = '(',\\ +f[i] = f[i-2] + 2, & \textit{if } s[i-1] = ')' \textit{ and } s[i-2] = '(',\\ +f[i] = f[i-1] + 2 + f[i-f[i-1]-2], & \textit{if } s[i-1] = ')' \textit{ and } s[i-2] = ')' \textit{ and } s[i-f[i-1]-2] = '(',\\ \end{cases} $$ diff --git a/solution/0000-0099/0044.Wildcard Matching/README.md b/solution/0000-0099/0044.Wildcard Matching/README.md index af58b95316fb6..4307c23d0f1bc 100644 --- a/solution/0000-0099/0044.Wildcard Matching/README.md +++ b/solution/0000-0099/0044.Wildcard Matching/README.md @@ -79,8 +79,8 @@ tags: 函数 $dfs(i, j)$ 的执行过程如下: -- 如果 $i \geq \text{len}(s)$,那么只有当 $j \geq \text{len}(p)$ 或者 $p[j] = '*'$ 且 $dfs(i, j + 1)$ 为真时,$dfs(i, j)$ 才为真。 -- 如果 $j \geq \text{len}(p)$,那么 $dfs(i, j)$ 为假。 +- 如果 $i \geq \textit{len}(s)$,那么只有当 $j \geq \textit{len}(p)$ 或者 $p[j] = '*'$ 且 $dfs(i, j + 1)$ 为真时,$dfs(i, j)$ 才为真。 +- 如果 $j \geq \textit{len}(p)$,那么 $dfs(i, j)$ 为假。 - 如果 $p[j] = '*'$,那么 $dfs(i, j)$ 为真当且仅当 $dfs(i + 1, j)$ 或 $dfs(i + 1, j + 1)$ 或 $dfs(i, j + 1)$ 中有一个为真。 - 否则 $dfs(i, j)$ 为真当且仅当 $p[j] = '?'$ 或 $s[i] = p[j]$ 且 $dfs(i + 1, j + 1)$ 为真。 @@ -293,7 +293,7 @@ public class Solution { 我们可以将方法一中的记忆化搜索转换为动态规划。 -定义 $f[i][j]$ 表示字符串 $s$ 的前 $i$ 个字符和字符串 $p$ 的前 $j$ 个字符是否匹配。初始时 $f[0][0] = \text{true}$,表示两个空字符串是匹配的。对于 $j \in [1, n]$,如果 $p[j-1] = '*'$,那么 $f[0][j] = f[0][j-1]$。 +定义 $f[i][j]$ 表示字符串 $s$ 的前 $i$ 个字符和字符串 $p$ 的前 $j$ 个字符是否匹配。初始时 $f[0][0] = \textit{true}$,表示两个空字符串是匹配的。对于 $j \in [1, n]$,如果 $p[j-1] = '*'$,那么 $f[0][j] = f[0][j-1]$。 接下来我们考虑 $i \in [1, m]$ 和 $j \in [1, n]$ 的情况: diff --git a/solution/0000-0099/0044.Wildcard Matching/README_EN.md b/solution/0000-0099/0044.Wildcard Matching/README_EN.md index 4bce7a87a5922..a18cfe3de356c 100644 --- a/solution/0000-0099/0044.Wildcard Matching/README_EN.md +++ b/solution/0000-0099/0044.Wildcard Matching/README_EN.md @@ -74,8 +74,8 @@ We design a function $dfs(i, j)$, which represents whether the string $s$ starti The execution process of the function $dfs(i, j)$ is as follows: -- If $i \geq \text{len}(s)$, then $dfs(i, j)$ is true only when $j \geq \text{len}(p)$ or $p[j] = '*'$ and $dfs(i, j + 1)$ is true. -- If $j \geq \text{len}(p)$, then $dfs(i, j)$ is false. +- If $i \geq \textit{len}(s)$, then $dfs(i, j)$ is true only when $j \geq \textit{len}(p)$ or $p[j] = '*'$ and $dfs(i, j + 1)$ is true. +- If $j \geq \textit{len}(p)$, then $dfs(i, j)$ is false. - If $p[j] = '*'$, then $dfs(i, j)$ is true if and only if $dfs(i + 1, j)$ or $dfs(i + 1, j + 1)$ or $dfs(i, j + 1)$ is true. - Otherwise, $dfs(i, j)$ is true if and only if $p[j] = '?'$ or $s[i] = p[j]$ and $dfs(i + 1, j + 1)$ is true. @@ -288,7 +288,7 @@ public class Solution { We can convert the memoization search in Solution 1 into dynamic programming. -Define $f[i][j]$ to represent whether the first $i$ characters of string $s$ match the first $j$ characters of string $p$. Initially, $f[0][0] = \text{true}$, indicating that two empty strings are matching. For $j \in [1, n]$, if $p[j-1] = '*'$, then $f[0][j] = f[0][j-1]$. +Define $f[i][j]$ to represent whether the first $i$ characters of string $s$ match the first $j$ characters of string $p$. Initially, $f[0][0] = \textit{true}$, indicating that two empty strings are matching. For $j \in [1, n]$, if $p[j-1] = '*'$, then $f[0][j] = f[0][j-1]$. Next, we consider the case of $i \in [1, m]$ and $j \in [1, n]$: diff --git a/solution/0000-0099/0062.Unique Paths/README.md b/solution/0000-0099/0062.Unique Paths/README.md index 5f4413661b48a..d8c583dcb020e 100644 --- a/solution/0000-0099/0062.Unique Paths/README.md +++ b/solution/0000-0099/0062.Unique Paths/README.md @@ -86,7 +86,7 @@ tags: $$ f[i][j] = \begin{cases} 1 & i = 0, j = 0 \\ -f[i - 1][j] + f[i][j - 1] & \text{otherwise} +f[i - 1][j] + f[i][j - 1] & \textit{otherwise} \end{cases} $$ diff --git a/solution/0000-0099/0062.Unique Paths/README_EN.md b/solution/0000-0099/0062.Unique Paths/README_EN.md index 2c2321c934c29..f276f8bc01d14 100644 --- a/solution/0000-0099/0062.Unique Paths/README_EN.md +++ b/solution/0000-0099/0062.Unique Paths/README_EN.md @@ -70,7 +70,7 @@ Therefore, we have the following state transition equation: $$ f[i][j] = \begin{cases} 1 & i = 0, j = 0 \\ -f[i - 1][j] + f[i][j - 1] & \text{otherwise} +f[i - 1][j] + f[i][j - 1] & \textit{otherwise} \end{cases} $$ diff --git a/solution/0000-0099/0072.Edit Distance/README.md b/solution/0000-0099/0072.Edit Distance/README.md index 404004165ea29..757c2199f9a93 100644 --- a/solution/0000-0099/0072.Edit Distance/README.md +++ b/solution/0000-0099/0072.Edit Distance/README.md @@ -81,10 +81,10 @@ exection -> execution (插入 'u') $$ f[i][j] = \begin{cases} -i, & \text{if } j = 0 \\ -j, & \text{if } i = 0 \\ -f[i - 1][j - 1], & \text{if } word1[i - 1] = word2[j - 1] \\ -\min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1, & \text{otherwise} +i, & \textit{if } j = 0 \\ +j, & \textit{if } i = 0 \\ +f[i - 1][j - 1], & \textit{if } word1[i - 1] = word2[j - 1] \\ +\min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1, & \textit{otherwise} \end{cases} $$ diff --git a/solution/0000-0099/0072.Edit Distance/README_EN.md b/solution/0000-0099/0072.Edit Distance/README_EN.md index cbac236faf701..41321ff893864 100644 --- a/solution/0000-0099/0072.Edit Distance/README_EN.md +++ b/solution/0000-0099/0072.Edit Distance/README_EN.md @@ -79,10 +79,10 @@ Finally, we can get the state transition equation: $$ f[i][j] = \begin{cases} -i, & \text{if } j = 0 \\ -j, & \text{if } i = 0 \\ -f[i - 1][j - 1], & \text{if } word1[i - 1] = word2[j - 1] \\ -\min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1, & \text{otherwise} +i, & \textit{if } j = 0 \\ +j, & \textit{if } i = 0 \\ +f[i - 1][j - 1], & \textit{if } word1[i - 1] = word2[j - 1] \\ +\min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1, & \textit{otherwise} \end{cases} $$ diff --git a/solution/0000-0099/0097.Interleaving String/README.md b/solution/0000-0099/0097.Interleaving String/README.md index 564d3cc41af10..805e8315d4e0f 100644 --- a/solution/0000-0099/0097.Interleaving String/README.md +++ b/solution/0000-0099/0097.Interleaving String/README.md @@ -377,13 +377,13 @@ public class Solution { $$ f[i][j] = \begin{cases} -f[i - 1][j] & \text{if } s_1[i - 1] = s_3[i + j - 1] \\ -\text{or } f[i][j - 1] & \text{if } s_2[j - 1] = s_3[i + j - 1] \\ -\text{false} & \text{otherwise} +f[i - 1][j] & \textit{if } s_1[i - 1] = s_3[i + j - 1] \\ +\textit{or } f[i][j - 1] & \textit{if } s_2[j - 1] = s_3[i + j - 1] \\ +\textit{false} & \textit{otherwise} \end{cases} $$ -其中 $f[0][0] = \text{true}$ 表示空串是两个空串的交错字符串。 +其中 $f[0][0] = \textit{true}$ 表示空串是两个空串的交错字符串。 答案即为 $f[m][n]$。 diff --git a/solution/0000-0099/0097.Interleaving String/README_EN.md b/solution/0000-0099/0097.Interleaving String/README_EN.md index eb48eb39c68a2..93801d1703bcf 100644 --- a/solution/0000-0099/0097.Interleaving String/README_EN.md +++ b/solution/0000-0099/0097.Interleaving String/README_EN.md @@ -379,13 +379,13 @@ We define $f[i][j]$ to represent whether the first $i$ characters of string $s_1 $$ f[i][j] = \begin{cases} -f[i - 1][j] & \text{if } s_1[i - 1] = s_3[i + j - 1] \\ -\text{or } f[i][j - 1] & \text{if } s_2[j - 1] = s_3[i + j - 1] \\ -\text{false} & \text{otherwise} +f[i - 1][j] & \textit{if } s_1[i - 1] = s_3[i + j - 1] \\ +\textit{or } f[i][j - 1] & \textit{if } s_2[j - 1] = s_3[i + j - 1] \\ +\textit{false} & \textit{otherwise} \end{cases} $$ -where $f[0][0] = \text{true}$ indicates that an empty string is an interleaving string of two empty strings. +where $f[0][0] = \textit{true}$ indicates that an empty string is an interleaving string of two empty strings. The answer is $f[m][n]$. diff --git a/solution/0000-0099/0098.Validate Binary Search Tree/README.md b/solution/0000-0099/0098.Validate Binary Search Tree/README.md index 129b61bafaef8..0f2ca49cb6046 100644 --- a/solution/0000-0099/0098.Validate Binary Search Tree/README.md +++ b/solution/0000-0099/0098.Validate Binary Search Tree/README.md @@ -65,7 +65,7 @@ tags: 我们可以对二叉树进行递归中序遍历,如果遍历到的结果是严格升序的,那么这棵树就是一个二叉搜索树。 -因此,我们使用一个变量 $\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$ 是二叉树的节点个数。 diff --git a/solution/0100-0199/0132.Palindrome Partitioning II/README.md b/solution/0100-0199/0132.Palindrome Partitioning II/README.md index 90bde0e1186f3..6c79e69452a67 100644 --- a/solution/0100-0199/0132.Palindrome Partitioning II/README.md +++ b/solution/0100-0199/0132.Palindrome Partitioning II/README.md @@ -73,7 +73,7 @@ tags: 接下来,我们考虑 $f[i]$ 如何进行状态转移。我们可以枚举上一个分割点 $j$,如果子串 $s[j..i]$ 是一个回文串,那么 $f[i]$ 就可以从 $f[j]$ 转移而来。如果 $j=0$,那么说明 $s[0..i]$ 本身就是一个回文串,此时不需要进行分割,即 $f[i]=0$。因此,状态转移方程如下: $$ -f[i]=\min_{0\leq j \leq i}\begin{cases} f[j-1]+1, & \text{if}\ g[j][i]=\text{True} \\ 0, & \text{if}\ g[0][i]=\text{True} \end{cases} +f[i]=\min_{0\leq j \leq i}\begin{cases} f[j-1]+1, & \textit{if}\ g[j][i]=\textit{True} \\ 0, & \textit{if}\ g[0][i]=\textit{True} \end{cases} $$ 答案即为 $f[n]$,其中 $n$ 是字符串 $s$ 的长度。 diff --git a/solution/0100-0199/0146.LRU Cache/README.md b/solution/0100-0199/0146.LRU Cache/README.md index ce93f6c338ef2..7bb35b178741e 100644 --- a/solution/0100-0199/0146.LRU Cache/README.md +++ b/solution/0100-0199/0146.LRU Cache/README.md @@ -87,7 +87,7 @@ lRUCache.get(4); // 返回 4 当插入一个节点时,如果节点存在,我们将其从原来的位置删除,并重新插入到链表头部。如果不存在,我们首先检查缓存是否已满,如果已满,则删除链表尾部的节点,将新的节点插入链表头部。 -时间复杂度 $O(1)$,空间复杂度 $O(\text{capacity})$。 +时间复杂度 $O(1)$,空间复杂度 $O(\textit{capacity})$。 diff --git a/solution/0100-0199/0146.LRU Cache/README_EN.md b/solution/0100-0199/0146.LRU Cache/README_EN.md index 719886f5ead4c..c3dbdaea5a858 100644 --- a/solution/0100-0199/0146.LRU Cache/README_EN.md +++ b/solution/0100-0199/0146.LRU Cache/README_EN.md @@ -81,7 +81,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})$. diff --git a/solution/0200-0299/0215.Kth Largest Element in an Array/README.md b/solution/0200-0299/0215.Kth Largest Element in an Array/README.md index c38a44c965194..9e8137ed7f22b 100644 --- a/solution/0200-0299/0215.Kth Largest Element in an Array/README.md +++ b/solution/0200-0299/0215.Kth Largest Element in an Array/README.md @@ -60,7 +60,7 @@ tags: 快速选择算法是一种在未排序的数组中查找第 `k` 个最大元素或最小元素的算法。它的基本思想是每次选择一个基准元素,将数组分为两部分,一部分的元素都比基准元素小,另一部分的元素都比基准元素大,然后根据基准元素的位置,决定继续在左边还是右边查找,直到找到第 `k` 个最大元素。 -时间复杂度 $O(n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -283,9 +283,9 @@ impl Solution { ### 方法二:优先队列(小根堆) -我们可以维护一个大小为 $k$ 的小根堆 $\text{minQ}$,然后遍历数组 $\text{nums}$,将数组中的元素依次加入到小根堆中,当小根堆的大小超过 $k$ 时,我们将堆顶元素弹出,这样最终小根堆中的 $k$ 个元素就是数组中的 $k$ 个最大元素,堆顶元素就是第 $k$ 个最大元素。 +我们可以维护一个大小为 $k$ 的小根堆 $\textit{minQ}$,然后遍历数组 $\textit{nums}$,将数组中的元素依次加入到小根堆中,当小根堆的大小超过 $k$ 时,我们将堆顶元素弹出,这样最终小根堆中的 $k$ 个元素就是数组中的 $k$ 个最大元素,堆顶元素就是第 $k$ 个最大元素。 -时间复杂度 $O(n\log k)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n\log k)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -400,9 +400,9 @@ impl Solution { ### 方法三:计数排序 -我们可以使用计数排序的思想,统计数组 $\text{nums}$ 中每个元素出现的次数,记录在哈希表 $\text{cnt}$ 中,然后从大到小遍历元素 $i$,每次减去出现的次数 $\text{cnt}[i]$,直到 $k$ 小于等于 $0$,此时的元素 $i$ 就是数组中的第 $k$ 个最大元素。 +我们可以使用计数排序的思想,统计数组 $\textit{nums}$ 中每个元素出现的次数,记录在哈希表 $\textit{cnt}$ 中,然后从大到小遍历元素 $i$,每次减去出现的次数 $\textit{cnt}[i]$,直到 $k$ 小于等于 $0$,此时的元素 $i$ 就是数组中的第 $k$ 个最大元素。 -时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度,而 $m$ 为数组 $\text{nums}$ 中元素的最大值。 +时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $m$ 为数组 $\textit{nums}$ 中元素的最大值。 diff --git a/solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md b/solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md index 9eecbd3277365..19297f374806e 100644 --- a/solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md +++ b/solution/0200-0299/0215.Kth Largest Element in an Array/README_EN.md @@ -52,7 +52,7 @@ tags: Quick Select is an algorithm for finding the $k^{th}$ largest or smallest element in an unsorted array. Its basic idea is to select a pivot element each time, dividing the array into two parts: one part contains elements smaller than the pivot, and the other part contains elements larger than the pivot. Then, based on the position of the pivot, it decides whether to continue the search on the left or right side until the $k^{th}$ largest element is found. -The time complexity is $O(n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\text{nums}$. +The time complexity is $O(n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -275,9 +275,9 @@ impl Solution { ### Solution 2: Priority Queue (Min Heap) -We can maintain a min heap $\text{minQ}$ of size $k$, and then iterate through the array $\text{nums}$, adding each element to the min heap. When the size of the min heap exceeds $k$, we pop the top element of the heap. This way, the final $k$ elements in the min heap are the $k$ largest elements in the array, and the top element of the heap is the $k^{th}$ largest element. +We can maintain a min heap $\textit{minQ}$ of size $k$, and then iterate through the array $\textit{nums}$, adding each element to the min heap. When the size of the min heap exceeds $k$, we pop the top element of the heap. This way, the final $k$ elements in the min heap are the $k$ largest elements in the array, and the top element of the heap is the $k^{th}$ largest element. -The time complexity is $O(n\log k)$, and the space complexity is $O(k)$. Here, $n$ is the length of the array $\text{nums}$. +The time complexity is $O(n\log k)$, and the space complexity is $O(k)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -392,9 +392,9 @@ impl Solution { ### Solution 3: Counting Sort -We can use the idea of counting sort, counting the occurrence of each element in the array $\text{nums}$ and recording it in a hash table $\text{cnt}$. Then, we iterate over the elements $i$ from largest to smallest, subtracting the occurrence count $\text{cnt}[i]$ each time, until $k$ is less than or equal to $0$. At this point, the element $i$ is the $k^{th}$ largest element in the array. +We can use the idea of counting sort, counting the occurrence of each element in the array $\textit{nums}$ and recording it in a hash table $\textit{cnt}$. Then, we iterate over the elements $i$ from largest to smallest, subtracting the occurrence count $\textit{cnt}[i]$ each time, until $k$ is less than or equal to $0$. At this point, the element $i$ is the $k^{th}$ largest element in the array. -The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\text{nums}$, and $m$ is the maximum value among the elements in $\text{nums}$. +The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$, and $m$ is the maximum value among the elements in $\textit{nums}$. diff --git a/solution/0200-0299/0221.Maximal Square/README.md b/solution/0200-0299/0221.Maximal Square/README.md index 908e41eea3d34..808e67027d021 100644 --- a/solution/0200-0299/0221.Maximal Square/README.md +++ b/solution/0200-0299/0221.Maximal Square/README.md @@ -69,8 +69,8 @@ tags: $$ dp[i + 1][j + 1] = \begin{cases} -0 & \text{if } matrix[i][j] = '0' \\ -\min(dp[i][j], dp[i][j + 1], dp[i + 1][j]) + 1 & \text{if } matrix[i][j] = '1' +0 & \textit{if } matrix[i][j] = '0' \\ +\min(dp[i][j], dp[i][j + 1], dp[i + 1][j]) + 1 & \textit{if } matrix[i][j] = '1' \end{cases} $$ diff --git a/solution/0200-0299/0221.Maximal Square/README_EN.md b/solution/0200-0299/0221.Maximal Square/README_EN.md index 0c7a2f3e01338..26665820aa503 100644 --- a/solution/0200-0299/0221.Maximal Square/README_EN.md +++ b/solution/0200-0299/0221.Maximal Square/README_EN.md @@ -67,8 +67,8 @@ The state transition equation is: $$ dp[i + 1][j + 1] = \begin{cases} -0 & \text{if } matrix[i][j] = '0' \\ -\min(dp[i][j], dp[i][j + 1], dp[i + 1][j]) + 1 & \text{if } matrix[i][j] = '1' +0 & \textit{if } matrix[i][j] = '0' \\ +\min(dp[i][j], dp[i][j + 1], dp[i + 1][j]) + 1 & \textit{if } matrix[i][j] = '1' \end{cases} $$ diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/README.md b/solution/0200-0299/0240.Search a 2D Matrix II/README.md index 278faf4bff982..70b13fc1bb898 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/README.md +++ b/solution/0200-0299/0240.Search a 2D Matrix II/README.md @@ -239,9 +239,9 @@ public 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`。 diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md b/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md index 1f1f8a46be5ef..2c2559f1266e7 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md +++ b/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md @@ -237,9 +237,9 @@ public 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`. diff --git a/solution/0200-0299/0270.Closest Binary Search Tree Value/README.md b/solution/0200-0299/0270.Closest Binary Search Tree Value/README.md index 38a99a6093a7a..8dc6f44ceb1c8 100644 --- a/solution/0200-0299/0270.Closest Binary Search Tree Value/README.md +++ b/solution/0200-0299/0270.Closest Binary Search Tree Value/README.md @@ -56,7 +56,7 @@ tags: ### 方法一:递归 -我们定义一个递归函数 $\text{dfs}(node)$,表示从当前节点 $node$ 开始,寻找最接近目标值 $target$ 的节点。我们可以通过比较当前节点的值与目标值的差的绝对值,来更新答案,如果目标值小于当前节点的值,我们就递归地搜索左子树,否则我们递归地搜索右子树。 +我们定义一个递归函数 $\textit{dfs}(node)$,表示从当前节点 $node$ 开始,寻找最接近目标值 $target$ 的节点。我们可以通过比较当前节点的值与目标值的差的绝对值,来更新答案,如果目标值小于当前节点的值,我们就递归地搜索左子树,否则我们递归地搜索右子树。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点数。 diff --git a/solution/0200-0299/0295.Find Median from Data Stream/README.md b/solution/0200-0299/0295.Find Median from Data Stream/README.md index e2121ad724561..5ac081b9a13b0 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/README.md +++ b/solution/0200-0299/0295.Find Median from Data Stream/README.md @@ -74,11 +74,11 @@ medianFinder.findMedian(); // return 2.0 ### 方法一:大小根堆(优先队列) -我们可以使用两个堆来维护所有的元素,一个小根堆 $\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$ 为元素的个数。 diff --git a/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md b/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md index c2787a62043d6..8f0d3a8b7cf01 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md +++ b/solution/0200-0299/0295.Find Median from Data Stream/README_EN.md @@ -79,11 +79,11 @@ medianFinder.findMedian(); // return 2.0 ### 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. diff --git a/solution/0300-0399/0338.Counting Bits/README.md b/solution/0300-0399/0338.Counting Bits/README.md index 9a81e7663965e..7e7fa93ad1cda 100644 --- a/solution/0300-0399/0338.Counting Bits/README.md +++ b/solution/0300-0399/0338.Counting Bits/README.md @@ -161,7 +161,7 @@ function bitCount(n: number): number { 我们定义一个长度为 $n+1$ 的答案数组 $ans$,初始时 $ans[0]=0$。 -对于 $1 \leq i \leq n$,我们有 $ans[i] = ans[i \text{ and } (i-1)] + 1$。其中 $i \text{ and } (i-1)$ 表示将 $i$ 的二进制表示中的最低位 $1$ 变成 $0$ 之后的数,显然 $i \text{ and } (i-1) < i$,且 $ans[i \text{ and } (i-1)]$ 已经被计算出来了,我们就能以 $O(1)$ 的时间得到 $ans[i]$。 +对于 $1 \leq i \leq n$,我们有 $ans[i] = ans[i \textit{ and } (i-1)] + 1$。其中 $i \textit{ and } (i-1)$ 表示将 $i$ 的二进制表示中的最低位 $1$ 变成 $0$ 之后的数,显然 $i \textit{ and } (i-1) < i$,且 $ans[i \textit{ and } (i-1)]$ 已经被计算出来了,我们就能以 $O(1)$ 的时间得到 $ans[i]$。 时间复杂度 $O(n)$,空间复杂度 $O(1)$。 diff --git a/solution/0300-0399/0347.Top K Frequent Elements/README.md b/solution/0300-0399/0347.Top K Frequent Elements/README.md index 0a21210a0545e..0ce1273eec34c 100644 --- a/solution/0300-0399/0347.Top K Frequent Elements/README.md +++ b/solution/0300-0399/0347.Top K Frequent Elements/README.md @@ -62,7 +62,7 @@ tags: ### 方法一:哈希表 + 优先队列(小根堆) -我们可以使用一个哈希表 $\text{cnt}$ 统计每个元素出现的次数,然后使用一个小根堆(优先队列)来保存前 $k$ 个高频元素。 +我们可以使用一个哈希表 $\textit{cnt}$ 统计每个元素出现的次数,然后使用一个小根堆(优先队列)来保存前 $k$ 个高频元素。 我们首先遍历一遍数组,统计每个元素出现的次数,然后遍历哈希表,将元素和出现次数存入小根堆中。如果小根堆的大小超过了 $k$,我们就将堆顶元素弹出,保证堆的大小始终为 $k$。 diff --git a/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md b/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md index c72b5148eb7d4..77a311335c84d 100644 --- a/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md +++ b/solution/0300-0399/0347.Top K Frequent Elements/README_EN.md @@ -54,7 +54,7 @@ tags: ### Solution 1: Hash Table + Priority Queue (Min Heap) -We can use a hash table $\text{cnt}$ to count the occurrence of each element, and then use a min heap (priority queue) to store the top $k$ frequent elements. +We can use a hash table $\textit{cnt}$ to count the occurrence of each element, and then use a min heap (priority queue) to store the top $k$ frequent elements. First, we traverse the array once to count the occurrence of each element. Then, we iterate through the hash table, storing each element and its count into the min heap. If the size of the min heap exceeds $k$, we pop the top element of the heap to ensure the heap size is always $k$. diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md index 4e7a31d0fa6ea..704fdabdeed81 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README.md @@ -64,11 +64,11 @@ tags: ### 方法一:哈希表 -我们可以用一个哈希表 $\text{cnt}$ 统计数组 $\text{nums1}$ 中每个元素出现的次数,然后遍历数组 $\text{nums2}$,如果元素 $x$ 在 $\text{cnt}$ 中,并且 $x$ 的出现次数大于 $0$,那么将 $x$ 加入答案,然后将 $x$ 的出现次数减一。 +我们可以用一个哈希表 $\textit{cnt}$ 统计数组 $\textit{nums1}$ 中每个元素出现的次数,然后遍历数组 $\textit{nums2}$,如果元素 $x$ 在 $\textit{cnt}$ 中,并且 $x$ 的出现次数大于 $0$,那么将 $x$ 加入答案,然后将 $x$ 的出现次数减一。 遍历结束后,返回答案数组即可。 -时间复杂度 $O(m + n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $\text{nums1}$ 和 $\text{nums2}$ 的长度。 +时间复杂度 $O(m + n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的长度。 diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md index cbbb237e372a1..6f88a14107803 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/README_EN.md @@ -63,11 +63,11 @@ tags: ### Solution 1: Hash Table -We can use a hash table $\text{cnt}$ to count the occurrences of each element in the array $\text{nums1}$. Then, we iterate through the array $\text{nums2}$. If an element $x$ is in $\text{cnt}$ and the occurrence of $x$ is greater than $0$, we add $x$ to the answer and then decrement the occurrence of $x$ by one. +We can use a hash table $\textit{cnt}$ to count the occurrences of each element in the array $\textit{nums1}$. Then, we iterate through the array $\textit{nums2}$. If an element $x$ is in $\textit{cnt}$ and the occurrence of $x$ is greater than $0$, we add $x$ to the answer and then decrement the occurrence of $x$ by one. After the iteration is finished, we return the answer array. -The time complexity is $O(m + n)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the lengths of the arrays $\text{nums1}$ and $\text{nums2}$, respectively. +The time complexity is $O(m + n)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the lengths of the arrays $\textit{nums1}$ and $\textit{nums2}$, respectively. diff --git a/solution/0300-0399/0364.Nested List Weight Sum II/README.md b/solution/0300-0399/0364.Nested List Weight Sum II/README.md index 863445b2ff5ee..b211f0779c411 100644 --- a/solution/0300-0399/0364.Nested List Weight Sum II/README.md +++ b/solution/0300-0399/0364.Nested List Weight Sum II/README.md @@ -64,31 +64,31 @@ tags: ### 方法一:DFS -我们不妨假设整数分别为 $a_1, a_2, \cdots, a_n$,它们的深度分别为 $d_1, d_2, \cdots, d_n$,最大深度为 $\text{maxDepth}$,那么答案就是: +我们不妨假设整数分别为 $a_1, a_2, \cdots, a_n$,它们的深度分别为 $d_1, d_2, \cdots, d_n$,最大深度为 $\textit{maxDepth}$,那么答案就是: $$ -a_1 \times \text{maxDepth} - a_1 \times d_1 + a_1 + a_2 \times \text{maxDepth} - a_2 \times d_2 + a_2 + \cdots + a_n \times \text{maxDepth} - a_n \times d_n + a_n +a_1 \times \textit{maxDepth} - a_1 \times d_1 + a_1 + a_2 \times \textit{maxDepth} - a_2 \times d_2 + a_2 + \cdots + a_n \times \textit{maxDepth} - a_n \times d_n + a_n $$ 即: $$ -(\text{maxDepth} + 1) \times (a_1 + a_2 + \cdots + a_n) - (a_1 \times d_1 + a_2 \times d_2 + \cdots + a_n \times d_n) +(\textit{maxDepth} + 1) \times (a_1 + a_2 + \cdots + a_n) - (a_1 \times d_1 + a_2 \times d_2 + \cdots + a_n \times d_n) $$ 如果我们记所有整数的和为 $s$,所有整数乘以深度的和为 $ws$,那么答案就是: $$ -(\text{maxDepth} + 1) \times s - ws +(\textit{maxDepth} + 1) \times s - ws $$ 因此,我们设计一个函数 $dfs(x, d)$,表示从 $x$ 开始,深度为 $d$ 开始搜索,函数 $dfs(x, d)$ 的执行过程如下: -- 我们先更新 $\text{maxDepth} = \max(\text{maxDepth}, d)$; +- 我们先更新 $\textit{maxDepth} = \max(\textit{maxDepth}, d)$; - 如果 $x$ 是一个整数,那么我们更新 $s = s + x$, $ws = ws + x \times d$; - 否则,我们递归地遍历 $x$ 的每一个元素 $y$,调用 $dfs(y, d + 1)$。 -我们遍历整个列表,对于每一个元素 $x$,我们调用 $dfs(x, 1)$,最终返回 $(\text{maxDepth} + 1) \times s - ws$ 即可。 +我们遍历整个列表,对于每一个元素 $x$,我们调用 $dfs(x, 1)$,最终返回 $(\textit{maxDepth} + 1) \times s - ws$ 即可。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为整数的个数。 diff --git a/solution/0300-0399/0364.Nested List Weight Sum II/README_EN.md b/solution/0300-0399/0364.Nested List Weight Sum II/README_EN.md index cb1f774da5472..4ca8f0301878d 100644 --- a/solution/0300-0399/0364.Nested List Weight Sum II/README_EN.md +++ b/solution/0300-0399/0364.Nested List Weight Sum II/README_EN.md @@ -62,31 +62,31 @@ tags: ### Solution 1: DFS -Let's assume the integers are $a_1, a_2, \cdots, a_n$, their depths are $d_1, d_2, \cdots, d_n$, the maximum depth is $\text{maxDepth}$, then the answer is: +Let's assume the integers are $a_1, a_2, \cdots, a_n$, their depths are $d_1, d_2, \cdots, d_n$, the maximum depth is $\textit{maxDepth}$, then the answer is: $$ -a_1 \times \text{maxDepth} - a_1 \times d_1 + a_1 + a_2 \times \text{maxDepth} - a_2 \times d_2 + a_2 + \cdots + a_n \times \text{maxDepth} - a_n \times d_n + a_n +a_1 \times \textit{maxDepth} - a_1 \times d_1 + a_1 + a_2 \times \textit{maxDepth} - a_2 \times d_2 + a_2 + \cdots + a_n \times \textit{maxDepth} - a_n \times d_n + a_n $$ which is: $$ -(\text{maxDepth} + 1) \times (a_1 + a_2 + \cdots + a_n) - (a_1 \times d_1 + a_2 \times d_2 + \cdots + a_n \times d_n) +(\textit{maxDepth} + 1) \times (a_1 + a_2 + \cdots + a_n) - (a_1 \times d_1 + a_2 \times d_2 + \cdots + a_n \times d_n) $$ If we denote the sum of all integers as $s$, and the sum of each integer multiplied by its depth as $ws$, then the answer is: $$ -(\text{maxDepth} + 1) \times s - ws +(\textit{maxDepth} + 1) \times s - ws $$ Therefore, we design a function $dfs(x, d)$, which starts searching from $x$ with depth $d$. The execution process of $dfs(x, d)$ is as follows: -- We first update $\text{maxDepth} = \max(\text{maxDepth}, d)$; +- We first update $\textit{maxDepth} = \max(\textit{maxDepth}, d)$; - If $x$ is an integer, then we update $s = s + x$, $ws = ws + x \times d$; - Otherwise, we recursively traverse each element $y$ of $x$, and call $dfs(y, d + 1)$. -We traverse the entire list, for each element $x$, we call $dfs(x, 1)$, and finally return $(\text{maxDepth} + 1) \times s - ws$. +We traverse the entire list, for each element $x$, we call $dfs(x, 1)$, and finally return $(\textit{maxDepth} + 1) \times s - ws$. The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of integers. diff --git a/solution/0300-0399/0365.Water and Jug Problem/README.md b/solution/0300-0399/0365.Water and Jug Problem/README.md index 47f5ea7fa8c23..7cc5f30f350c6 100644 --- a/solution/0300-0399/0365.Water and Jug Problem/README.md +++ b/solution/0300-0399/0365.Water and Jug Problem/README.md @@ -76,7 +76,7 @@ tags: ### 方法一:DFS -我们不妨记 $\text{jug1Capacity}$ 为 $x$, $\text{jug2Capacity}$ 为 $y$, $\text{targetCapacity}$ 为 $z$。 +我们不妨记 $\textit{jug1Capacity}$ 为 $x$, $\textit{jug2Capacity}$ 为 $y$, $\textit{targetCapacity}$ 为 $z$。 接下来,我们设计一个函数 $dfs(i, j)$,表示当前 $jug1$ 中有 $i$ 升水,$jug2$ 中有 $j$ 升水,是否可以得到 $z$ 升水。 @@ -89,7 +89,7 @@ tags: 答案即为 $dfs(0, 0)$。 -时间复杂度 $O(x + y)$,空间复杂度 $O(x + y)$。其中 $x$ 和 $y$ 分别为 $\text{jug1Capacity}$ 和 $\text{jug2Capacity}$。 +时间复杂度 $O(x + y)$,空间复杂度 $O(x + y)$。其中 $x$ 和 $y$ 分别为 $\textit{jug1Capacity}$ 和 $\textit{jug2Capacity}$。 diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md index 11b26eb1296c2..fd4551f364ff0 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README.md @@ -60,7 +60,7 @@ tags: 考虑 $f[i][j]$,如果我们选取了第 $i$ 个数 $x$,那么 $f[i][j] = f[i - 1][j - x]$;如果我们没有选取第 $i$ 个数 $x$,那么 $f[i][j] = f[i - 1][j]$。因此状态转移方程为: $$ -f[i][j] = f[i - 1][j] \text{ or } f[i - 1][j - x] \text{ if } j \geq x +f[i][j] = f[i - 1][j] \textit{ or } f[i - 1][j - x] \textit{ if } j \geq x $$ 最终答案为 $f[n][m]$。 diff --git a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md index 1138a5a641ab5..1b1a01a59566d 100644 --- a/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md +++ b/solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md @@ -59,7 +59,7 @@ We define $f[i][j]$ to represent whether it is possible to select several number Considering $f[i][j]$, if we select the $i$-th number $x$, then $f[i][j] = f[i - 1][j - x]$. If we do not select the $i$-th number $x$, then $f[i][j] = f[i - 1][j]$. Therefore, the state transition equation is: $$ -f[i][j] = f[i - 1][j] \text{ or } f[i - 1][j - x] \text{ if } j \geq x +f[i][j] = f[i - 1][j] \textit{ or } f[i - 1][j - x] \textit{ if } j \geq x $$ The final answer is $f[n][m]$. diff --git a/solution/0400-0499/0464.Can I Win/README.md b/solution/0400-0499/0464.Can I Win/README.md index 7e65d5c648c85..d73ab18ab788a 100644 --- a/solution/0400-0499/0464.Can I Win/README.md +++ b/solution/0400-0499/0464.Can I Win/README.md @@ -77,9 +77,9 @@ tags: 我们首先判断可以选择的所有整数的和是否小于目标值,如果是,说明无论如何都无法赢,直接返回 `false`。 -然后,我们设计一个函数 $\text{dfs}(mask, s)$,其中 `mask` 表示当前已选择的整数的状态,`s` 表示当前的累计和。函数返回值为当前玩家是否能赢。 +然后,我们设计一个函数 $\textit{dfs}(mask, s)$,其中 `mask` 表示当前已选择的整数的状态,`s` 表示当前的累计和。函数返回值为当前玩家是否能赢。 -函数 $\text{dfs}(mask, s)$ 的执行过程如下: +函数 $\textit{dfs}(mask, s)$ 的执行过程如下: 我们遍历 $1$ 到 $maxChoosableInteger$ 中的每个整数 $i$,如果 $i$ 还没有被选择,我们可以选择 $i$,如果选择 $i$ 后的累计和 $s + i$ 大于等于目标值 `desiredTotal`,或者对手选择 $i$ 后的结果是输的,那么当前玩家就是赢的,返回 `true`。 diff --git a/solution/0400-0499/0476.Number Complement/README.md b/solution/0400-0499/0476.Number Complement/README.md index ef94361032a12..152830c396a89 100644 --- a/solution/0400-0499/0476.Number Complement/README.md +++ b/solution/0400-0499/0476.Number Complement/README.md @@ -67,13 +67,13 @@ tags: 根据题目描述,我们可以通过异或运算来实现取反的操作,步骤如下: -我们首先找到 $\text{num}$ 的二进制表示中最高位的 $1$,位置记为 $k$。 +我们首先找到 $\textit{num}$ 的二进制表示中最高位的 $1$,位置记为 $k$。 然后,构造一个二进制数,第 $k$ 位为 $0$,其余低位为 $1$,即 $2^k - 1$; -最后,将 $\text{num}$ 与上述构造的二进制数进行异或运算,即可得到答案。 +最后,将 $\textit{num}$ 与上述构造的二进制数进行异或运算,即可得到答案。 -时间复杂度 $O(\log \text{num})$,其中 $\text{num}$ 为输入的整数。空间复杂度 $O(1)$。 +时间复杂度 $O(\log \textit{num})$,其中 $\textit{num}$ 为输入的整数。空间复杂度 $O(1)$。 diff --git a/solution/0400-0499/0476.Number Complement/README_EN.md b/solution/0400-0499/0476.Number Complement/README_EN.md index ea786621d6a48..5e9d87c4bc1d5 100644 --- a/solution/0400-0499/0476.Number Complement/README_EN.md +++ b/solution/0400-0499/0476.Number Complement/README_EN.md @@ -61,13 +61,13 @@ tags: According to the problem description, we can use XOR operation to implement the flipping operation, the steps are as follows: -First, we find the highest bit of $1$ in the binary representation of $\text{num}$, and the position is denoted as $k$. +First, we find the highest bit of $1$ in the binary representation of $\textit{num}$, and the position is denoted as $k$. Then, we construct a binary number, where the $k$-th bit is $0$ and the rest of the lower bits are $1$, which is $2^k - 1$; -Finally, we perform XOR operation on $\text{num}$ and the constructed binary number to get the answer. +Finally, we perform XOR operation on $\textit{num}$ and the constructed binary number to get the answer. -The time complexity is $O(\log \text{num})$, where $\text{num}$ is the input integer. The space complexity is $O(1)$. +The time complexity is $O(\log \textit{num})$, where $\textit{num}$ is the input integer. The space complexity is $O(1)$. diff --git a/solution/0400-0499/0494.Target Sum/README.md b/solution/0400-0499/0494.Target Sum/README.md index 79680b9b0202b..3308936b2552d 100644 --- a/solution/0400-0499/0494.Target Sum/README.md +++ b/solution/0400-0499/0494.Target Sum/README.md @@ -69,27 +69,27 @@ tags: ### 方法一:动态规划 -我们记数组 $\text{nums}$ 所有元素的和为 $s$,添加负号的元素之和为 $x$,则添加正号的元素之和为 $s - x$,则有: +我们记数组 $\textit{nums}$ 所有元素的和为 $s$,添加负号的元素之和为 $x$,则添加正号的元素之和为 $s - x$,则有: $$ -(s - x) - x = \text{target} \Rightarrow x = \frac{s - \text{target}}{2} +(s - x) - x = \textit{target} \Rightarrow x = \frac{s - \textit{target}}{2} $$ -由于 $x \geq 0$,且 $x$ 为整数,所以 $s \geq \text{target}$ 且 $s - \text{target}$ 为偶数。如果不满足这两个条件,则直接返回 $0$。 +由于 $x \geq 0$,且 $x$ 为整数,所以 $s \geq \textit{target}$ 且 $s - \textit{target}$ 为偶数。如果不满足这两个条件,则直接返回 $0$。 -接下来,我们可以将问题转化为:在数组 $\text{nums}$ 中选取若干元素,使得这些元素之和等于 $\frac{s - \text{target}}{2}$,问有多少种选取方法。 +接下来,我们可以将问题转化为:在数组 $\textit{nums}$ 中选取若干元素,使得这些元素之和等于 $\frac{s - \textit{target}}{2}$,问有多少种选取方法。 -我们可以使用动态规划来解决这个问题。定义 $f[i][j]$ 表示在数组 $\text{nums}$ 的前 $i$ 个元素中选取若干元素,使得这些元素之和等于 $j$ 的选取方案数。 +我们可以使用动态规划来解决这个问题。定义 $f[i][j]$ 表示在数组 $\textit{nums}$ 的前 $i$ 个元素中选取若干元素,使得这些元素之和等于 $j$ 的选取方案数。 -对于 $\text{nums}[i - 1]$,我们有两种选择:选取或不选取。如果我们不选取 $\text{nums}[i - 1]$,则 $f[i][j] = f[i - 1][j]$;如果我们选取 $\text{nums}[i - 1]$,则 $f[i][j] = f[i - 1][j - \text{nums}[i - 1]]$。因此,状态转移方程为: +对于 $\textit{nums}[i - 1]$,我们有两种选择:选取或不选取。如果我们不选取 $\textit{nums}[i - 1]$,则 $f[i][j] = f[i - 1][j]$;如果我们选取 $\textit{nums}[i - 1]$,则 $f[i][j] = f[i - 1][j - \textit{nums}[i - 1]]$。因此,状态转移方程为: $$ -f[i][j] = f[i - 1][j] + f[i - 1][j - \text{nums}[i - 1]] +f[i][j] = f[i - 1][j] + f[i - 1][j - \textit{nums}[i - 1]] $$ -其中,选取的前提是 $j \geq \text{nums}[i - 1]$。 +其中,选取的前提是 $j \geq \textit{nums}[i - 1]$。 -最终答案即为 $f[m][n]$。其中 $m$ 为数组 $\text{nums}$ 的长度,而 $n = \frac{s - \text{target}}{2}$。 +最终答案即为 $f[m][n]$。其中 $m$ 为数组 $\textit{nums}$ 的长度,而 $n = \frac{s - \textit{target}}{2}$。 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。 @@ -282,7 +282,7 @@ var findTargetSumWays = function (nums, target) { ### 方法二:动态规划(空间优化) -我们可以发现,方法一中的状态转移方程中,$f[i][j]$ 的值只和 $f[i - 1][j]$ 以及 $f[i - 1][j - \text{nums}[i - 1]]$ 有关,因此我们去掉第一维空间,只使用一维数组即可。 +我们可以发现,方法一中的状态转移方程中,$f[i][j]$ 的值只和 $f[i - 1][j]$ 以及 $f[i - 1][j - \textit{nums}[i - 1]]$ 有关,因此我们去掉第一维空间,只使用一维数组即可。 时间复杂度 $O(m \times n)$,空间复杂度 $O(n)$。 diff --git a/solution/0400-0499/0494.Target Sum/README_EN.md b/solution/0400-0499/0494.Target Sum/README_EN.md index 776a50a943237..b8d7be99b24d4 100644 --- a/solution/0400-0499/0494.Target Sum/README_EN.md +++ b/solution/0400-0499/0494.Target Sum/README_EN.md @@ -67,27 +67,27 @@ tags: ### Solution 1: Dynamic Programming -Let's denote the sum of all elements in the array $\text{nums}$ as $s$, and the sum of elements to which we assign a negative sign as $x$. Therefore, the sum of elements with a positive sign is $s - x$. We have: +Let's denote the sum of all elements in the array $\textit{nums}$ as $s$, and the sum of elements to which we assign a negative sign as $x$. Therefore, the sum of elements with a positive sign is $s - x$. We have: $$ -(s - x) - x = \text{target} \Rightarrow x = \frac{s - \text{target}}{2} +(s - x) - x = \textit{target} \Rightarrow x = \frac{s - \textit{target}}{2} $$ -Since $x \geq 0$ and $x$ must be an integer, it follows that $s \geq \text{target}$ and $s - \text{target}$ must be even. If these two conditions are not met, we directly return $0$. +Since $x \geq 0$ and $x$ must be an integer, it follows that $s \geq \textit{target}$ and $s - \textit{target}$ must be even. If these two conditions are not met, we directly return $0$. -Next, we can transform the problem into: selecting several elements from the array $\text{nums}$ such that the sum of these elements equals $\frac{s - \text{target}}{2}$. We are asked how many ways there are to make such a selection. +Next, we can transform the problem into: selecting several elements from the array $\textit{nums}$ such that the sum of these elements equals $\frac{s - \textit{target}}{2}$. We are asked how many ways there are to make such a selection. -We can use dynamic programming to solve this problem. Define $f[i][j]$ as the number of ways to select several elements from the first $i$ elements of the array $\text{nums}$ such that the sum of these elements equals $j$. +We can use dynamic programming to solve this problem. Define $f[i][j]$ as the number of ways to select several elements from the first $i$ elements of the array $\textit{nums}$ such that the sum of these elements equals $j$. -For $\text{nums}[i - 1]$, we have two choices: to select or not to select. If we do not select $\text{nums}[i - 1]$, then $f[i][j] = f[i - 1][j]$; if we do select $\text{nums}[i - 1]$, then $f[i][j] = f[i - 1][j - \text{nums}[i - 1]]$. Therefore, the state transition equation is: +For $\textit{nums}[i - 1]$, we have two choices: to select or not to select. If we do not select $\textit{nums}[i - 1]$, then $f[i][j] = f[i - 1][j]$; if we do select $\textit{nums}[i - 1]$, then $f[i][j] = f[i - 1][j - \textit{nums}[i - 1]]$. Therefore, the state transition equation is: $$ -f[i][j] = f[i - 1][j] + f[i - 1][j - \text{nums}[i - 1]] +f[i][j] = f[i - 1][j] + f[i - 1][j - \textit{nums}[i - 1]] $$ -This selection is based on the premise that $j \geq \text{nums}[i - 1]$. +This selection is based on the premise that $j \geq \textit{nums}[i - 1]$. -The final answer is $f[m][n]$, where $m$ is the length of the array $\text{nums}$, and $n = \frac{s - \text{target}}{2}$. +The final answer is $f[m][n]$, where $m$ is the length of the array $\textit{nums}$, and $n = \frac{s - \textit{target}}{2}$. The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. @@ -280,7 +280,7 @@ var findTargetSumWays = function (nums, target) { ### Solution 2: Dynamic Programming (Space Optimization) -We can observe that in the state transition equation of Solution 1, the value of $f[i][j]$ is only related to $f[i - 1][j]$ and $f[i - 1][j - \text{nums}[i - 1]]$. Therefore, we can eliminate the first dimension of the space and use only a one-dimensional array. +We can observe that in the state transition equation of Solution 1, the value of $f[i][j]$ is only related to $f[i - 1][j]$ and $f[i - 1][j - \textit{nums}[i - 1]]$. Therefore, we can eliminate the first dimension of the space and use only a one-dimensional array. The time complexity is $O(m \times n)$, and the space complexity is $O(n)$. diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/README.md b/solution/0500-0599/0510.Inorder Successor in BST II/README.md index 4bc1701d3fa7e..dc93a079a1459 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/README.md +++ b/solution/0500-0599/0510.Inorder Successor in BST II/README.md @@ -76,9 +76,9 @@ class Node { ### 方法一:分情况讨论 -如果 $\text{node}$ 有右子树,那么 $\text{node}$ 的中序后继节点是右子树中最左边的节点。 +如果 $\textit{node}$ 有右子树,那么 $\textit{node}$ 的中序后继节点是右子树中最左边的节点。 -如果 $\text{node}$ 没有右子树,那么如果 $\text{node}$ 是其父节点的右子树,我们就一直向上搜索,直到节点的父节点为空,或者节点是其父节点的左子树,此时父节点就是中序后继节点。 +如果 $\textit{node}$ 没有右子树,那么如果 $\textit{node}$ 是其父节点的右子树,我们就一直向上搜索,直到节点的父节点为空,或者节点是其父节点的左子树,此时父节点就是中序后继节点。 时间复杂度 $O(h)$,其中 $h$ 是二叉树的高度。空间复杂度 $O(1)$。 diff --git a/solution/0500-0599/0514.Freedom Trail/README.md b/solution/0500-0599/0514.Freedom Trail/README.md index cbf5121cede7e..a4e424c4d1f77 100644 --- a/solution/0500-0599/0514.Freedom Trail/README.md +++ b/solution/0500-0599/0514.Freedom Trail/README.md @@ -81,7 +81,7 @@ tags: 我们可以先初始化 $f[0][j]$,其中 $j$ 是字符 $key[0]$ 在 $ring$ 中出现的位置。由于 $ring$ 的第 $j$ 个字符与 $12:00$ 方向对齐,因此我们只需要 $1$ 步即可拼写出 $key[0]$。此外,我们还需要 $min(j, n - j)$ 步将 $ring$ 旋转到 $12:00$ 方向。因此 $f[0][j]=min(j, n - j) + 1$。 -接下来,我们考虑当 $i \geq 1$ 时,状态如何转移。我们可以枚举 $key[i]$ 在 $ring$ 中的位置列表 $pos[key[i]]$,并枚举 $key[i-1]$ 在 $ring$ 中的位置列表 $pos[key[i-1]]$,然后更新 $f[i][j]$,即 $f[i][j]=\min_{k \in pos[key[i-1]]} f[i-1][k] + \min(\text{abs}(j - k), n - \text{abs}(j - k)) + 1$。 +接下来,我们考虑当 $i \geq 1$ 时,状态如何转移。我们可以枚举 $key[i]$ 在 $ring$ 中的位置列表 $pos[key[i]]$,并枚举 $key[i-1]$ 在 $ring$ 中的位置列表 $pos[key[i-1]]$,然后更新 $f[i][j]$,即 $f[i][j]=\min_{k \in pos[key[i-1]]} f[i-1][k] + \min(\textit{abs}(j - k), n - \textit{abs}(j - k)) + 1$。 最后,我们返回 $\min_{0 \leq j \lt n} f[m - 1][j]$ 即可。 diff --git a/solution/0500-0599/0514.Freedom Trail/README_EN.md b/solution/0500-0599/0514.Freedom Trail/README_EN.md index fa2703896e2d1..c71e89f108e0d 100644 --- a/solution/0500-0599/0514.Freedom Trail/README_EN.md +++ b/solution/0500-0599/0514.Freedom Trail/README_EN.md @@ -75,7 +75,7 @@ Then we define $f[i][j]$ as the minimum number of steps to spell the first $i+1$ We can first initialize $f[0][j]$, where $j$ is the position where the character $key[0]$ appears in $ring$. Since the $j$-th character of $ring$ is aligned with the $12:00$ direction, we only need $1$ step to spell $key[0]$. In addition, we need $min(j, n - j)$ steps to rotate $ring$ to the $12:00$ direction. Therefore, $f[0][j]=min(j, n - j) + 1$. -Next, we consider how the state transitions when $i \geq 1$. We can enumerate the position list $pos[key[i]]$ where $key[i]$ appears in $ring$, and enumerate the position list $pos[key[i-1]]$ where $key[i-1]$ appears in $ring$, and then update $f[i][j]$, i.e., $f[i][j]=\min_{k \in pos[key[i-1]]} f[i-1][k] + \min(\text{abs}(j - k), n - \text{abs}(j - k)) + 1$. +Next, we consider how the state transitions when $i \geq 1$. We can enumerate the position list $pos[key[i]]$ where $key[i]$ appears in $ring$, and enumerate the position list $pos[key[i-1]]$ where $key[i-1]$ appears in $ring$, and then update $f[i][j]$, i.e., $f[i][j]=\min_{k \in pos[key[i-1]]} f[i-1][k] + \min(\textit{abs}(j - k), n - \textit{abs}(j - k)) + 1$. Finally, we return $\min_{0 \leq j \lt n} f[m - 1][j]$. diff --git a/solution/0500-0599/0517.Super Washing Machines/README.md b/solution/0500-0599/0517.Super Washing Machines/README.md index 059d32586a40b..f393ab044ed55 100644 --- a/solution/0500-0599/0517.Super Washing Machines/README.md +++ b/solution/0500-0599/0517.Super Washing Machines/README.md @@ -77,7 +77,7 @@ tags: 否则,假设洗衣机内的衣服总数为 $s$,那么最终每台洗衣机内的衣服数量都会变为 $k = s / n$。 -我们定义 $a_i$ 为第 $i$ 台洗衣机内的衣服数量与 $k$ 的差值,即 $a_i = \text{machines}[i] - k$。若 $a_i > 0$,则表示第 $i$ 台洗衣机内有多余的衣服,需要向相邻的洗衣机传递;若 $a_i < 0$,则表示第 $i$ 台洗衣机内缺少衣服,需要从相邻的洗衣机获得。 +我们定义 $a_i$ 为第 $i$ 台洗衣机内的衣服数量与 $k$ 的差值,即 $a_i = \textit{machines}[i] - k$。若 $a_i > 0$,则表示第 $i$ 台洗衣机内有多余的衣服,需要向相邻的洗衣机传递;若 $a_i < 0$,则表示第 $i$ 台洗衣机内缺少衣服,需要从相邻的洗衣机获得。 我们将前 $i$ 台洗衣机的衣服数量差值之和定义为 $s_i = \sum_{j=0}^{i-1} a_j$,如果把前 $i$ 台洗衣机视为一组,其余的洗衣机视为另一组。那么若 $s_i$ 为正数,表示第一组洗衣机内有多余的衣服,需要向第二组洗衣机传递;若 $s_i$ 为负数,表示第一组洗衣机内缺少衣服,需要从第二组洗衣机获得。 diff --git a/solution/0500-0599/0517.Super Washing Machines/README_EN.md b/solution/0500-0599/0517.Super Washing Machines/README_EN.md index 3952e5cc518ca..2977a8a153c0e 100644 --- a/solution/0500-0599/0517.Super Washing Machines/README_EN.md +++ b/solution/0500-0599/0517.Super Washing Machines/README_EN.md @@ -75,7 +75,7 @@ If the total number of clothes in the washing machines cannot be divided evenly Otherwise, suppose the total number of clothes in the washing machines is $s$, then the number of clothes in each washing machine will eventually become $k = s / n$. -We define $a_i$ as the difference between the number of clothes in the $i$-th washing machine and $k$, that is, $a_i = \text{machines}[i] - k$. If $a_i > 0$, it means that the $i$-th washing machine has extra clothes and needs to pass them to the adjacent washing machine; if $a_i < 0$, it means that the $i$-th washing machine lacks clothes and needs to get them from the adjacent washing machine. +We define $a_i$ as the difference between the number of clothes in the $i$-th washing machine and $k$, that is, $a_i = \textit{machines}[i] - k$. If $a_i > 0$, it means that the $i$-th washing machine has extra clothes and needs to pass them to the adjacent washing machine; if $a_i < 0$, it means that the $i$-th washing machine lacks clothes and needs to get them from the adjacent washing machine. We define the sum of the differences in the number of clothes in the first $i$ washing machines as $s_i = \sum_{j=0}^{i-1} a_j$. If we regard the first $i$ washing machines as one group and the remaining washing machines as another group. Then if $s_i$ is a positive number, it means that the first group of washing machines has extra clothes and needs to pass them to the second group of washing machines; if $s_i$ is a negative number, it means that the first group of washing machines lacks clothes and needs to get them from the second group of washing machines. diff --git a/solution/0500-0599/0523.Continuous Subarray Sum/README.md b/solution/0500-0599/0523.Continuous Subarray Sum/README.md index 5fa616e1e822c..6b96ec4b64700 100644 --- a/solution/0500-0599/0523.Continuous Subarray Sum/README.md +++ b/solution/0500-0599/0523.Continuous Subarray Sum/README.md @@ -79,15 +79,15 @@ tags: ### 方法一:前缀和 + 哈希表 -根据题目描述,如果存在两个前缀和模 $k$ 的余数相同的位置 $i$ 和 $j$(不妨设 $j < i$),那么 $\text{nums}[j+1..i]$ 这个子数组的和是 $k$ 的倍数。 +根据题目描述,如果存在两个前缀和模 $k$ 的余数相同的位置 $i$ 和 $j$(不妨设 $j < i$),那么 $\textit{nums}[j+1..i]$ 这个子数组的和是 $k$ 的倍数。 因此,我们可以使用哈希表存储每个前缀和模 $k$ 的余数第一次出现的位置。初始时,我们在哈希表中存入一对键值对 $(0, -1)$,表示前缀和为 $0$ 的余数 $0$ 出现在位置 $-1$。 -遍历数组时,我们计算当前前缀和的模 $k$ 的余数,如果当前前缀和的模 $k$ 的余数没有在哈希表中出现过,我们就将当前前缀和的模 $k$ 的余数和对应的位置存入哈希表中。否则,如果当前前缀和的模 $k$ 的余数在哈希表中已经出现过,位置为 $j$,那么我们就找到了一个满足条件的子数组 $\text{nums}[j+1..i]$,因此返回 $\text{True}$。 +遍历数组时,我们计算当前前缀和的模 $k$ 的余数,如果当前前缀和的模 $k$ 的余数没有在哈希表中出现过,我们就将当前前缀和的模 $k$ 的余数和对应的位置存入哈希表中。否则,如果当前前缀和的模 $k$ 的余数在哈希表中已经出现过,位置为 $j$,那么我们就找到了一个满足条件的子数组 $\textit{nums}[j+1..i]$,因此返回 $\textit{True}$。 -遍历结束后,如果没有找到满足条件的子数组,我们返回 $\text{False}$。 +遍历结束后,如果没有找到满足条件的子数组,我们返回 $\textit{False}$。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 diff --git a/solution/0500-0599/0523.Continuous Subarray Sum/README_EN.md b/solution/0500-0599/0523.Continuous Subarray Sum/README_EN.md index 00c540a86a7a5..049ef24bff658 100644 --- a/solution/0500-0599/0523.Continuous Subarray Sum/README_EN.md +++ b/solution/0500-0599/0523.Continuous Subarray Sum/README_EN.md @@ -78,15 +78,15 @@ tags: ### Solution 1: Prefix Sum + Hash Table -According to the problem description, if there exist two positions $i$ and $j$ ($j < i$) where the remainders of the prefix sums modulo $k$ are the same, then the sum of the subarray $\text{nums}[j+1..i]$ is a multiple of $k$. +According to the problem description, if there exist two positions $i$ and $j$ ($j < i$) where the remainders of the prefix sums modulo $k$ are the same, then the sum of the subarray $\textit{nums}[j+1..i]$ is a multiple of $k$. Therefore, we can use a hash table to store the first occurrence of each remainder of the prefix sum modulo $k$. Initially, we store a key-value pair $(0, -1)$ in the hash table, indicating that the remainder $0$ of the prefix sum $0$ appears at position $-1$. -As we iterate through the array, we calculate the current prefix sum's remainder modulo $k$. If the current prefix sum's remainder modulo $k$ has not appeared in the hash table, we store the current prefix sum's remainder modulo $k$ and its corresponding position in the hash table. Otherwise, if the current prefix sum's remainder modulo $k$ has already appeared in the hash table at position $j$, then we have found a subarray $\text{nums}[j+1..i]$ that meets the conditions, and thus return $\text{True}$. +As we iterate through the array, we calculate the current prefix sum's remainder modulo $k$. If the current prefix sum's remainder modulo $k$ has not appeared in the hash table, we store the current prefix sum's remainder modulo $k$ and its corresponding position in the hash table. Otherwise, if the current prefix sum's remainder modulo $k$ has already appeared in the hash table at position $j$, then we have found a subarray $\textit{nums}[j+1..i]$ that meets the conditions, and thus return $\textit{True}$. -After completing the iteration, if no subarray meeting the conditions is found, we return $\text{False}$. +After completing the iteration, if no subarray meeting the conditions is found, we return $\textit{False}$. -The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. diff --git a/solution/0500-0599/0560.Subarray Sum Equals K/README.md b/solution/0500-0599/0560.Subarray Sum Equals K/README.md index 0112346d84f67..a49ab117c3b80 100644 --- a/solution/0500-0599/0560.Subarray Sum Equals K/README.md +++ b/solution/0500-0599/0560.Subarray Sum Equals K/README.md @@ -56,13 +56,13 @@ tags: ### 方法一:哈希表 + 前缀和 -我们定义一个哈希表 $\text{cnt}$,用于存储数组 $\text{nums}$ 的前缀和出现的次数。初始时,我们将 $\text{cnt}[0]$ 的值设为 $1$,表示前缀和 $0$ 出现了一次。 +我们定义一个哈希表 $\textit{cnt}$,用于存储数组 $\textit{nums}$ 的前缀和出现的次数。初始时,我们将 $\textit{cnt}[0]$ 的值设为 $1$,表示前缀和 $0$ 出现了一次。 -我们遍历数组 $\text{nums}$,计算前缀和 $\text{s}$,然后将 $\text{cnt}[s - k]$ 的值累加到答案中,并将 $\text{cnt}[s]$ 的值增加 $1$。 +我们遍历数组 $\textit{nums}$,计算前缀和 $\textit{s}$,然后将 $\textit{cnt}[s - k]$ 的值累加到答案中,并将 $\textit{cnt}[s]$ 的值增加 $1$。 遍历结束后,我们返回答案。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md index f6cc7e8ee7e05..8336433ad0550 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md @@ -67,9 +67,9 @@ tags: ### 方法一:哈希表 -我们可以用一个哈希表 $\text{cnt}$ 记录数组 $\text{nums}$ 中每个元素出现的次数,然后遍历哈希表中的每个键值对 $(x, c)$,如果哈希表中存在键 $x + 1$,那么 $\text{nums}$ 中元素 $x$ 和 $x + 1$ 出现的次数之和 $c + \text{cnt}[x + 1]$ 就是一个和谐子序列,我们只需要在所有和谐子序列中找到最大的长度即可。 +我们可以用一个哈希表 $\textit{cnt}$ 记录数组 $\textit{nums}$ 中每个元素出现的次数,然后遍历哈希表中的每个键值对 $(x, c)$,如果哈希表中存在键 $x + 1$,那么 $\textit{nums}$ 中元素 $x$ 和 $x + 1$ 出现的次数之和 $c + \textit{cnt}[x + 1]$ 就是一个和谐子序列,我们只需要在所有和谐子序列中找到最大的长度即可。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md b/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md index d57222ade3e83..b257e33ab7df9 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md @@ -80,9 +80,9 @@ tags: ### Solution 1: Hash Table -We can use a hash table $\text{cnt}$ to record the occurrence count of each element in the array $\text{nums}$. Then, we iterate through each key-value pair $(x, c)$ in the hash table. If the key $x + 1$ exists in the hash table, then the sum of occurrences of elements $x$ and $x + 1$, $c + \text{cnt}[x + 1]$, forms a harmonious subsequence. We just need to find the maximum length among all harmonious subsequences. +We can use a hash table $\textit{cnt}$ to record the occurrence count of each element in the array $\textit{nums}$. Then, we iterate through each key-value pair $(x, c)$ in the hash table. If the key $x + 1$ exists in the hash table, then the sum of occurrences of elements $x$ and $x + 1$, $c + \textit{cnt}[x + 1]$, forms a harmonious subsequence. We just need to find the maximum length among all harmonious subsequences. -The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$. +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. diff --git a/solution/0600-0699/0638.Shopping Offers/README.md b/solution/0600-0699/0638.Shopping Offers/README.md index fd29183233542..eb75179881823 100644 --- a/solution/0600-0699/0638.Shopping Offers/README.md +++ b/solution/0600-0699/0638.Shopping Offers/README.md @@ -76,17 +76,17 @@ tags: 我们注意到,题目中物品的种类 $n \leq 6$,而每种物品需要购买的数量不超过 $10$,我们可以用 $4$ 个二进制位来表示每种物品需要购买的数量,这样,我们只需要最多 $6 \times 4 = 24$ 个二进制位来表示整个购物清单。 -我们首先将购物清单 $\text{needs}$ 转换为一个整数 $\text{mask}$,其中第 $i$ 个物品需要购买的数量存储在 $\text{mask}$ 的第 $i \times 4$ 位到第 $(i + 1) \times 4 - 1$ 位。例如,当 $\text{needs} = [1, 2, 1]$ 时,有 $\text{mask} = 0b0001 0010 0001$。 +我们首先将购物清单 $\textit{needs}$ 转换为一个整数 $\textit{mask}$,其中第 $i$ 个物品需要购买的数量存储在 $\textit{mask}$ 的第 $i \times 4$ 位到第 $(i + 1) \times 4 - 1$ 位。例如,当 $\textit{needs} = [1, 2, 1]$ 时,有 $\textit{mask} = 0b0001 0010 0001$。 -然后,我们设计一个函数 $\text{dfs}(cur)$,表示当前购物清单的状态为 $\text{cur}$ 时,我们需要花费的最少金额。那么答案即为 $\text{dfs}(\text{mask})$。 +然后,我们设计一个函数 $\textit{dfs}(cur)$,表示当前购物清单的状态为 $\textit{cur}$ 时,我们需要花费的最少金额。那么答案即为 $\textit{dfs}(\textit{mask})$。 -函数 $\text{dfs}(cur)$ 的计算方法如下: +函数 $\textit{dfs}(cur)$ 的计算方法如下: -- 我们首先计算当前购物清单 $\text{cur}$ 不使用大礼包时的花费,记为 $\text{ans}$。 -- 然后,我们遍历每一个大礼包 $\text{offer}$,如果当前购物清单 $\text{cur}$ 能够使用大礼包 $\text{offer}$,即 $\text{cur}$ 中每种物品的数量都不小于大礼包 $\text{offer}$ 中的数量,那么我们可以尝试使用这个大礼包。我们将 $\text{cur}$ 中每种物品的数量减去大礼包 $\text{offer}$ 中的数量,得到一个新的购物清单 $\text{nxt}$,然后递归计算 $\text{nxt}$ 的最少花费,并加上大礼包的价格 $\text{offer}[n]$,更新 $\text{ans}$,即 $\text{ans} = \min(\text{ans}, \text{offer}[n] + \text{dfs}(\text{nxt}))$。 -- 最后,返回 $\text{ans}$。 +- 我们首先计算当前购物清单 $\textit{cur}$ 不使用大礼包时的花费,记为 $\textit{ans}$。 +- 然后,我们遍历每一个大礼包 $\textit{offer}$,如果当前购物清单 $\textit{cur}$ 能够使用大礼包 $\textit{offer}$,即 $\textit{cur}$ 中每种物品的数量都不小于大礼包 $\textit{offer}$ 中的数量,那么我们可以尝试使用这个大礼包。我们将 $\textit{cur}$ 中每种物品的数量减去大礼包 $\textit{offer}$ 中的数量,得到一个新的购物清单 $\textit{nxt}$,然后递归计算 $\textit{nxt}$ 的最少花费,并加上大礼包的价格 $\textit{offer}[n]$,更新 $\textit{ans}$,即 $\textit{ans} = \min(\textit{ans}, \textit{offer}[n] + \textit{dfs}(\textit{nxt}))$。 +- 最后,返回 $\textit{ans}$。 -为了避免重复计算,我们使用一个哈希表 $\text{f}$ 记录每一个状态 $\text{cur}$ 对应的最少花费。 +为了避免重复计算,我们使用一个哈希表 $\textit{f}$ 记录每一个状态 $\textit{cur}$ 对应的最少花费。 时间复杂度 $O(n \times k \times m^n)$,其中 $n$ 表示物品的种类,而 $k$ 和 $m$ 分别表示大礼包的数量以及每种物品的最大需求量。空间复杂度 $O(n \times m^n)$。 diff --git a/solution/0600-0699/0638.Shopping Offers/README_EN.md b/solution/0600-0699/0638.Shopping Offers/README_EN.md index a08277e2168ce..a6b8750f96c32 100644 --- a/solution/0600-0699/0638.Shopping Offers/README_EN.md +++ b/solution/0600-0699/0638.Shopping Offers/README_EN.md @@ -74,17 +74,17 @@ You cannot add more items, though only $9 for 2A ,2B and 1C. We notice that the number of types of items $n \leq 6$ in the problem, and the quantity of each item needed does not exceed $10$. We can use $4$ binary bits to represent the quantity of each item needed. Thus, we only need at most $6 \times 4 = 24$ binary bits to represent the entire shopping list. -First, we convert the shopping list $\text{needs}$ into an integer $\text{mask}$, where the quantity of the $i$-th item needed is stored in the $i \times 4$ to $(i + 1) \times 4 - 1$ bits of $\text{mask}$. For example, when $\text{needs} = [1, 2, 1]$, we have $\text{mask} = 0b0001 0010 0001$. +First, we convert the shopping list $\textit{needs}$ into an integer $\textit{mask}$, where the quantity of the $i$-th item needed is stored in the $i \times 4$ to $(i + 1) \times 4 - 1$ bits of $\textit{mask}$. For example, when $\textit{needs} = [1, 2, 1]$, we have $\textit{mask} = 0b0001 0010 0001$. -Then, we design a function $\text{dfs}(cur)$, representing the minimum amount of money we need to spend when the current state of the shopping list is $\text{cur}$. Therefore, the answer is $\text{dfs}(\text{mask})$. +Then, we design a function $\textit{dfs}(cur)$, representing the minimum amount of money we need to spend when the current state of the shopping list is $\textit{cur}$. Therefore, the answer is $\textit{dfs}(\textit{mask})$. -The calculation method of the function $\text{dfs}(cur)$ is as follows: +The calculation method of the function $\textit{dfs}(cur)$ is as follows: -- First, we calculate the cost of the current shopping list $\text{cur}$ without using any bundles, denoted as $\text{ans}$. -- Then, we iterate through each bundle $\text{offer}$. If the current shopping list $\text{cur}$ can use the bundle $\text{offer}$, i.e., the quantity of each item in $\text{cur}$ is not less than that in the bundle $\text{offer}$, then we can try to use this bundle. We subtract the quantity of each item in the bundle $\text{offer}$ from $\text{cur}$, obtaining a new shopping list $\text{nxt}$, then recursively calculate the minimum cost of $\text{nxt}$ and add the price of the bundle $\text{offer}[n]$, updating $\text{ans}$, i.e., $\text{ans} = \min(\text{ans}, \text{offer}[n] + \text{dfs}(\text{nxt}))$. -- Finally, return $\text{ans}$. +- First, we calculate the cost of the current shopping list $\textit{cur}$ without using any bundles, denoted as $\textit{ans}$. +- Then, we iterate through each bundle $\textit{offer}$. If the current shopping list $\textit{cur}$ can use the bundle $\textit{offer}$, i.e., the quantity of each item in $\textit{cur}$ is not less than that in the bundle $\textit{offer}$, then we can try to use this bundle. We subtract the quantity of each item in the bundle $\textit{offer}$ from $\textit{cur}$, obtaining a new shopping list $\textit{nxt}$, then recursively calculate the minimum cost of $\textit{nxt}$ and add the price of the bundle $\textit{offer}[n]$, updating $\textit{ans}$, i.e., $\textit{ans} = \min(\textit{ans}, \textit{offer}[n] + \textit{dfs}(\textit{nxt}))$. +- Finally, return $\textit{ans}$. -To avoid repeated calculations, we use a hash table $\text{f}$ to record the minimum cost corresponding to each state $\text{cur}$. +To avoid repeated calculations, we use a hash table $\textit{f}$ to record the minimum cost corresponding to each state $\textit{cur}$. The time complexity is $O(n \times k \times m^n)$, where $n$ represents the types of items, and $k$ and $m$ respectively represent the number of bundles and the maximum demand for each type of item. The space complexity is $O(n \times m^n)$. diff --git a/solution/0600-0699/0664.Strange Printer/README.md b/solution/0600-0699/0664.Strange Printer/README.md index fdbc2fa97358f..9ef146a0d673f 100644 --- a/solution/0600-0699/0664.Strange Printer/README.md +++ b/solution/0600-0699/0664.Strange Printer/README.md @@ -67,9 +67,9 @@ tags: $$ f[i][j]= \begin{cases} -1, & \text{if } i=j \\ -f[i][j-1], & \text{if } s[i]=s[j] \\ -\min_{i \leq k < j} \{f[i][k]+f[k+1][j]\}, & \text{otherwise} +1, & \textit{if } i=j \\ +f[i][j-1], & \textit{if } s[i]=s[j] \\ +\min_{i \leq k < j} \{f[i][k]+f[k+1][j]\}, & \textit{otherwise} \end{cases} $$ diff --git a/solution/0600-0699/0664.Strange Printer/README_EN.md b/solution/0600-0699/0664.Strange Printer/README_EN.md index fa77002259dd1..7e5385de0d3c0 100644 --- a/solution/0600-0699/0664.Strange Printer/README_EN.md +++ b/solution/0600-0699/0664.Strange Printer/README_EN.md @@ -66,9 +66,9 @@ Consider $f[i][j]$, if $s[i] = s[j]$, we can print $s[j]$ when print $s[i]$, so $$ f[i][j]= \begin{cases} -1, & \text{if } i=j \\ -f[i][j-1], & \text{if } s[i]=s[j] \\ -\min_{i \leq k < j} \{f[i][k]+f[k+1][j]\}, & \text{otherwise} +1, & \textit{if } i=j \\ +f[i][j-1], & \textit{if } s[i]=s[j] \\ +\min_{i \leq k < j} \{f[i][k]+f[k+1][j]\}, & \textit{otherwise} \end{cases} $$ diff --git a/solution/0600-0699/0691.Stickers to Spell Word/README.md b/solution/0600-0699/0691.Stickers to Spell Word/README.md index ba583ec999ee5..dde145678425d 100644 --- a/solution/0600-0699/0691.Stickers to Spell Word/README.md +++ b/solution/0600-0699/0691.Stickers to Spell Word/README.md @@ -69,11 +69,11 @@ tags: ### 方法一:BFS + 状态压缩 -我们注意到,字符串 $\text{target}$ 的长度不超过 $15$,我们可以使用一个长度为 $15$ 的二进制数来表示 $\text{target}$ 的每个字符是否被拼出,如果第 $i$ 位为 $1$,表示 $\text{target}$ 的第 $i$ 个字符已经被拼出,否则表示未被拼出。 +我们注意到,字符串 $\textit{target}$ 的长度不超过 $15$,我们可以使用一个长度为 $15$ 的二进制数来表示 $\textit{target}$ 的每个字符是否被拼出,如果第 $i$ 位为 $1$,表示 $\textit{target}$ 的第 $i$ 个字符已经被拼出,否则表示未被拼出。 -我们定义一个初始状态 $0$,表示所有字符都未被拼出,然后我们使用广度优先搜索的方法,从初始状态开始,每次搜索时,我们枚举所有的贴纸,对于每一张贴纸,我们尝试拼出 $\text{target}$ 的每一个字符,如果拼出了某个字符,我们就将对应的二进制数的第 $i$ 位设置为 $1$,表示该字符已经被拼出,然后我们继续搜索,直到我们拼出了 $\text{target}$ 的所有字符。 +我们定义一个初始状态 $0$,表示所有字符都未被拼出,然后我们使用广度优先搜索的方法,从初始状态开始,每次搜索时,我们枚举所有的贴纸,对于每一张贴纸,我们尝试拼出 $\textit{target}$ 的每一个字符,如果拼出了某个字符,我们就将对应的二进制数的第 $i$ 位设置为 $1$,表示该字符已经被拼出,然后我们继续搜索,直到我们拼出了 $\textit{target}$ 的所有字符。 -时间复杂度 $O(2^n \times m \times (l + n))$,空间复杂度 $O(2^n)$。其中 $n$ 是字符串 $\text{target}$ 的长度,而 $m$ 和 $l$ 分别是贴纸的数量和贴纸的平均长度。 +时间复杂度 $O(2^n \times m \times (l + n))$,空间复杂度 $O(2^n)$。其中 $n$ 是字符串 $\textit{target}$ 的长度,而 $m$ 和 $l$ 分别是贴纸的数量和贴纸的平均长度。 diff --git a/solution/0700-0799/0704.Binary Search/README.md b/solution/0700-0799/0704.Binary Search/README.md index 15b289eaf7ef0..293c01438e03b 100644 --- a/solution/0700-0799/0704.Binary Search/README.md +++ b/solution/0700-0799/0704.Binary Search/README.md @@ -54,14 +54,14 @@ tags: 我们定义二分查找的左边界 $l=0$,右边界 $r=n-1$。 -每一次循环,我们计算中间位置 $\text{mid}=(l+r)/2$,然后比较 $\text{nums}[\text{mid}]$ 和 $\text{target}$ 的大小。 +每一次循环,我们计算中间位置 $\textit{mid}=(l+r)/2$,然后比较 $\textit{nums}[\textit{mid}]$ 和 $\textit{target}$ 的大小。 -- 如果 $\text{nums}[\text{mid}] \geq \text{target}$,说明 $\text{target}$ 在左半部分,我们将右边界 $r$ 移动到 $\text{mid}$; -- 否则,说明 $\text{target}$ 在右半部分,我们将左边界 $l$ 移动到 $\text{mid}+1$。 +- 如果 $\textit{nums}[\textit{mid}] \geq \textit{target}$,说明 $\textit{target}$ 在左半部分,我们将右边界 $r$ 移动到 $\textit{mid}$; +- 否则,说明 $\textit{target}$ 在右半部分,我们将左边界 $l$ 移动到 $\textit{mid}+1$。 -循环结束的条件是 $l diff --git a/solution/0700-0799/0704.Binary Search/README_EN.md b/solution/0700-0799/0704.Binary Search/README_EN.md index 1fa800eee3eb6..316ff5daa8a2e 100644 --- a/solution/0700-0799/0704.Binary Search/README_EN.md +++ b/solution/0700-0799/0704.Binary Search/README_EN.md @@ -58,14 +58,14 @@ tags: We define the left boundary $l=0$ and the right boundary $r=n-1$ for binary search. -In each iteration, we calculate the middle position $\text{mid}=(l+r)/2$, then compare the size of $\text{nums}[\text{mid}]$ and $\text{target}$. +In each iteration, we calculate the middle position $\textit{mid}=(l+r)/2$, then compare the size of $\textit{nums}[\textit{mid}]$ and $\textit{target}$. -- If $\text{nums}[\text{mid}] \geq \text{target}$, it means $\text{target}$ is in the left half, so we move the right boundary $r$ to $\text{mid}$; -- Otherwise, it means $\text{target}$ is in the right half, so we move the left boundary $l$ to $\text{mid}+1$. +- If $\textit{nums}[\textit{mid}] \geq \textit{target}$, it means $\textit{target}$ is in the left half, so we move the right boundary $r$ to $\textit{mid}$; +- Otherwise, it means $\textit{target}$ is in the right half, so we move the left boundary $l$ to $\textit{mid}+1$. -The loop ends when $l diff --git a/solution/0700-0799/0727.Minimum Window Subsequence/README.md b/solution/0700-0799/0727.Minimum Window Subsequence/README.md index da3875ac1a869..e18eafea57589 100644 --- a/solution/0700-0799/0727.Minimum Window Subsequence/README.md +++ b/solution/0700-0799/0727.Minimum Window Subsequence/README.md @@ -57,8 +57,8 @@ S = "abcdebdde", T = "bde" $$ f[i][j] = \begin{cases} -i, & j = 1 \text{ and } s1[i-1] = s2[j] \\ -f[i - 1][j - 1], & j > 1 \text{ and } s1[i-1] = s2[j-1] \\ +i, & j = 1 \textit{ and } s1[i-1] = s2[j] \\ +f[i - 1][j - 1], & j > 1 \textit{ and } s1[i-1] = s2[j-1] \\ f[i - 1][j], & s1[i-1] \ne s2[j-1] \end{cases} $$ diff --git a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md index 2102e01230162..3ae8f7268b653 100644 --- a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md +++ b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README.md @@ -64,13 +64,13 @@ tags: ### 方法一:二分查找 -由于 $\text{letters}$ 是按照非递减顺序排序的,所以我们可以使用二分查找来找到大于 `target` 的最小字符。 +由于 $\textit{letters}$ 是按照非递减顺序排序的,所以我们可以使用二分查找来找到大于 `target` 的最小字符。 -我们定义二分查找的左边界 $l = 0$,右边界 $r = n$。对于每一次二分查找,我们计算中间位置 $mid = (l + r) / 2$,如果 $letters[mid] > \text{target}$,则说明我们需要在左半部分继续查找,即 $r = mid$;否则我们需要在右半部分继续查找,即 $l = mid + 1$。 +我们定义二分查找的左边界 $l = 0$,右边界 $r = n$。对于每一次二分查找,我们计算中间位置 $mid = (l + r) / 2$,如果 $letters[mid] > \textit{target}$,则说明我们需要在左半部分继续查找,即 $r = mid$;否则我们需要在右半部分继续查找,即 $l = mid + 1$。 最后我们返回 $letters[l \mod n]$ 即可。 -时间复杂度 $O(\log n)$,其中 $n$ 是 $\text{letters}$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(\log n)$,其中 $n$ 是 $\textit{letters}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md index dcf32c88a0947..fda3474a9c6c6 100644 --- a/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md +++ b/solution/0700-0799/0744.Find Smallest Letter Greater Than Target/README_EN.md @@ -67,7 +67,7 @@ tags: Since `letters` is sorted in non-decreasing order, we can use binary search to find the smallest character that is larger than `target`. -We define the left boundary of the binary search as $l = 0$, and the right boundary as $r = n$. For each binary search, we calculate the middle position $mid = (l + r) / 2$. If $letters[mid] > \text{target}$, it means we need to continue searching in the left half, so we set $r = mid$. Otherwise, we need to continue searching in the right half, so we set $l = mid + 1$. +We define the left boundary of the binary search as $l = 0$, and the right boundary as $r = n$. For each binary search, we calculate the middle position $mid = (l + r) / 2$. If $letters[mid] > \textit{target}$, it means we need to continue searching in the left half, so we set $r = mid$. Otherwise, we need to continue searching in the right half, so we set $l = mid + 1$. Finally, we return $letters[l \mod n]$. diff --git a/solution/0800-0899/0807.Max Increase to Keep City Skyline/README.md b/solution/0800-0899/0807.Max Increase to Keep City Skyline/README.md index 694dd93284e8b..382955b9cae79 100644 --- a/solution/0800-0899/0807.Max Increase to Keep City Skyline/README.md +++ b/solution/0800-0899/0807.Max Increase to Keep City Skyline/README.md @@ -69,11 +69,11 @@ gridNew = [ [8, 4, 8, 7], ### 方法一:贪心 -根据题目描述,我们可以将每个单元格 $(i, j)$ 的值增加至第 $i$ 行的最大值和第 $j$ 列的最大值中的较小值,这样可以保证不影响天际线,即每个单元格增加的高度为 $\min(\text{rowMax}[i], \text{colMax}[j]) - \text{grid}[i][j]$。 +根据题目描述,我们可以将每个单元格 $(i, j)$ 的值增加至第 $i$ 行的最大值和第 $j$ 列的最大值中的较小值,这样可以保证不影响天际线,即每个单元格增加的高度为 $\min(\textit{rowMax}[i], \textit{colMax}[j]) - \textit{grid}[i][j]$。 -因此,我们可以先遍历一次矩阵,分别计算出每行和每列的最大值,记录在数组 $\text{rowMax}$ 和 $\text{colMax}$ 中,然后再遍历一次矩阵,计算出答案即可。 +因此,我们可以先遍历一次矩阵,分别计算出每行和每列的最大值,记录在数组 $\textit{rowMax}$ 和 $\textit{colMax}$ 中,然后再遍历一次矩阵,计算出答案即可。 -时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为矩阵 $\text{grid}$ 的边长。 +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为矩阵 $\textit{grid}$ 的边长。 diff --git a/solution/0800-0899/0807.Max Increase to Keep City Skyline/README_EN.md b/solution/0800-0899/0807.Max Increase to Keep City Skyline/README_EN.md index 8e7d66a40dac4..7aa55968344f4 100644 --- a/solution/0800-0899/0807.Max Increase to Keep City Skyline/README_EN.md +++ b/solution/0800-0899/0807.Max Increase to Keep City Skyline/README_EN.md @@ -67,11 +67,11 @@ gridNew = [ [8, 4, 8, 7], ### Solution 1: Greedy -According to the problem description, we can increase the value of each cell $(i, j)$ to the smaller value between the maximum value of the $i$-th row and the $j$-th column, ensuring it does not affect the skyline. Thus, the height added to each cell is $\min(\text{rowMax}[i], \text{colMax}[j]) - \text{grid}[i][j]$. +According to the problem description, we can increase the value of each cell $(i, j)$ to the smaller value between the maximum value of the $i$-th row and the $j$-th column, ensuring it does not affect the skyline. Thus, the height added to each cell is $\min(\textit{rowMax}[i], \textit{colMax}[j]) - \textit{grid}[i][j]$. -Therefore, we can first traverse the matrix once to calculate the maximum value of each row and column, storing them in the arrays $\text{rowMax}$ and $\text{colMax}$, respectively. Then, we traverse the matrix again to compute the answer. +Therefore, we can first traverse the matrix once to calculate the maximum value of each row and column, storing them in the arrays $\textit{rowMax}$ and $\textit{colMax}$, respectively. Then, we traverse the matrix again to compute the answer. -The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the side length of the matrix $\text{grid}$. +The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the side length of the matrix $\textit{grid}$. diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README.md b/solution/0800-0899/0826.Most Profit Assigning Work/README.md index 468f898113a8b..1926bc576fe8e 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README.md @@ -194,7 +194,7 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num ### 方法二:动态规划 -我们不妨记 $m = \max(\text{difficulty})$,定义一个长度为 $m + 1$ 的数组 $f$,其中 $f[i]$ 表示难度小于等于 $i$ 的工作中收益的最大值,初始时 $f[i] = 0$。 +我们不妨记 $m = \max(\textit{difficulty})$,定义一个长度为 $m + 1$ 的数组 $f$,其中 $f[i]$ 表示难度小于等于 $i$ 的工作中收益的最大值,初始时 $f[i] = 0$。 然后我们遍历工作,对于每个工作 $(d, p)$,我们更新 $f[d] = \max(f[d], p)$。 diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md index d9edd25f594fc..2a3b780e574a9 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md @@ -194,7 +194,7 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num ### Solution 2: Dynamic Programming -Let's denote $m = \max(\text{difficulty})$ and define an array $f$ of length $m + 1$, where $f[i]$ represents the maximum profit among jobs with difficulty less than or equal to $i$, initially $f[i] = 0$. +Let's denote $m = \max(\textit{difficulty})$ and define an array $f$ of length $m + 1$, where $f[i]$ represents the maximum profit among jobs with difficulty less than or equal to $i$, initially $f[i] = 0$. Then, we iterate over the jobs, and for each job $(d, p)$, if $d \leq m$, we update $f[d] = \max(f[d], p)$. diff --git a/solution/0800-0899/0875.Koko Eating Bananas/README.md b/solution/0800-0899/0875.Koko Eating Bananas/README.md index 375436e0b4f85..d3eed49cf0dd5 100644 --- a/solution/0800-0899/0875.Koko Eating Bananas/README.md +++ b/solution/0800-0899/0875.Koko Eating Bananas/README.md @@ -71,7 +71,7 @@ tags: 我们注意到,如果珂珂能够以 $k$ 的速度在 $h$ 小时内吃完所有香蕉,那么她也可以以 $k' > k$ 的速度在 $h$ 小时内吃完所有香蕉。这存在着单调性,因此我们可以使用二分查找,找到最小的满足条件的 $k$。 -我们定义二分查找的左边界 $l = 1$,右边界 $r = \max(\text{piles})$。每一次二分,我们取中间值 $mid = \frac{l + r}{2}$,然后计算以 $mid$ 的速度吃香蕉需要的时间 $s$。如果 $s \leq h$,说明 $mid$ 的速度可以满足条件,我们将右边界 $r$ 更新为 $mid$;否则,我们将左边界 $l$ 更新为 $mid + 1$。最终,当 $l = r$ 时,我们找到了最小的满足条件的 $k$。 +我们定义二分查找的左边界 $l = 1$,右边界 $r = \max(\textit{piles})$。每一次二分,我们取中间值 $mid = \frac{l + r}{2}$,然后计算以 $mid$ 的速度吃香蕉需要的时间 $s$。如果 $s \leq h$,说明 $mid$ 的速度可以满足条件,我们将右边界 $r$ 更新为 $mid$;否则,我们将左边界 $l$ 更新为 $mid + 1$。最终,当 $l = r$ 时,我们找到了最小的满足条件的 $k$。 时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 `piles` 的长度和最大值。空间复杂度 $O(1)$。 diff --git a/solution/0800-0899/0875.Koko Eating Bananas/README_EN.md b/solution/0800-0899/0875.Koko Eating Bananas/README_EN.md index 13a26d48b3fb0..b8b6d373d6aa1 100644 --- a/solution/0800-0899/0875.Koko Eating Bananas/README_EN.md +++ b/solution/0800-0899/0875.Koko Eating Bananas/README_EN.md @@ -66,7 +66,7 @@ tags: We notice that if Koko can eat all the bananas at a speed of $k$ within $h$ hours, then she can also eat all the bananas at a speed of $k' > k$ within $h$ hours. This shows monotonicity, so we can use binary search to find the smallest $k$ that satisfies the condition. -We define the left boundary of the binary search as $l = 1$, and the right boundary as $r = \max(\text{piles})$. For each binary search, we take the middle value $mid = \frac{l + r}{2}$, and then calculate the time $s$ required to eat bananas at a speed of $mid$. If $s \leq h$, it means that the speed of $mid$ can meet the condition, and we update the right boundary $r$ to $mid$; otherwise, we update the left boundary $l$ to $mid + 1$. Finally, when $l = r$, we find the smallest $k$ that satisfies the condition. +We define the left boundary of the binary search as $l = 1$, and the right boundary as $r = \max(\textit{piles})$. For each binary search, we take the middle value $mid = \frac{l + r}{2}$, and then calculate the time $s$ required to eat bananas at a speed of $mid$. If $s \leq h$, it means that the speed of $mid$ can meet the condition, and we update the right boundary $r$ to $mid$; otherwise, we update the left boundary $l$ to $mid + 1$. Finally, when $l = r$, we find the smallest $k$ that satisfies the condition. The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length and maximum value of the array `piles` respectively. The space complexity is $O(1)$. diff --git a/solution/0900-0999/0925.Long Pressed Name/README.md b/solution/0900-0999/0925.Long Pressed Name/README.md index 12ec097abfb5b..d0c698d7f1ee0 100644 --- a/solution/0900-0999/0925.Long Pressed Name/README.md +++ b/solution/0900-0999/0925.Long Pressed Name/README.md @@ -56,9 +56,9 @@ tags: ### 方法一:双指针 -我们利用两个指针 $i$ 和 $j$ 分别指向字符串 $\text{typed}$ 和 $\text{name}$ 的第一个字符,然后开始遍历,如果 $\text{typed}[j] \neq \text{name}[i]$,说明两个字符串不匹配,直接返回 $\text{False}$。否则,我们找到连续相同的字符的下一个位置,分别记为 $x$ 和 $y$,如果 $x - i > y - j$,说明 $\text{typed}$ 中的字符个数小于 $\text{name}$ 中的字符个数,直接返回 $\text{False}$。否则,我们将 $i$ 和 $j$ 更新为 $x$ 和 $y$,继续遍历,直到 $i$ 和 $j$ 分别遍历完 $\text{name}$ 和 $\text{typed}$,返回 $\text{True}$。 +我们利用两个指针 $i$ 和 $j$ 分别指向字符串 $\textit{typed}$ 和 $\textit{name}$ 的第一个字符,然后开始遍历,如果 $\textit{typed}[j] \neq \textit{name}[i]$,说明两个字符串不匹配,直接返回 $\textit{False}$。否则,我们找到连续相同的字符的下一个位置,分别记为 $x$ 和 $y$,如果 $x - i > y - j$,说明 $\textit{typed}$ 中的字符个数小于 $\textit{name}$ 中的字符个数,直接返回 $\textit{False}$。否则,我们将 $i$ 和 $j$ 更新为 $x$ 和 $y$,继续遍历,直到 $i$ 和 $j$ 分别遍历完 $\textit{name}$ 和 $\textit{typed}$,返回 $\textit{True}$。 -时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别是字符串 $\text{name}$ 和 $\text{typed}$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别是字符串 $\textit{name}$ 和 $\textit{typed}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md index 72e3e3bfde0bd..70740c3609d9d 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README.md @@ -153,9 +153,9 @@ function minIncrementForUnique(nums: number[]): number { ### 方法二:计数 + 贪心 -根据题目描述,结果数组的最大值 $m = \max(\text{nums}) + \text{len}(\text{nums})$,我们可以使用一个计数数组 $\textit{cnt}$ 来记录每个元素出现的次数。 +根据题目描述,结果数组的最大值 $m = \max(\textit{nums}) + \textit{len}(\textit{nums})$,我们可以使用一个计数数组 $\textit{cnt}$ 来记录每个元素出现的次数。 -然后从 $0$ 到 $m - 1$ 遍历,对于每个元素 $i$,如果它出现的次数 $\text{cnt}[i]$ 大于 $1$,那么我们将 $\text{cnt}[i] - 1$ 个元素增加到 $i + 1$,并将操作次数累加到结果中。 +然后从 $0$ 到 $m - 1$ 遍历,对于每个元素 $i$,如果它出现的次数 $\textit{cnt}[i]$ 大于 $1$,那么我们将 $\textit{cnt}[i] - 1$ 个元素增加到 $i + 1$,并将操作次数累加到结果中。 遍历完成后,返回结果即可。 diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md index 0c5e0cfd03638..54526c5199143 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/README_EN.md @@ -151,7 +151,7 @@ function minIncrementForUnique(nums: number[]): number { ### Solution 2: Counting + Greedy -According to the problem description, the maximum value of the result array $m = \max(\text{nums}) + \text{len}(\text{nums})$. We can use a counting array $\textit{cnt}$ to record the occurrence count of each element. +According to the problem description, the maximum value of the result array $m = \max(\textit{nums}) + \textit{len}(\textit{nums})$. We can use a counting array $\textit{cnt}$ to record the occurrence count of each element. Then, we iterate from $0$ to $m - 1$. For each element $i$, if its occurrence count $\textit{cnt}[i]$ is greater than $1$, then we add $\textit{cnt}[i] - 1$ elements to $i + 1$, and accumulate the operation count into the result. diff --git a/solution/0900-0999/0956.Tallest Billboard/README.md b/solution/0900-0999/0956.Tallest Billboard/README.md index e745f9a2d9729..9d919ef4ad0a3 100644 --- a/solution/0900-0999/0956.Tallest Billboard/README.md +++ b/solution/0900-0999/0956.Tallest Billboard/README.md @@ -245,9 +245,9 @@ function tallestBillboard(rods: number[]): number { $$ \begin{aligned} f[i][j] &= f[i-1][j] \\ -f[i][j] &= max(f[i][j], f[i-1][j-rods[i-1]]) & \text{if } j \geq rods[i-1] \\ -f[i][j] &= max(f[i][j], f[i-1][j+rods[i-1]] + rods[i-1]) & \text{if } j + rods[i-1] \leq s \\ -f[i][j] &= max(f[i][j], f[i-1][rods[i-1]-j] + rods[i-1]-j) & \text{if } j \lt rods[i-1] +f[i][j] &= max(f[i][j], f[i-1][j-rods[i-1]]) & \textit{if } j \geq rods[i-1] \\ +f[i][j] &= max(f[i][j], f[i-1][j+rods[i-1]] + rods[i-1]) & \textit{if } j + rods[i-1] \leq s \\ +f[i][j] &= max(f[i][j], f[i-1][rods[i-1]-j] + rods[i-1]-j) & \textit{if } j \lt rods[i-1] \end{aligned} $$ diff --git a/solution/0900-0999/0973.K Closest Points to Origin/README.md b/solution/0900-0999/0973.K Closest Points to Origin/README.md index d2c088e0e15aa..c2619b2a8acc1 100644 --- a/solution/0900-0999/0973.K Closest Points to Origin/README.md +++ b/solution/0900-0999/0973.K Closest Points to Origin/README.md @@ -71,7 +71,7 @@ tags: 我们将所有点按照与原点的距离从小到大排序,然后取前 $k$ 个点即可。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\text{points}$ 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{points}$ 的长度。 @@ -155,7 +155,7 @@ impl Solution { 我们可以使用一个优先队列(大根堆)来维护距离原点最近的 $k$ 个点。 -时间复杂度 $O(n \times \log k)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\text{points}$ 的长度。 +时间复杂度 $O(n \times \log k)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\textit{points}$ 的长度。 @@ -285,7 +285,7 @@ function kClosest(points: number[][], k: number): number[][] { 二分查找结束后,我们只需要返回距离小于等于左边界的点即可。 -时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{points}$ 的长度,而 $M$ 为距离的最大值。 +时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{points}$ 的长度,而 $M$ 为距离的最大值。 diff --git a/solution/0900-0999/0973.K Closest Points to Origin/README_EN.md b/solution/0900-0999/0973.K Closest Points to Origin/README_EN.md index 6a79fa85e7dc4..5a6704413aee5 100644 --- a/solution/0900-0999/0973.K Closest Points to Origin/README_EN.md +++ b/solution/0900-0999/0973.K Closest Points to Origin/README_EN.md @@ -67,7 +67,7 @@ We only want the closest k = 1 points from the origin, so the answer is just [[- We sort all points by their distance from the origin in ascending order, and then take the first $k$ points. -The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\text{points}$. +The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{points}$. @@ -151,7 +151,7 @@ impl Solution { We can use a priority queue (max heap) to maintain the $k$ closest points to the origin. -The time complexity is $O(n \times \log k)$, and the space complexity is $O(k)$. Here, $n$ is the length of the array $\text{points}$. +The time complexity is $O(n \times \log k)$, and the space complexity is $O(k)$. Here, $n$ is the length of the array $\textit{points}$. @@ -281,7 +281,7 @@ Therefore, we can use binary search to enumerate the distance. In each binary se After the binary search is finished, we just need to return the points whose distance is less than or equal to the left boundary. -The time complexity is $O(n \times \log M)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\text{points}$, and $M$ is the maximum value of the distance. +The time complexity is $O(n \times \log M)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{points}$, and $M$ is the maximum value of the distance. diff --git a/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md b/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md index 7602fd5761710..c6ea3a99187da 100644 --- a/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md +++ b/solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md @@ -61,9 +61,9 @@ tags: ### 方法一:DFS -我们定义一个函数 $\text{dfs(\text{node})}$,表示以 $\text{node}$ 为根节点的子树中,金币的超载量,即金币的数量减去节点数。如果 $\text{dfs(\text{node})}$ 为正数,表示该子树中金币的数量多于节点数,需要将多余的金币移出该子树;如果 $\text{dfs(\text{node})}$ 为负数,表示该子树中金币的数量少于节点数,需要将不足的金币移入该子树。 +我们定义一个函数 $\textit{dfs(\textit{node})}$,表示以 $\textit{node}$ 为根节点的子树中,金币的超载量,即金币的数量减去节点数。如果 $\textit{dfs(\textit{node})}$ 为正数,表示该子树中金币的数量多于节点数,需要将多余的金币移出该子树;如果 $\textit{dfs(\textit{node})}$ 为负数,表示该子树中金币的数量少于节点数,需要将不足的金币移入该子树。 -在函数 $\text{dfs(\text{node})}$ 中,我们首先遍历左右子树,获得左右子树的金币超载量 $\text{left}$ 和 $\text{right}$。那么当前移动的次数需要加上 $|\text{left}| + |\text{right}|$,即将左右子树中的金币移动到当前节点。然后,我们返回整个子树的金币超载量,即 $\text{left} + \text{right} + \text{node.val} - 1$。 +在函数 $\textit{dfs(\textit{node})}$ 中,我们首先遍历左右子树,获得左右子树的金币超载量 $\textit{left}$ 和 $\textit{right}$。那么当前移动的次数需要加上 $|\textit{left}| + |\textit{right}|$,即将左右子树中的金币移动到当前节点。然后,我们返回整个子树的金币超载量,即 $\textit{left} + \textit{right} + \textit{node.val} - 1$。 最后返回移动的次数即可。 diff --git a/solution/0900-0999/0979.Distribute Coins in Binary Tree/README_EN.md b/solution/0900-0999/0979.Distribute Coins in Binary Tree/README_EN.md index 33414a0a71cf0..e808d017f80f0 100644 --- a/solution/0900-0999/0979.Distribute Coins in Binary Tree/README_EN.md +++ b/solution/0900-0999/0979.Distribute Coins in Binary Tree/README_EN.md @@ -59,9 +59,9 @@ tags: ### Solution 1: DFS -We define a function $\text{dfs(node)}$, which represents the coin overload in the subtree rooted at $\text{node}$, i.e., the number of coins minus the number of nodes. If $\text{dfs(node)}$ is positive, it means the subtree has more coins than nodes, and the excess coins need to be moved out of the subtree; if $\text{dfs(node)}$ is negative, it means the subtree has fewer coins than nodes, and the shortfall needs to be moved into the subtree. +We define a function $\textit{dfs(node)}$, which represents the coin overload in the subtree rooted at $\textit{node}$, i.e., the number of coins minus the number of nodes. If $\textit{dfs(node)}$ is positive, it means the subtree has more coins than nodes, and the excess coins need to be moved out of the subtree; if $\textit{dfs(node)}$ is negative, it means the subtree has fewer coins than nodes, and the shortfall needs to be moved into the subtree. -In the function $\text{dfs(node)}$, we first traverse the left and right subtrees to obtain the coin overload $\text{left}$ and $\text{right}$ of the left and right subtrees, respectively. Then, the current number of moves needs to be increased by $|\text{left}| + |\text{right}|$, which means moving the coins from the left and right subtrees to the current node. After that, we return the coin overload of the entire subtree, which is $\text{left} + \text{right} + \text{node.val} - 1$. +In the function $\textit{dfs(node)}$, we first traverse the left and right subtrees to obtain the coin overload $\textit{left}$ and $\textit{right}$ of the left and right subtrees, respectively. Then, the current number of moves needs to be increased by $|\textit{left}| + |\textit{right}|$, which means moving the coins from the left and right subtrees to the current node. After that, we return the coin overload of the entire subtree, which is $\textit{left} + \textit{right} + \textit{node.val} - 1$. Finally, we return the number of moves. diff --git a/solution/0900-0999/0983.Minimum Cost For Tickets/README.md b/solution/0900-0999/0983.Minimum Cost For Tickets/README.md index e15b0e21a303b..3bbdbc2ca3ab0 100644 --- a/solution/0900-0999/0983.Minimum Cost For Tickets/README.md +++ b/solution/0900-0999/0983.Minimum Cost For Tickets/README.md @@ -78,12 +78,12 @@ tags: ### 方法一:记忆化搜索 + 二分查找 -我们定义一个函数 $\text{dfs(i)}$,表示从第 $i$ 次出行开始到最后一次出行结束所需的最小花费。那么答案为 $\text{dfs(0)}$。 +我们定义一个函数 $\textit{dfs(i)}$,表示从第 $i$ 次出行开始到最后一次出行结束所需的最小花费。那么答案为 $\textit{dfs(0)}$。 -函数 $\text{dfs(i)}$ 的执行过程如下: +函数 $\textit{dfs(i)}$ 的执行过程如下: - 如果 $i \geq n$,表示所有出行已经结束,返回 $0$; -- 否则,我们需要考虑三种购买方式,分别是购买 $1$ 天通行证、购买 $7$ 天通行证和购买 $30$ 天通行证。我们分别计算这三种购买方式的花费,并且利用二分查找,找到下一次出行的下标 $j$,然后递归调用 $\text{dfs(j)}$,最后返回这三种购买方式的最小花费。 +- 否则,我们需要考虑三种购买方式,分别是购买 $1$ 天通行证、购买 $7$ 天通行证和购买 $30$ 天通行证。我们分别计算这三种购买方式的花费,并且利用二分查找,找到下一次出行的下标 $j$,然后递归调用 $\textit{dfs(j)}$,最后返回这三种购买方式的最小花费。 为了避免重复计算,我们使用记忆化搜索,将已经计算过的结果保存起来。 @@ -247,9 +247,9 @@ function mincostTickets(days: number[], costs: number[]): number { ### 方法二:动态规划 -我们不妨记 $\text{days}$ 数组中的最后一天为 $m$,那么我们可以定义一个长度为 $m + 1$ 的数组 $f$,其中 $f[i]$ 表示从第 $1$ 天到第 $i$ 天的最小花费。 +我们不妨记 $\textit{days}$ 数组中的最后一天为 $m$,那么我们可以定义一个长度为 $m + 1$ 的数组 $f$,其中 $f[i]$ 表示从第 $1$ 天到第 $i$ 天的最小花费。 -我们可以按照 $\text{days}$ 数组中的日期递增的顺序,从第 $1$ 天开始,依次计算 $f[i]$ 的值。如果第 $i$ 天是出行的日期,那么我们可以考虑三种购买方式,分别是购买 $1$ 天通行证、购买 $7$ 天通行证和购买 $30$ 天通行证。我们分别计算这三种购买方式的花费,并且取这三种购买方式的最小花费作为 $f[i]$ 的值。如果第 $i$ 天不是出行的日期,那么 $f[i] = f[i - 1]$。 +我们可以按照 $\textit{days}$ 数组中的日期递增的顺序,从第 $1$ 天开始,依次计算 $f[i]$ 的值。如果第 $i$ 天是出行的日期,那么我们可以考虑三种购买方式,分别是购买 $1$ 天通行证、购买 $7$ 天通行证和购买 $30$ 天通行证。我们分别计算这三种购买方式的花费,并且取这三种购买方式的最小花费作为 $f[i]$ 的值。如果第 $i$ 天不是出行的日期,那么 $f[i] = f[i - 1]$。 最终答案为 $f[m]$。 diff --git a/solution/0900-0999/0983.Minimum Cost For Tickets/README_EN.md b/solution/0900-0999/0983.Minimum Cost For Tickets/README_EN.md index 4323db3959fe5..4d70c7339522b 100644 --- a/solution/0900-0999/0983.Minimum Cost For Tickets/README_EN.md +++ b/solution/0900-0999/0983.Minimum Cost For Tickets/README_EN.md @@ -78,12 +78,12 @@ In total, you spent $17 and covered all the days of your travel. ### Solution 1: Memoization Search + Binary Search -We define a function $\text{dfs(i)}$, which represents the minimum cost required from the $i$-th trip to the last trip. Thus, the answer is $\text{dfs(0)}$. +We define a function $\textit{dfs(i)}$, which represents the minimum cost required from the $i$-th trip to the last trip. Thus, the answer is $\textit{dfs(0)}$. -The execution process of the function $\text{dfs(i)}$ is as follows: +The execution process of the function $\textit{dfs(i)}$ is as follows: - If $i \geq n$, it means all trips have ended, return $0$; -- Otherwise, we need to consider three types of purchases: buying a 1-day pass, buying a 7-day pass, and buying a 30-day pass. We calculate the cost for these three purchasing methods separately and use binary search to find the index $j$ of the next trip, then recursively call $\text{dfs(j)}$, and finally return the minimum cost among these three purchasing methods. +- Otherwise, we need to consider three types of purchases: buying a 1-day pass, buying a 7-day pass, and buying a 30-day pass. We calculate the cost for these three purchasing methods separately and use binary search to find the index $j$ of the next trip, then recursively call $\textit{dfs(j)}$, and finally return the minimum cost among these three purchasing methods. To avoid repeated calculations, we use memoization search to save the results that have already been calculated. @@ -247,9 +247,9 @@ function mincostTickets(days: number[], costs: number[]): number { ### Solution 2: Dynamic Programming -Let's denote the last day in the $\text{days}$ array as $m$. We can define an array $f$ of length $m + 1$, where $f[i]$ represents the minimum cost from day $1$ to day $i$. +Let's denote the last day in the $\textit{days}$ array as $m$. We can define an array $f$ of length $m + 1$, where $f[i]$ represents the minimum cost from day $1$ to day $i$. -We can calculate the value of $f[i]$ in increasing order of the dates in the $\text{days}$ array, starting from day $1$. If day $i$ is a travel day, we can consider three purchasing options: buying a 1-day pass, buying a 7-day pass, and buying a 30-day pass. We calculate the cost for these three purchasing methods separately and take the minimum cost among these three as the value of $f[i]$. If day $i$ is not a travel day, then $f[i] = f[i - 1]$. +We can calculate the value of $f[i]$ in increasing order of the dates in the $\textit{days}$ array, starting from day $1$. If day $i$ is a travel day, we can consider three purchasing options: buying a 1-day pass, buying a 7-day pass, and buying a 30-day pass. We calculate the cost for these three purchasing methods separately and take the minimum cost among these three as the value of $f[i]$. If day $i$ is not a travel day, then $f[i] = f[i - 1]$. The final answer is $f[m]$. diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index 2b458272f51d8..d0ec4e3e88685 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README.md +++ b/solution/0900-0999/0994.Rotting Oranges/README.md @@ -76,7 +76,7 @@ tags: ### 方法一:BFS -我们首先遍历一遍整个网格,统计出新鲜橘子的数量,记为 $\text{cnt}$,并且将所有腐烂的橘子的坐标加入队列 $q$ 中。 +我们首先遍历一遍整个网格,统计出新鲜橘子的数量,记为 $\textit{cnt}$,并且将所有腐烂的橘子的坐标加入队列 $q$ 中。 接下来,我们进行广度优先搜索,每一轮搜索,我们将队列中的所有腐烂的橘子向四个方向腐烂新鲜橘子,直到队列为空或者新鲜橘子的数量为 $0$ 为止。 diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index 6fe41467c7731..c1482642cdeb6 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -72,7 +72,7 @@ tags: ### Solution 1: BFS -First, we traverse the entire grid once, count the number of fresh oranges, denoted as $\text{cnt}$, and add the coordinates of all rotten oranges to the queue $q$. +First, we traverse the entire grid once, count the number of fresh oranges, denoted as $\textit{cnt}$, and add the coordinates of all rotten oranges to the queue $q$. Next, we perform a breadth-first search. In each round of the search, we let all the rotten oranges in the queue rot the fresh oranges in four directions, until the queue is empty or the number of fresh oranges is $0$. diff --git a/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README.md b/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README.md index 239f2598b2679..ab761d1188ba5 100644 --- a/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README.md +++ b/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README.md @@ -238,9 +238,9 @@ impl Solution { ### 方法二:滑动窗口 -我们可以用一个变量 $\text{flipped}$ 来表示当前位置是否翻转,如果 $\text{flipped}$ 为 $1$,表示当前位置已经翻转,否则表示当前位置未翻转。对于翻转过的位置,我们可以将其值设置为 $-1$,这样我们就可以区分出哪些位置已经翻转过了。 +我们可以用一个变量 $\textit{flipped}$ 来表示当前位置是否翻转,如果 $\textit{flipped}$ 为 $1$,表示当前位置已经翻转,否则表示当前位置未翻转。对于翻转过的位置,我们可以将其值设置为 $-1$,这样我们就可以区分出哪些位置已经翻转过了。 -接下来我们从左到右遍历数组,对于每个位置 $i$,如果 $i \geq k$ 且 $i-k$ 位置的元素为 $-1$,那么当前位置的翻转状态应该与前一个位置的翻转状态相反。即 $\text{flipped} = \text{flipped} \oplus 1$。如果当前位置的元素与当前位置的翻转状态相同,那么我们需要翻转当前位置,此时我们判断一下 $i+k$ 是否超出了数组的长度,如果超出了数组的长度,那么就无法完成目标,返回 $-1$。否则我们将当前位置的翻转状态取反,同时将答案增加 $1$,并且将当前位置的元素设置为 $-1$。 +接下来我们从左到右遍历数组,对于每个位置 $i$,如果 $i \geq k$ 且 $i-k$ 位置的元素为 $-1$,那么当前位置的翻转状态应该与前一个位置的翻转状态相反。即 $\textit{flipped} = \textit{flipped} \oplus 1$。如果当前位置的元素与当前位置的翻转状态相同,那么我们需要翻转当前位置,此时我们判断一下 $i+k$ 是否超出了数组的长度,如果超出了数组的长度,那么就无法完成目标,返回 $-1$。否则我们将当前位置的翻转状态取反,同时将答案增加 $1$,并且将当前位置的元素设置为 $-1$。 这样当我们处理完数组中的所有元素时,返回答案即可。 diff --git a/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README_EN.md b/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README_EN.md index 0b765434e9498..b05ee0398b2ba 100644 --- a/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README_EN.md +++ b/solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README_EN.md @@ -236,9 +236,9 @@ impl Solution { ### Solution 2: Sliding Window -We can use a variable $\text{flipped}$ to indicate whether the current position has been flipped. If $\text{flipped} = 1$, it means the current position has already been flipped; otherwise, it means the current position has not been flipped. For positions that have been flipped, we can set their value to $-1$, allowing us to distinguish which positions have been flipped. +We can use a variable $\textit{flipped}$ to indicate whether the current position has been flipped. If $\textit{flipped} = 1$, it means the current position has already been flipped; otherwise, it means the current position has not been flipped. For positions that have been flipped, we can set their value to $-1$, allowing us to distinguish which positions have been flipped. -Next, we traverse the array from left to right. For each position $i$, if $i \geq k$ and the element at position $i-k$ is $-1$, then the flip state of the current position should be the opposite of the flip state of the previous position. That is, $\text{flipped} = \text{flipped} \oplus 1$. If the element at the current position is the same as the current flip state, then we need to flip the current position. At this point, we check if $i+k$ exceeds the length of the array. If it does, then it is impossible to achieve the goal, and we return $-1$. Otherwise, we invert the current flip state, increase the answer by $1$, and set the element at the current position to $-1$. +Next, we traverse the array from left to right. For each position $i$, if $i \geq k$ and the element at position $i-k$ is $-1$, then the flip state of the current position should be the opposite of the flip state of the previous position. That is, $\textit{flipped} = \textit{flipped} \oplus 1$. If the element at the current position is the same as the current flip state, then we need to flip the current position. At this point, we check if $i+k$ exceeds the length of the array. If it does, then it is impossible to achieve the goal, and we return $-1$. Otherwise, we invert the current flip state, increase the answer by $1$, and set the element at the current position to $-1$. By processing all elements in the array in this manner, we can return the answer upon completion. diff --git a/solution/1000-1099/1092.Shortest Common Supersequence/README.md b/solution/1000-1099/1092.Shortest Common Supersequence/README.md index 059371c6bc367..049ce2150adc7 100644 --- a/solution/1000-1099/1092.Shortest Common Supersequence/README.md +++ b/solution/1000-1099/1092.Shortest Common Supersequence/README.md @@ -67,7 +67,7 @@ str2 = "cab" 是 "cabac" 的一个子串,因为我们可以删去 "cabac" 末 $$ f[i][j] = \begin{cases} -0 & i = 0 \text{ or } j = 0 \\ +0 & i = 0 \textit{ or } j = 0 \\ f[i - 1][j - 1] + 1 & str1[i - 1] = str2[j - 1] \\ \max(f[i - 1][j], f[i][j - 1]) & str1[i - 1] \neq str2[j - 1] \end{cases} diff --git a/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README.md b/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README.md index 098e5a5df0d03..f9333cb272f43 100644 --- a/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README.md +++ b/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README.md @@ -82,8 +82,8 @@ tags: $$ dfs(i, j) = \begin{cases} -0, & \text{if } i = j \\ -\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + \max_{i \leq t \leq k} \{arr[t]\} \max_{k < t \leq j} \{arr[t]\}\}, & \text{if } i < j +0, & \textit{if } i = j \\ +\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + \max_{i \leq t \leq k} \{arr[t]\} \max_{k < t \leq j} \{arr[t]\}\}, & \textit{if } i < j \end{cases} $$ @@ -91,8 +91,8 @@ $$ $$ dfs(i, j) = \begin{cases} -0, & \text{if } i = j \\ -\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + g[i][k] \cdot g[k + 1][j]\}, & \text{if } i < j +0, & \textit{if } i = j \\ +\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + g[i][k] \cdot g[k + 1][j]\}, & \textit{if } i < j \end{cases} $$ @@ -271,8 +271,8 @@ function mctFromLeafValues(arr: number[]): number { $$ f[i][j] = \begin{cases} -0, & \text{if } i = j \\ -\min_{i \leq k < j} \{f[i][k] + f[k + 1][j] + g[i][k] \cdot g[k + 1][j]\}, & \text{if } i < j +0, & \textit{if } i = j \\ +\min_{i \leq k < j} \{f[i][k] + f[k + 1][j] + g[i][k] \cdot g[k + 1][j]\}, & \textit{if } i < j \end{cases} $$ diff --git a/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README_EN.md b/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README_EN.md index 8f9e9930c6536..7e8fc81fd4612 100644 --- a/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README_EN.md +++ b/solution/1100-1199/1130.Minimum Cost Tree From Leaf Values/README_EN.md @@ -81,8 +81,8 @@ In summary, we can get: $$ dfs(i, j) = \begin{cases} -0, & \text{if } i = j \\ -\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + \max_{i \leq t \leq k} \{arr[t]\} \max_{k < t \leq j} \{arr[t]\}\}, & \text{if } i < j +0, & \textit{if } i = j \\ +\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + \max_{i \leq t \leq k} \{arr[t]\} \max_{k < t \leq j} \{arr[t]\}\}, & \textit{if } i < j \end{cases} $$ @@ -90,8 +90,8 @@ In the above recursive process, we can use the method of memoization search to a $$ dfs(i, j) = \begin{cases} -0, & \text{if } i = j \\ -\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + g[i][k] \cdot g[k + 1][j]\}, & \text{if } i < j +0, & \textit{if } i = j \\ +\min_{i \leq k < j} \{dfs(i, k) + dfs(k + 1, j) + g[i][k] \cdot g[k + 1][j]\}, & \textit{if } i < j \end{cases} $$ @@ -270,8 +270,8 @@ Define $f[i][j]$ to represent the minimum possible sum of all non-leaf node valu $$ f[i][j] = \begin{cases} -0, & \text{if } i = j \\ -\min_{i \leq k < j} \{f[i][k] + f[k + 1][j] + g[i][k] \cdot g[k + 1][j]\}, & \text{if } i < j +0, & \textit{if } i = j \\ +\min_{i \leq k < j} \{f[i][k] + f[k + 1][j] + g[i][k] \cdot g[k + 1][j]\}, & \textit{if } i < j \end{cases} $$ diff --git a/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md b/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md index d546d78b61320..d308081cda84e 100644 --- a/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md +++ b/solution/1100-1199/1143.Longest Common Subsequence/README_EN.md @@ -75,8 +75,8 @@ If the $i$th character of $text1$ and the $j$th character of $text2$ are the sam $$ f[i][j] = \begin{cases} -f[i - 1][j - 1] + 1, & \text{if } text1[i - 1] = text2[j - 1] \\ -\max(f[i - 1][j], f[i][j - 1]), & \text{if } text1[i - 1] \neq text2[j - 1] +f[i - 1][j - 1] + 1, & \textit{if } text1[i - 1] = text2[j - 1] \\ +\max(f[i - 1][j], f[i][j - 1]), & \textit{if } text1[i - 1] \neq text2[j - 1] \end{cases} $$ diff --git a/solution/1100-1199/1146.Snapshot Array/README.md b/solution/1100-1199/1146.Snapshot Array/README.md index 5c7ab4602280d..cd8e9c5ec7e69 100644 --- a/solution/1100-1199/1146.Snapshot Array/README.md +++ b/solution/1100-1199/1146.Snapshot Array/README.md @@ -64,7 +64,7 @@ snapshotArr.get(0,0); // 获取 snap_id = 0 的快照中 array[0] 的值,返 ### 方法一:数组 + 二分查找 -我们维护一个长度为 $\text{length}$ 的数组,数组中的每个元素是一个列表,用来存储每次设置的值以及对应的快照 ID。 +我们维护一个长度为 $\textit{length}$ 的数组,数组中的每个元素是一个列表,用来存储每次设置的值以及对应的快照 ID。 调用 `set` 方法时,将值和快照 ID 添加到对应索引的列表中。时间复杂度 $O(1)$。 diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md index eb097e9454c46..a7aa549ff9a06 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README.md @@ -265,7 +265,7 @@ function equalSubstring(s: string, t: string, maxCost: number): number { ### 方法二:双指针 -我们可以维护两个指针 $l$ 和 $r$,初始时 $l = r = 0$;维护一个变量 $\text{cost}$,表示下标区间 $[l,..r]$ 之间的 ASCII 码值的差的绝对值之和。在每一步中,我们将 $r$ 向右移动一位,然后更新 $\text{cost} = \text{cost} + |s[r] - t[r]|$。如果 $\text{cost} \gt \text{maxCost}$,那么我们就循环将 $l$ 向右移动一位,并且减少 $\text{cost}$ 的值,直到 $\text{cost} \leq \text{maxCost}$。然后我们更新答案,即 $\text{ans} = \max(\text{ans}, r - l + 1)$。 +我们可以维护两个指针 $l$ 和 $r$,初始时 $l = r = 0$;维护一个变量 $\textit{cost}$,表示下标区间 $[l,..r]$ 之间的 ASCII 码值的差的绝对值之和。在每一步中,我们将 $r$ 向右移动一位,然后更新 $\textit{cost} = \textit{cost} + |s[r] - t[r]|$。如果 $\textit{cost} \gt \textit{maxCost}$,那么我们就循环将 $l$ 向右移动一位,并且减少 $\textit{cost}$ 的值,直到 $\textit{cost} \leq \textit{maxCost}$。然后我们更新答案,即 $\textit{ans} = \max(\textit{ans}, r - l + 1)$。 最后返回答案即可。 @@ -382,7 +382,7 @@ function equalSubstring(s: string, t: string, maxCost: number): number { 在方法二中,双指针维护的区间可能变短,也可能变长,由于题目只需要求出最大长度,我们可以维护一个单调变长的区间。 -具体地,我们用两个指针 $l$ 和 $r$ 指向区间的左右端点,初始时 $l = r = 0$。在每一步中,我们将 $r$ 向右移动一位,然后更新 $\text{cost} = \text{cost} + |s[r] - t[r]|$。如果 $\text{cost} \gt \text{maxCost}$,那么我们就将 $l$ 向右移动一位,并且减少 $\text{cost}$ 的值。 +具体地,我们用两个指针 $l$ 和 $r$ 指向区间的左右端点,初始时 $l = r = 0$。在每一步中,我们将 $r$ 向右移动一位,然后更新 $\textit{cost} = \textit{cost} + |s[r] - t[r]|$。如果 $\textit{cost} \gt \textit{maxCost}$,那么我们就将 $l$ 向右移动一位,并且减少 $\textit{cost}$ 的值。 最后返回 $n - l$ 即可。 diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md index 4e1b62096976b..4a1edd94ae9d8 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/README_EN.md @@ -262,7 +262,7 @@ function equalSubstring(s: string, t: string, maxCost: number): number { ### Solution 2: Two Pointers -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)$. +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)$. Finally, return the answer. @@ -379,7 +379,7 @@ function equalSubstring(s: string, t: string, maxCost: number): number { 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. -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}$. +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}$. Finally, return $n - l$. diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md index ccc4d129ec2fa..3afed1acfac9b 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README.md @@ -80,11 +80,11 @@ tags: ### 方法一:模拟 -我们创建一个矩阵 $g$ 来存放操作的结果。对于 $\text{indices}$ 中的每一对 $(r_i, c_i)$,我们将矩阵第 $r_i$ 行的所有数加 $1$,第 $c_i$ 列的所有元素加 $1$。 +我们创建一个矩阵 $g$ 来存放操作的结果。对于 $\textit{indices}$ 中的每一对 $(r_i, c_i)$,我们将矩阵第 $r_i$ 行的所有数加 $1$,第 $c_i$ 列的所有元素加 $1$。 模拟结束后,遍历矩阵,统计奇数的个数。 -时间复杂度 $O(k \times (m + n) + m \times n)$,空间复杂度 $O(m \times n)$。其中 $k$ 为 $\text{indices}$ 的长度。 +时间复杂度 $O(k \times (m + n) + m \times n)$,空间复杂度 $O(m \times n)$。其中 $k$ 为 $\textit{indices}$ 的长度。 @@ -190,11 +190,11 @@ func oddCells(m int, n int, indices [][]int) int { ### 方法二:空间优化 -我们可以使用行数组 $\text{row}$ 和列数组 $\text{col}$ 来记录每一行、每一列被增加的次数。对于 $\text{indices}$ 中的每一对 $(r_i, c_i)$,我们将 $\text{row}[r_i]$ 和 $\text{col}[c_i]$ 分别加 $1$。 +我们可以使用行数组 $\textit{row}$ 和列数组 $\textit{col}$ 来记录每一行、每一列被增加的次数。对于 $\textit{indices}$ 中的每一对 $(r_i, c_i)$,我们将 $\textit{row}[r_i]$ 和 $\textit{col}[c_i]$ 分别加 $1$。 -操作结束后,可以算出 $(i, j)$ 位置的计数为 $\text{row}[i]+\text{col}[j]$。遍历矩阵,统计奇数的个数。 +操作结束后,可以算出 $(i, j)$ 位置的计数为 $\textit{row}[i]+\textit{col}[j]$。遍历矩阵,统计奇数的个数。 -时间复杂度 $O(k + m \times n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\text{indices}$ 的长度。 +时间复杂度 $O(k + m \times n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\textit{indices}$ 的长度。 @@ -287,11 +287,11 @@ func oddCells(m int, n int, indices [][]int) int { ### 方法三:数学优化 -我们注意到,只有当 $\text{row}[i]$ 和 $\text{col}[j]$ 中恰好为“一奇一偶”时,矩阵 $(i, j)$ 位置的数才会是奇数。 +我们注意到,只有当 $\textit{row}[i]$ 和 $\textit{col}[j]$ 中恰好为“一奇一偶”时,矩阵 $(i, j)$ 位置的数才会是奇数。 -我们统计 $\text{row}$ 中的奇数个数,记为 $\text{cnt1}$;而 $\text{col}$ 中的奇数个数,记为 $\text{cnt2}$。那么最终得到的奇数个数为 $\text{cnt1} \times (n - \text{cnt2}) + \text{cnt2} \times (m - \text{cnt1})$。 +我们统计 $\textit{row}$ 中的奇数个数,记为 $\textit{cnt1}$;而 $\textit{col}$ 中的奇数个数,记为 $\textit{cnt2}$。那么最终得到的奇数个数为 $\textit{cnt1} \times (n - \textit{cnt2}) + \textit{cnt2} \times (m - \textit{cnt1})$。 -时间复杂度 $O(k + m + n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\text{indices}$ 的长度。 +时间复杂度 $O(k + m + n)$,空间复杂度 $O(m + n)$。其中 $k$ 为 $\textit{indices}$ 的长度。 diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md index 0f77e493498a6..af5b2216ebf56 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/README_EN.md @@ -71,11 +71,11 @@ The final matrix is [[1,3,1],[1,3,1]], which contains 6 odd numbers. ### Solution 1: Simulation -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. +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. After the simulation ends, we traverse the matrix and count the number of odd numbers. -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}$. +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}$. @@ -181,11 +181,11 @@ func oddCells(m int, n int, indices [][]int) int { ### Solution 2: Space Optimization -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. +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. -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. +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. -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}$. +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}$. @@ -278,11 +278,11 @@ func oddCells(m int, n int, indices [][]int) int { ### Solution 3: Mathematical Optimization -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. +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. -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})$. +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})$. -The time complexity is $O(k + m + n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\text{indices}$. +The time complexity is $O(k + m + n)$, and the space complexity is $O(m + n)$. Here, $k$ is the length of $\textit{indices}$. diff --git "a/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README.md" "b/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README.md" index 5b3261fb50fa9..7eec87147e5f1 100644 --- "a/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README.md" +++ "b/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README.md" @@ -188,10 +188,10 @@ function numOfWays(n: number): number { 因此,我们定义 $f[i][j]$ 表示前 $i$ 行中,第 $i$ 行的涂色状态为 $j$ 的方案数。状态 $f[i][j]$ 由 $f[i - 1][k]$ 转移而来,其中 $k$ 是第 $i - 1$ 行的涂色状态,且 $k$ 和 $j$ 满足不同颜色相邻的要求。即: $$ -f[i][j] = \sum_{k \in \text{valid}(j)} f[i - 1][k] +f[i][j] = \sum_{k \in \textit{valid}(j)} f[i - 1][k] $$ -其中 $\text{valid}(j)$ 表示状态 $j$ 的所有合法前驱状态。 +其中 $\textit{valid}(j)$ 表示状态 $j$ 的所有合法前驱状态。 最终的答案即为 $f[n][j]$ 的总和,其中 $j$ 是任意合法的状态。 diff --git "a/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README_EN.md" "b/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README_EN.md" index 6470c015003ed..901db62d70f83 100644 --- "a/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README_EN.md" +++ "b/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/README_EN.md" @@ -166,10 +166,10 @@ We notice that the grid only has $3$ columns, so there are at most $3^3=27$ diff 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: $$ -f[i][j] = \sum_{k \in \text{valid}(j)} f[i - 1][k] +f[i][j] = \sum_{k \in \textit{valid}(j)} f[i - 1][k] $$ -where $\text{valid}(j)$ represents all legal predecessor states of state $j$. +where $\textit{valid}(j)$ represents all legal predecessor states of state $j$. The final answer is the sum of $f[n][j]$, where $j$ is any legal state. diff --git a/solution/1500-1599/1510.Stone Game IV/README.md b/solution/1500-1599/1510.Stone Game IV/README.md index 1b8702110aa62..b268f3b525de9 100644 --- a/solution/1500-1599/1510.Stone Game IV/README.md +++ b/solution/1500-1599/1510.Stone Game IV/README.md @@ -242,8 +242,8 @@ function winnerSquareGame(n: number): boolean { $$ f[i]= \begin{cases} -true, & \text{if } \exists j \in [1,..i], j^2 \leq i \text{ and } f[i-j^2] = false\\ -false, & \text{otherwise} +true, & \textit{if } \exists j \in [1,..i], j^2 \leq i \textit{ and } f[i-j^2] = false\\ +false, & \textit{otherwise} \end{cases} $$ diff --git a/solution/1500-1599/1550.Three Consecutive Odds/README.md b/solution/1500-1599/1550.Three Consecutive Odds/README.md index 36d12e53d91a7..a6129a9b336ce 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/README.md +++ b/solution/1500-1599/1550.Three Consecutive Odds/README.md @@ -53,13 +53,13 @@ tags: ### 方法一:遍历 + 计数 -我们用一个变量 $\text{cnt}$ 记录当前连续奇数的个数。 +我们用一个变量 $\textit{cnt}$ 记录当前连续奇数的个数。 -接下来,我们遍历数组,如果当前元素是奇数,则 $\text{cnt}$ 加一,如果 $\text{cnt}$ 等于 3,则返回 $\text{True}$。如果当前元素是偶数,则 $\text{cnt}$ 置零。 +接下来,我们遍历数组,如果当前元素是奇数,则 $\textit{cnt}$ 加一,如果 $\textit{cnt}$ 等于 3,则返回 $\textit{True}$。如果当前元素是偶数,则 $\textit{cnt}$ 置零。 -遍历结束后,如果没有找到连续三个奇数,则返回 $\text{False}$。 +遍历结束后,如果没有找到连续三个奇数,则返回 $\textit{False}$。 -时间复杂度 $O(n)$,其中 $n$ 是数组 $\text{arr}$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$。 @@ -167,9 +167,9 @@ function threeConsecutiveOdds(arr: number[]): boolean { 根据位运算的性质,两个数进行按位与运算是奇数,当且仅当两个数都是奇数。如果有连续三个数按位与运算的结果是奇数,那么这三个数都是奇数。 -因此,我们只需要遍历数组,判断是否存在连续三个数的按位与结果是否是奇数即可,如果存在则返回 $\text{True}$,否则返回 $\text{False}$。 +因此,我们只需要遍历数组,判断是否存在连续三个数的按位与结果是否是奇数即可,如果存在则返回 $\textit{True}$,否则返回 $\textit{False}$。 -时间复杂度 $O(n)$,其中 $n$ 是数组 $\text{arr}$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md b/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md index f6b6381f1ceea..79052357c5e52 100644 --- a/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md +++ b/solution/1500-1599/1550.Three Consecutive Odds/README_EN.md @@ -53,13 +53,13 @@ Given an integer array arr, return true if there ### Solution 1: Iteration + Counting -We use a variable $\text{cnt}$ to record the current count of consecutive odd numbers. +We use a variable $\textit{cnt}$ to record the current count of consecutive odd numbers. -Next, we iterate through the array. If the current element is odd, then $\text{cnt}$ is incremented by one. If $\text{cnt}$ equals 3, then return $\text{True}$. If the current element is even, then $\text{cnt}$ is reset to zero. +Next, we iterate through the array. If the current element is odd, then $\textit{cnt}$ is incremented by one. If $\textit{cnt}$ equals 3, then return $\textit{True}$. If the current element is even, then $\textit{cnt}$ is reset to zero. -After the iteration, if three consecutive odd numbers are not found, then return $\text{False}$. +After the iteration, if three consecutive odd numbers are not found, then return $\textit{False}$. -The time complexity is $O(n)$, where $n$ is the length of the array $\text{arr}$. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(1)$. @@ -167,9 +167,9 @@ function threeConsecutiveOdds(arr: number[]): boolean { Based on the properties of bitwise operations, the result of a bitwise AND operation between two numbers is odd if and only if both numbers are odd. If there are three consecutive numbers whose bitwise AND result is odd, then these three numbers are all odd. -Therefore, we only need to iterate through the array and check if there exists three consecutive numbers whose bitwise AND result is odd. If such numbers exist, return $\text{True}$; otherwise, return $\text{False}$. +Therefore, we only need to iterate through the array and check if there exists three consecutive numbers whose bitwise AND result is odd. If such numbers exist, return $\textit{True}$; otherwise, return $\textit{False}$. -The time complexity is $O(n)$, where $n$ is the length of the array $\text{arr}$. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(1)$. diff --git a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md index 254a569e2af6a..6aa40c35c2a7b 100644 --- a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md +++ b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md @@ -66,7 +66,7 @@ tags: 我们注意到,任意两球间的最小磁力越大,能够放入的球的数量就越少,这存在着单调性。我们可以使用二分查找,找到最大的最小磁力,使得能够放入的球的数量不小于 $m$。 -我们首先对篮子的位置进行排序,然后使用二分查找的方法,定义二分查找的左边界 $l = 1$,右边界 $r = \text{position}[n - 1]$,其中 $n$ 为篮子的数量。在每次二分查找的过程中,我们计算取中值 $m = (l + r + 1) / 2$,然后判断是否存在一种放置球的方法,使得能够放入的球的数量不小于 $m$。 +我们首先对篮子的位置进行排序,然后使用二分查找的方法,定义二分查找的左边界 $l = 1$,右边界 $r = \textit{position}[n - 1]$,其中 $n$ 为篮子的数量。在每次二分查找的过程中,我们计算取中值 $m = (l + r + 1) / 2$,然后判断是否存在一种放置球的方法,使得能够放入的球的数量不小于 $m$。 问题转换为判断一个给定的最小磁力 $f$ 是否能够放入 $m$ 个球。我们可以从左到右遍历篮子的位置,如果上一个球的位置与当前篮子的位置的距离大于等于 $f$,则说明可以在当前篮子放置一个球。最后判断放置的球的数量是否不小于 $m$ 即可。 diff --git a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md index 1baf8b261e93c..79b3601a09f74 100644 --- a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md +++ b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md @@ -64,7 +64,7 @@ tags: We notice that the greater the minimum magnetic force between any two balls, the fewer balls can be placed, which exhibits monotonicity. We can use binary search to find the maximum minimum magnetic force that allows the number of balls not less than $m$ to be placed. -First, we sort the positions of the baskets, and then use binary search with the left boundary $l = 1$ and the right boundary $r = \text{position}[n - 1]$, where $n$ is the number of baskets. In each binary search iteration, we calculate the midpoint $m = (l + r + 1) / 2$, and then determine if there is a way to place the balls such that the number of balls placed is not less than $m$. +First, we sort the positions of the baskets, and then use binary search with the left boundary $l = 1$ and the right boundary $r = \textit{position}[n - 1]$, where $n$ is the number of baskets. In each binary search iteration, we calculate the midpoint $m = (l + r + 1) / 2$, and then determine if there is a way to place the balls such that the number of balls placed is not less than $m$. The problem is transformed into determining whether a given minimum magnetic force $f$ can place $m$ balls. We can traverse the positions of the baskets from left to right, and if the distance between the position of the last ball and the current basket's position is greater than or equal to $f$, it indicates that a ball can be placed in the current basket. Finally, we check if the number of balls placed is not less than $m$. diff --git a/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README.md b/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README.md index 652b8db2dea7a..f5722029abf42 100644 --- a/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README.md +++ b/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README.md @@ -129,10 +129,10 @@ tags: 状态转移方程为: $$ -f[i] = \min_{j \in [i - maxBoxes, i - 1]} \left(f[j] + \sum_{k = j + 1}^i \text{cost}(k)\right) +f[i] = \min_{j \in [i - maxBoxes, i - 1]} \left(f[j] + \sum_{k = j + 1}^i \textit{cost}(k)\right) $$ -其中 $\sum_{k = j + 1}^i \text{cost}(k)$ 表示通过一次运输,把 $[j+1,..i]$ 这些箱子送往对应的码头所需要的行程数。这部分行程数可以通过前缀和快速计算出来。 +其中 $\sum_{k = j + 1}^i \textit{cost}(k)$ 表示通过一次运输,把 $[j+1,..i]$ 这些箱子送往对应的码头所需要的行程数。这部分行程数可以通过前缀和快速计算出来。 简单举个例子,假设我们取出了 $1, 2, 3$ 这三个箱子,需要送往 $4, 4, 5$ 这三个码头,那么我们首先要从仓库到 $4$ 号码头,然后再从 $4$ 号码头到 $5$ 号码头,最后再从 $5$ 号码头回到仓库。可以发现,从仓库到码头,以及从码头到仓库,需要花费 $2$ 趟行程,而从码头到码头的行程数,取决于相邻两个码头是否相同,如果不相同,那么行程数会增加 $1$,否则不变。因此,我们可以通过前缀和,计算出码头之间的行程数,再加上首尾两趟行程,就能把 $[j+1,..i]$ 这些箱子送往对应的码头所需要的行程数计算出来。 diff --git a/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README_EN.md b/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README_EN.md index 4307ebf39c334..724de9eb78581 100644 --- a/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README_EN.md +++ b/solution/1600-1699/1687.Delivering Boxes from Storage to Ports/README_EN.md @@ -112,10 +112,10 @@ Therefore, we can enumerate the index $j$ of the last box transported in the las The state transition equation is: $$ -f[i] = \min_{j \in [i - maxBoxes, i - 1]} \left(f[j] + \sum_{k = j + 1}^i \text{cost}(k)\right) +f[i] = \min_{j \in [i - maxBoxes, i - 1]} \left(f[j] + \sum_{k = j + 1}^i \textit{cost}(k)\right) $$ -Where $\sum_{k = j + 1}^i \text{cost}(k)$ represents the number of trips required to deliver the boxes in $[j+1,..i]$ to their corresponding docks in one trip. This part of the trip count can be quickly calculated using prefix sums. +Where $\sum_{k = j + 1}^i \textit{cost}(k)$ represents the number of trips required to deliver the boxes in $[j+1,..i]$ to their corresponding docks in one trip. This part of the trip count can be quickly calculated using prefix sums. For example, suppose we take out boxes $1, 2, 3$ and need to deliver them to docks $4, 4, 5$. We first go from the warehouse to dock $4$, then from dock $4$ to dock $5$, and finally from dock $5$ back to the warehouse. It can be seen that it takes $2$ trips to go from the warehouse to the dock and from the dock back to the warehouse. The number of trips from dock to dock depends on whether the two adjacent docks are the same. If they are not the same, the number of trips will increase by $1$, otherwise it remains the same. Therefore, we can calculate the number of trips between docks using prefix sums, and add two trips for the start and end, to calculate the number of trips required to deliver the boxes in $[j+1,..i]$ to their corresponding docks. diff --git a/solution/1700-1799/1720.Decode XORed Array/README.md b/solution/1700-1799/1720.Decode XORed Array/README.md index 37f409d755300..560febe26024a 100644 --- a/solution/1700-1799/1720.Decode XORed Array/README.md +++ b/solution/1700-1799/1720.Decode XORed Array/README.md @@ -66,22 +66,22 @@ tags: 根据题目描述,有: $$ -\text{encoded}[i] = \text{arr}[i] \oplus \text{arr}[i + 1] +\textit{encoded}[i] = \textit{arr}[i] \oplus \textit{arr}[i + 1] $$ -如果我们将等式两边同时异或上 $\text{arr}[i]$,那么就会得到: +如果我们将等式两边同时异或上 $\textit{arr}[i]$,那么就会得到: $$ -\text{arr}[i] \oplus \text{arr}[i] \oplus \text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i] +\textit{arr}[i] \oplus \textit{arr}[i] \oplus \textit{arr}[i + 1] = \textit{arr}[i] \oplus \textit{encoded}[i] $$ 即: $$ -\text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i] +\textit{arr}[i + 1] = \textit{arr}[i] \oplus \textit{encoded}[i] $$ -根据上述推导,我们可以从 $\text{first}$ 开始,依次计算出数组 $\text{arr}$ 的每一个元素。 +根据上述推导,我们可以从 $\textit{first}$ 开始,依次计算出数组 $\textit{arr}$ 的每一个元素。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。 diff --git a/solution/1700-1799/1720.Decode XORed Array/README_EN.md b/solution/1700-1799/1720.Decode XORed Array/README_EN.md index 625288c83a8a1..3d92af479bece 100644 --- a/solution/1700-1799/1720.Decode XORed Array/README_EN.md +++ b/solution/1700-1799/1720.Decode XORed Array/README_EN.md @@ -64,22 +64,22 @@ tags: Based on the problem description, we have: $$ -\text{encoded}[i] = \text{arr}[i] \oplus \text{arr}[i + 1] +\textit{encoded}[i] = \textit{arr}[i] \oplus \textit{arr}[i + 1] $$ -If we XOR both sides of the equation with $\text{arr}[i]$, we get: +If we XOR both sides of the equation with $\textit{arr}[i]$, we get: $$ -\text{arr}[i] \oplus \text{arr}[i] \oplus \text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i] +\textit{arr}[i] \oplus \textit{arr}[i] \oplus \textit{arr}[i + 1] = \textit{arr}[i] \oplus \textit{encoded}[i] $$ Which simplifies to: $$ -\text{arr}[i + 1] = \text{arr}[i] \oplus \text{encoded}[i] +\textit{arr}[i + 1] = \textit{arr}[i] \oplus \textit{encoded}[i] $$ -Following the derivation above, we can start with $\text{first}$ and sequentially calculate every element of the array $\text{arr}$. +Following the derivation above, we can start with $\textit{first}$ and sequentially calculate every element of the array $\textit{arr}$. The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array. diff --git a/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README.md b/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README.md index 2026613cd79d8..f97322ec25b37 100644 --- a/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README.md +++ b/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README.md @@ -69,7 +69,7 @@ tags: $$ \begin{aligned} -\text{ans} &= (a_1 \wedge b_1) \oplus (a_1 \wedge b_2) ... (a_1 \wedge b_m) \\ +\textit{ans} &= (a_1 \wedge b_1) \oplus (a_1 \wedge b_2) ... (a_1 \wedge b_m) \\ &\quad \oplus (a_2 \wedge b_1) \oplus (a_2 \wedge b_2) ... (a_2 \wedge b_m) \\ &\quad \oplus \cdots \\ &\quad \oplus (a_n \wedge b_1) \oplus (a_n \wedge b_2) ... (a_n \wedge b_m) \\ @@ -79,7 +79,7 @@ $$ 由于布尔代数中,异或运算就是不进位的加法,与运算就是乘法,所以上式可以简化为: $$ -\text{ans} = (a_1 \oplus a_2 \oplus \cdots \oplus a_n) \wedge (b_1 \oplus b_2 \oplus \cdots \oplus b_m) +\textit{ans} = (a_1 \oplus a_2 \oplus \cdots \oplus a_n) \wedge (b_1 \oplus b_2 \oplus \cdots \oplus b_m) $$ 即,数组 $arr1$ 的异或和与数组 $arr2$ 的异或和的与运算结果。 diff --git a/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README_EN.md b/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README_EN.md index a3ae9b3edfd6b..04e6b209b39da 100644 --- a/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README_EN.md +++ b/solution/1800-1899/1835.Find XOR Sum of All Pairs Bitwise AND/README_EN.md @@ -70,7 +70,7 @@ Assume that the elements of array $arr1$ are $a_1, a_2, ..., a_n$, and the eleme $$ \begin{aligned} -\text{ans} &= (a_1 \wedge b_1) \oplus (a_1 \wedge b_2) ... (a_1 \wedge b_m) \\ +\textit{ans} &= (a_1 \wedge b_1) \oplus (a_1 \wedge b_2) ... (a_1 \wedge b_m) \\ &\quad \oplus (a_2 \wedge b_1) \oplus (a_2 \wedge b_2) ... (a_2 \wedge b_m) \\ &\quad \oplus \cdots \\ &\quad \oplus (a_n \wedge b_1) \oplus (a_n \wedge b_2) ... (a_n \wedge b_m) \\ @@ -80,7 +80,7 @@ $$ Since in Boolean algebra, the XOR operation is addition without carry, and the AND operation is multiplication, the above formula can be simplified as: $$ -\text{ans} = (a_1 \oplus a_2 \oplus \cdots \oplus a_n) \wedge (b_1 \oplus b_2 \oplus \cdots \oplus b_m) +\textit{ans} = (a_1 \oplus a_2 \oplus \cdots \oplus a_n) \wedge (b_1 \oplus b_2 \oplus \cdots \oplus b_m) $$ That is, the bitwise AND of the XOR sum of array $arr1$ and the XOR sum of array $arr2$. diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md index b429e2b9e0d9d..6c7f379d1ddbc 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md @@ -87,7 +87,7 @@ tags: 在二分查找之前,我们需要先判断是否有可能在规定时间内到达。如果列车数量大于向上取整的规定时间,那么一定无法在规定时间内到达,直接返回 $-1$。 -接下来,我们定义二分的左右边界为 $l = 1$, $r = 10^7 + 1$,然后我们每次取中间值 $\text{mid} = \frac{l + r}{2}$,判断是否满足条件。如果满足条件,我们将右边界移动到 $\text{mid}$,否则将左边界移动到 $\text{mid} + 1$。 +接下来,我们定义二分的左右边界为 $l = 1$, $r = 10^7 + 1$,然后我们每次取中间值 $\textit{mid} = \frac{l + r}{2}$,判断是否满足条件。如果满足条件,我们将右边界移动到 $\textit{mid}$,否则将左边界移动到 $\textit{mid} + 1$。 问题转化为判断一个速度值 $v$ 是否能够在规定时间内到达。我们可以遍历每一趟列车,计算每一趟列车的运行时间 $t = \frac{d}{v}$,如果是最后一趟列车,我们直接加上 $t$,否则我们向上取整加上 $t$。最后判断总时间是否小于等于规定时间,如果是则说明满足条件。 diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md index 48acea1aa2c99..a5c22968be5ce 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md @@ -87,7 +87,7 @@ We notice that if a speed value $v$ allows us to arrive within the stipulated ti Before conducting the binary search, we need to first determine if it is possible to arrive within the stipulated time. If the number of trains is greater than the ceiling of the stipulated time, then it is definitely impossible to arrive within the stipulated time, and we should directly return $-1$. -Next, we define the left and right boundaries for the binary search as $l = 1$, $r = 10^7 + 1$, and then we take the middle value $\text{mid} = \frac{l + r}{2}$ each time to check if it meets the condition. If it does, we move the right boundary to $\text{mid}$; otherwise, we move the left boundary to $\text{mid} + 1$. +Next, we define the left and right boundaries for the binary search as $l = 1$, $r = 10^7 + 1$, and then we take the middle value $\textit{mid} = \frac{l + r}{2}$ each time to check if it meets the condition. If it does, we move the right boundary to $\textit{mid}$; otherwise, we move the left boundary to $\textit{mid} + 1$. The problem is transformed into determining whether a speed value $v$ can allow us to arrive within the stipulated time. We can traverse each train trip, calculate the running time of each trip $t = \frac{d}{v}$, if it is the last trip, we directly add $t$; otherwise, we round up and add $t$. Finally, we check if the total time is less than or equal to the stipulated time, if so, it means the condition is met. diff --git a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md index c71aa41c7ab4b..47a865c2a7a1e 100644 --- a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md +++ b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md @@ -91,7 +91,7 @@ tags: 由于我们可以选择跳过或者不跳过第 $i$ 条道路的休息时间,因此我们可以列出状态转移方程: $$ -f[i][j]=\min\left\{\begin{aligned} \lceil f[i-1][j]+\frac{d_i}{s}\rceil & \text{不跳过第 $i$ 条道路的休息时间} \\ f[i-1][j-1]+\frac{d_i}{s} & \text{跳过第 $i$ 条道路的休息时间} \end{aligned}\right. +f[i][j]=\min\left\{\begin{aligned} \lceil f[i-1][j]+\frac{d_i}{s}\rceil & \textit{不跳过第 $i$ 条道路的休息时间} \\ f[i-1][j-1]+\frac{d_i}{s} & \textit{跳过第 $i$ 条道路的休息时间} \end{aligned}\right. $$ 其中 $\lceil x\rceil$ 表示将 $x$ 向上取整。需要注意的是,由于我们需要保证恰好跳过 $j$ 次休息时间,因此我们必须有 $j\le i$;另外,如果 $j=0$,不能跳过任何休息时间。 diff --git a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md index 0484509ca530a..0599c4e55362d 100644 --- a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md +++ b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md @@ -89,7 +89,7 @@ We define $f[i][j]$ as the shortest time considering the first $i$ roads and exa Since we can choose to skip or not skip the rest time of the $i$-th road, we can list the state transition equation: $$ -f[i][j]=\min\left\{\begin{aligned} \lceil f[i-1][j]+\frac{d_i}{s}\rceil & \text{Do not skip the rest time of the $i$-th road} \\ f[i-1][j-1]+\frac{d_i}{s} & \text{Skip the rest time of the $i$-th road} \end{aligned}\right. +f[i][j]=\min\left\{\begin{aligned} \lceil f[i-1][j]+\frac{d_i}{s}\rceil & \textit{Do not skip the rest time of the $i$-th road} \\ f[i-1][j-1]+\frac{d_i}{s} & \textit{Skip the rest time of the $i$-th road} \end{aligned}\right. $$ Where $\lceil x\rceil$ represents rounding $x$ up. It should be noted that since we need to ensure that exactly $j$ rest times are skipped, we must have $j\le i$; moreover, if $j=0$, no rest time can be skipped. diff --git a/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README.md b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README.md index 3af675abd5697..5bbeaa09e599d 100644 --- a/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README.md +++ b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README.md @@ -69,10 +69,10 @@ tags: 因此,我们定义 $f[i][j]$ 表示前 $i$ 列中,第 $i$ 列的涂色状态为 $j$ 的方案数。状态 $f[i][j]$ 由 $f[i - 1][k]$ 转移而来,其中 $k$ 是第 $i - 1$ 列的涂色状态,且 $k$ 和 $j$ 满足不同颜色相邻的要求。即: $$ -f[i][j] = \sum_{k \in \text{valid}(j)} f[i - 1][k] +f[i][j] = \sum_{k \in \textit{valid}(j)} f[i - 1][k] $$ -其中 $\text{valid}(j)$ 表示状态 $j$ 的所有合法前驱状态。 +其中 $\textit{valid}(j)$ 表示状态 $j$ 的所有合法前驱状态。 最终的答案即为 $f[n][j]$ 的总和,其中 $j$ 是任意合法的状态。 diff --git a/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README_EN.md b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README_EN.md index d27ad61cb964e..9f89d7d33664f 100644 --- a/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README_EN.md +++ b/solution/1900-1999/1931.Painting a Grid With Three Different Colors/README_EN.md @@ -67,10 +67,10 @@ We notice that the number of rows in the grid does not exceed $5$, so there are Therefore, we define $f[i][j]$ to represent the number of schemes in the first $i$ columns, where the coloring state of the $i$th column 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 column, and $k$ and $j$ meet the requirement of different colors being adjacent. That is: $$ -f[i][j] = \sum_{k \in \text{valid}(j)} f[i - 1][k] +f[i][j] = \sum_{k \in \textit{valid}(j)} f[i - 1][k] $$ -where $\text{valid}(j)$ represents all legal predecessor states of state $j$. +where $\textit{valid}(j)$ represents all legal predecessor states of state $j$. The final answer is the sum of $f[n][j]$, where $j$ is any legal state. diff --git a/solution/1900-1999/1958.Check if Move is Legal/README.md b/solution/1900-1999/1958.Check if Move is Legal/README.md index e223c10567665..c1eb86ca94cd3 100644 --- a/solution/1900-1999/1958.Check if Move is Legal/README.md +++ b/solution/1900-1999/1958.Check if Move is Legal/README.md @@ -70,9 +70,9 @@ tags: ### 方法一:枚举 -我们枚举所有可能的方向,对于每个方向 $(a, b)$,我们从 $(\textit{rMove}, \textit{cMove})$ 出发,用一个变量 $\textit{cnt}$ 记录我们走过的格子数,如果我们在走的过程中遇到了颜色为 $\textit{color}$ 的格子,且 $\textit{cnt} > 1$,那么我们就找到了一个好线段,返回 $\text{true}$。 +我们枚举所有可能的方向,对于每个方向 $(a, b)$,我们从 $(\textit{rMove}, \textit{cMove})$ 出发,用一个变量 $\textit{cnt}$ 记录我们走过的格子数,如果我们在走的过程中遇到了颜色为 $\textit{color}$ 的格子,且 $\textit{cnt} > 1$,那么我们就找到了一个好线段,返回 $\textit{true}$。 -枚举结束后,如果我们没有找到任何好线段,那么返回 $\text{false}$。 +枚举结束后,如果我们没有找到任何好线段,那么返回 $\textit{false}$。 时间复杂度 $O(m + n)$,其中 $m$ 为 $\textit{board}$ 的行数,而 $n$ 为 $\textit{board}$ 的列数,本题中 $m = n = 8$。空间复杂度 $O(1)$。 diff --git a/solution/1900-1999/1958.Check if Move is Legal/README_EN.md b/solution/1900-1999/1958.Check if Move is Legal/README_EN.md index 14a577ecb54ea..ec946ff5fcbab 100644 --- a/solution/1900-1999/1958.Check if Move is Legal/README_EN.md +++ b/solution/1900-1999/1958.Check if Move is Legal/README_EN.md @@ -64,9 +64,9 @@ The two good lines with the chosen cell as an endpoint are annotated above with ### Solution 1: Enumeration -We enumerate all possible directions. For each direction $(a, b)$, we start from $(\textit{rMove}, \textit{cMove})$ and use a variable $\textit{cnt}$ to record the number of cells we have passed. If, during our traversal, we encounter a cell of color $\textit{color}$ and $\textit{cnt} > 1$, then we have found a good line segment and return $\text{true}$. +We enumerate all possible directions. For each direction $(a, b)$, we start from $(\textit{rMove}, \textit{cMove})$ and use a variable $\textit{cnt}$ to record the number of cells we have passed. If, during our traversal, we encounter a cell of color $\textit{color}$ and $\textit{cnt} > 1$, then we have found a good line segment and return $\textit{true}$. -If no good line segments are found after the enumeration, we return $\text{false}$. +If no good line segments are found after the enumeration, we return $\textit{false}$. The time complexity is $O(m + n)$, where $m$ is the number of rows and $n$ is the number of columns in $\textit{board}$, with $m = n = 8$ in this problem. The space complexity is $O(1)$. diff --git a/solution/1900-1999/1971.Find if Path Exists in Graph/README.md b/solution/1900-1999/1971.Find if Path Exists in Graph/README.md index f6e85b3808683..b31c2f87b0253 100644 --- a/solution/1900-1999/1971.Find if Path Exists in Graph/README.md +++ b/solution/1900-1999/1971.Find if Path Exists in Graph/README.md @@ -268,7 +268,7 @@ fn dfs(i: usize, destination: usize, vis: &mut Vec, g: &Vec 具体地,我们定义一个队列 $q$,初始时将 `source` 加入队列。另外,我们用一个集合 `vis` 记录已经访问过的顶点,避免重复访问。 -接下来,我们不断从队列中取出顶点 $i$,如果 $i = \text{destination}$,则说明存在从 `source` 到 `destination` 的路径,返回 `true`。否则,我们遍历 $i$ 的所有邻接顶点 $j$,如果 $j$ 没有被访问过,我们将 $j$ 加入队列 $q$,并且标记 $j$ 为已访问。 +接下来,我们不断从队列中取出顶点 $i$,如果 $i = \textit{destination}$,则说明存在从 `source` 到 `destination` 的路径,返回 `true`。否则,我们遍历 $i$ 的所有邻接顶点 $j$,如果 $j$ 没有被访问过,我们将 $j$ 加入队列 $q$,并且标记 $j$ 为已访问。 最后,如果队列为空,说明不存在从 `source` 到 `destination` 的路径,返回 `false`。 diff --git a/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md b/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md index 7c77c68d6fb4a..90c191aa18fcb 100644 --- a/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md +++ b/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md @@ -266,7 +266,7 @@ We can also use BFS to determine whether there is a path from `source` to `desti Specifically, we define a queue $q$ and initially add `source` to the queue. In addition, we use a set `vis` to record the vertices that have been visited to avoid repeated visits. -Next, we continuously take out the vertex $i$ from the queue. If $i = \text{destination}$, it means that there is a path from `source` to `destination`, and we return `true`. Otherwise, we traverse all adjacent vertices $j$ of $i$. If $j$ has not been visited, we add $j$ to the queue $q$ and mark $j$ as visited. +Next, we continuously take out the vertex $i$ from the queue. If $i = \textit{destination}$, it means that there is a path from `source` to `destination`, and we return `true`. Otherwise, we traverse all adjacent vertices $j$ of $i$. If $j$ has not been visited, we add $j$ to the queue $q$ and mark $j$ as visited. Finally, if the queue is empty, it means that there is no path from `source` to `destination`, and we return `false`. diff --git a/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README.md b/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README.md index b2eaf9741054e..4a3a003aeb309 100644 --- a/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README.md +++ b/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README.md @@ -92,7 +92,7 @@ tags: 设 $f[i][j]$ 表示前 $i$ 行是否能选出元素和为 $j$,则有状态转移方程: $$ -f[i][j] = \begin{cases} 1 & \text{如果存在 } x \in row[i] \text{ 使得 } f[i - 1][j - x] = 1 \\ 0 & \text{否则} \end{cases} +f[i][j] = \begin{cases} 1 & \textit{如果存在 } x \in row[i] \textit{ 使得 } f[i - 1][j - x] = 1 \\ 0 & \textit{否则} \end{cases} $$ 其中 $row[i]$ 表示第 $i$ 行的元素集合。 diff --git a/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README_EN.md b/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README_EN.md index 3edbf948ad8fc..355e68a7aeaa4 100644 --- a/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README_EN.md +++ b/solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README_EN.md @@ -84,7 +84,7 @@ The absolute difference is 1. Let $f[i][j]$ represent whether it is possible to select elements from the first $i$ rows with a sum of $j$. Then we have the state transition equation: $$ -f[i][j] = \begin{cases} 1 & \text{if there exists } x \in row[i] \text{ such that } f[i - 1][j - x] = 1 \\ 0 & \text{otherwise} \end{cases} +f[i][j] = \begin{cases} 1 & \textit{if there exists } x \in row[i] \textit{ such that } f[i - 1][j - x] = 1 \\ 0 & \textit{otherwise} \end{cases} $$ where $row[i]$ represents the set of elements in the $i$-th row. diff --git a/solution/2000-2099/2028.Find Missing Observations/README.md b/solution/2000-2099/2028.Find Missing Observations/README.md index 8ca34e9873b1c..b9902128353e6 100644 --- a/solution/2000-2099/2028.Find Missing Observations/README.md +++ b/solution/2000-2099/2028.Find Missing Observations/README.md @@ -82,7 +82,7 @@ tags: ### 方法一:构造 -根据题目描述,所有数字之和为 $(n + m) \times \text{mean}$,已知的数字之和为 $\sum_{i=0}^{m-1} \text{rolls}[i]$,那么缺失的数字之和为 $s = (n + m) \times \text{mean} - \sum_{i=0}^{m-1} \text{rolls}[i]$。 +根据题目描述,所有数字之和为 $(n + m) \times \textit{mean}$,已知的数字之和为 $\sum_{i=0}^{m-1} \textit{rolls}[i]$,那么缺失的数字之和为 $s = (n + m) \times \textit{mean} - \sum_{i=0}^{m-1} \textit{rolls}[i]$。 如果 $s \gt n \times 6$ 或者 $s \lt n$,说明不存在满足条件的答案,返回空数组。 diff --git a/solution/2000-2099/2028.Find Missing Observations/README_EN.md b/solution/2000-2099/2028.Find Missing Observations/README_EN.md index c069a29c629e4..7f7cb203655f5 100644 --- a/solution/2000-2099/2028.Find Missing Observations/README_EN.md +++ b/solution/2000-2099/2028.Find Missing Observations/README_EN.md @@ -72,7 +72,7 @@ tags: ### Solution 1: Construction -According to the problem description, the sum of all numbers is $(n + m) \times \text{mean}$, and the sum of known numbers is $\sum_{i=0}^{m-1} \text{rolls}[i]$. Therefore, the sum of the missing numbers is $s = (n + m) \times \text{mean} - \sum_{i=0}^{m-1} \text{rolls}[i]$. +According to the problem description, the sum of all numbers is $(n + m) \times \textit{mean}$, and the sum of known numbers is $\sum_{i=0}^{m-1} \textit{rolls}[i]$. Therefore, the sum of the missing numbers is $s = (n + m) \times \textit{mean} - \sum_{i=0}^{m-1} \textit{rolls}[i]$. If $s \gt n \times 6$ or $s \lt n$, it means there is no answer that satisfies the conditions, so we return an empty array. diff --git a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md index f41f8fe27b6c3..689849e6061c0 100644 --- a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md +++ b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md @@ -77,19 +77,19 @@ tags: ### 方法一:二分查找 -我们可以二分枚举乘积的值 $p$,定义二分的区间为 $[l, r]$,其中 $l = -\text{max}(|\text{nums1}[0]|, |\text{nums1}[n - 1]|) \times \text{max}(|\text{nums2}[0]|, |\text{nums2}[n - 1]|)$, $r = -l$。 +我们可以二分枚举乘积的值 $p$,定义二分的区间为 $[l, r]$,其中 $l = -\textit{max}(|\textit{nums1}[0]|, |\textit{nums1}[n - 1]|) \times \textit{max}(|\textit{nums2}[0]|, |\textit{nums2}[n - 1]|)$, $r = -l$。 对于每个 $p$,我们计算出乘积小于等于 $p$ 的乘积的个数,如果这个个数大于等于 $k$,那么说明第 $k$ 小的乘积一定小于等于 $p$,我们就可以将区间右端点缩小到 $p$,否则我们将区间左端点增大到 $p + 1$。 -那么问题的关键就是如何计算乘积小于等于 $p$ 的乘积的个数。我们可以枚举 $\text{nums1}$ 中的每个数 $x$,分类讨论: +那么问题的关键就是如何计算乘积小于等于 $p$ 的乘积的个数。我们可以枚举 $\textit{nums1}$ 中的每个数 $x$,分类讨论: -- 如果 $x > 0$,那么 $x \times \text{nums2}[i]$ 随着 $i$ 的增大是单调递增的,我们可以使用二分查找找到最小的 $i$,使得 $x \times \text{nums2}[i] > p$,那么 $i$ 就是小于等于 $p$ 的乘积的个数,累加到个数 $\text{cnt}$ 中; -- 如果 $x < 0$,那么 $x \times \text{nums2}[i]$ 随着 $i$ 的增大是单调递减的,我们可以使用二分查找找到最小的 $i$,使得 $x \times \text{nums2}[i] \leq p$,那么 $n - i$ 就是小于等于 $p$ 的乘积的个数,累加到个数 $\text{cnt}$ 中; -- 如果 $x = 0$,那么 $x \times \text{nums2}[i] = 0$,如果 $p \geq 0$,那么 $n$ 就是小于等于 $p$ 的乘积的个数,累加到个数 $\text{cnt}$ 中。 +- 如果 $x > 0$,那么 $x \times \textit{nums2}[i]$ 随着 $i$ 的增大是单调递增的,我们可以使用二分查找找到最小的 $i$,使得 $x \times \textit{nums2}[i] > p$,那么 $i$ 就是小于等于 $p$ 的乘积的个数,累加到个数 $\textit{cnt}$ 中; +- 如果 $x < 0$,那么 $x \times \textit{nums2}[i]$ 随着 $i$ 的增大是单调递减的,我们可以使用二分查找找到最小的 $i$,使得 $x \times \textit{nums2}[i] \leq p$,那么 $n - i$ 就是小于等于 $p$ 的乘积的个数,累加到个数 $\textit{cnt}$ 中; +- 如果 $x = 0$,那么 $x \times \textit{nums2}[i] = 0$,如果 $p \geq 0$,那么 $n$ 就是小于等于 $p$ 的乘积的个数,累加到个数 $\textit{cnt}$ 中。 这样我们就可以通过二分查找找到第 $k$ 小的乘积。 -时间复杂度 $O(m \times \log n \times \log M)$,其中 $m$ 和 $n$ 分别为 $\text{nums1}$ 和 $\text{nums2}$ 的长度,而 $M$ 为 $\text{nums1}$ 和 $\text{nums2}$ 中的最大值的绝对值。 +时间复杂度 $O(m \times \log n \times \log M)$,其中 $m$ 和 $n$ 分别为 $\textit{nums1}$ 和 $\textit{nums2}$ 的长度,而 $M$ 为 $\textit{nums1}$ 和 $\textit{nums2}$ 中的最大值的绝对值。 diff --git a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md index 6921c75cba414..3f8742d1afed5 100644 --- a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md +++ b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md @@ -78,19 +78,19 @@ The 3rd smallest product is -6. ### Solution 1: Binary Search -We can use binary search to enumerate the value of the product $p$, defining the binary search interval as $[l, r]$, where $l = -\text{max}(|\text{nums1}[0]|, |\text{nums1}[n - 1]|) \times \text{max}(|\text{nums2}[0]|, |\text{nums2}[n - 1]|)$, $r = -l$. +We can use binary search to enumerate the value of the product $p$, defining the binary search interval as $[l, r]$, where $l = -\textit{max}(|\textit{nums1}[0]|, |\textit{nums1}[n - 1]|) \times \textit{max}(|\textit{nums2}[0]|, |\textit{nums2}[n - 1]|)$, $r = -l$. For each $p$, we calculate the number of products less than or equal to $p$. If this number is greater than or equal to $k$, it means the $k$-th smallest product must be less than or equal to $p$, so we can reduce the right endpoint of the interval to $p$. Otherwise, we increase the left endpoint of the interval to $p + 1$. -The key to the problem is how to calculate the number of products less than or equal to $p$. We can enumerate each number $x$ in $\text{nums1}$ and discuss in cases: +The key to the problem is how to calculate the number of products less than or equal to $p$. We can enumerate each number $x$ in $\textit{nums1}$ and discuss in cases: -- If $x > 0$, then $x \times \text{nums2}[i]$ is monotonically increasing as $i$ increases. We can use binary search to find the smallest $i$ such that $x \times \text{nums2}[i] > p$. Then, $i$ is the number of products less than or equal to $p$, which is accumulated into the count $\text{cnt}$; -- If $x < 0$, then $x \times \text{nums2}[i]$ is monotonically decreasing as $i$ increases. We can use binary search to find the smallest $i$ such that $x \times \text{nums2}[i] \leq p$. Then, $n - i$ is the number of products less than or equal to $p$, which is accumulated into the count $\text{cnt}$; -- If $x = 0$, then $x \times \text{nums2}[i] = 0$. If $p \geq 0$, then $n$ is the number of products less than or equal to $p$, which is accumulated into the count $\text{cnt}$. +- If $x > 0$, then $x \times \textit{nums2}[i]$ is monotonically increasing as $i$ increases. We can use binary search to find the smallest $i$ such that $x \times \textit{nums2}[i] > p$. Then, $i$ is the number of products less than or equal to $p$, which is accumulated into the count $\textit{cnt}$; +- If $x < 0$, then $x \times \textit{nums2}[i]$ is monotonically decreasing as $i$ increases. We can use binary search to find the smallest $i$ such that $x \times \textit{nums2}[i] \leq p$. Then, $n - i$ is the number of products less than or equal to $p$, which is accumulated into the count $\textit{cnt}$; +- If $x = 0$, then $x \times \textit{nums2}[i] = 0$. If $p \geq 0$, then $n$ is the number of products less than or equal to $p$, which is accumulated into the count $\textit{cnt}$. This way, we can find the $k$-th smallest product through binary search. -The time complexity is $O(m \times \log n \times \log M)$, where $m$ and $n$ are the lengths of $\text{nums1}$ and $\text{nums2}$, respectively, and $M$ is the maximum absolute value in $\text{nums1}$ and $\text{nums2}$. +The time complexity is $O(m \times \log n \times \log M)$, where $m$ and $n$ are the lengths of $\textit{nums1}$ and $\textit{nums2}$, respectively, and $M$ is the maximum absolute value in $\textit{nums1}$ and $\textit{nums2}$. diff --git a/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README.md b/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README.md index c771acaebd79c..227b49a7aea6f 100644 --- a/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README.md +++ b/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README.md @@ -97,7 +97,7 @@ tags: ### 方法一:直接遍历 -根据题目描述,我们需要找出链表的第一个临界点和最后一个临界点位置 $\text{first}$ 和 $\text{last}$,这样可以计算出最大距离 $\text{maxDistance} = \text{last} - \text{first}$。对于最小距离 $\text{minDistance}$,我们需要遍历链表,计算相邻两个临界点之间的距离,取最小值即可。 +根据题目描述,我们需要找出链表的第一个临界点和最后一个临界点位置 $\textit{first}$ 和 $\textit{last}$,这样可以计算出最大距离 $\textit{maxDistance} = \textit{last} - \textit{first}$。对于最小距离 $\textit{minDistance}$,我们需要遍历链表,计算相邻两个临界点之间的距离,取最小值即可。 时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。 diff --git a/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README_EN.md b/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README_EN.md index 1675eed3844cb..e88b1ec8718bd 100644 --- a/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README_EN.md +++ b/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README_EN.md @@ -79,7 +79,7 @@ Note that the last node is not considered a local maxima because it does not hav ### Solution 1: Direct Traversal -Based on the problem description, we need to find the positions of the first and last critical points in the linked list, $\text{first}$ and $\text{last}$, respectively. This allows us to calculate the maximum distance $\text{maxDistance} = \text{last} - \text{first}$. For the minimum distance $\text{minDistance}$, we need to traverse the linked list, calculate the distance between two adjacent critical points, and take the minimum value. +Based on the problem description, we need to find the positions of the first and last critical points in the linked list, $\textit{first}$ and $\textit{last}$, respectively. This allows us to calculate the maximum distance $\textit{maxDistance} = \textit{last} - \textit{first}$. For the minimum distance $\textit{minDistance}$, we need to traverse the linked list, calculate the distance between two adjacent critical points, and take the minimum value. The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$. diff --git a/solution/2000-2099/2065.Maximum Path Quality of a Graph/README.md b/solution/2000-2099/2065.Maximum Path Quality of a Graph/README.md index 1d43c5a7acd3a..8fdfc6e964d9b 100644 --- a/solution/2000-2099/2065.Maximum Path Quality of a Graph/README.md +++ b/solution/2000-2099/2065.Maximum Path Quality of a Graph/README.md @@ -102,20 +102,20 @@ tags: ### 方法一:DFS -我们观察题目的数据范围,可以发现从 $0$ 开始的每条合法路径的边数不超过 $\frac{\text{maxTime}}{\min(time_j)} = \frac{100}{10} = 10$ 条,并且每个节点至多有四条边,所以我们可以直接使用朴素的 DFS 暴力搜索所有合法路径。 +我们观察题目的数据范围,可以发现从 $0$ 开始的每条合法路径的边数不超过 $\frac{\textit{maxTime}}{\min(time_j)} = \frac{100}{10} = 10$ 条,并且每个节点至多有四条边,所以我们可以直接使用朴素的 DFS 暴力搜索所有合法路径。 -我们先将图的边存储在邻接表表 $g$ 中,然后我们设计一个函数 $\text{dfs}(u, \text{cost}, \text{value})$,其中 $u$ 表示当前节点编号,而 $\text{cost}$ 和 $\text{value}$ 分别表示当前路径的花费时间和价值。另外,使用一个长度为 $n$ 的数组 $\text{vis}$ 记录每个节点是否被访问过。初始时,我们将节点 $0$ 标记为已访问。 +我们先将图的边存储在邻接表表 $g$ 中,然后我们设计一个函数 $\textit{dfs}(u, \textit{cost}, \textit{value})$,其中 $u$ 表示当前节点编号,而 $\textit{cost}$ 和 $\textit{value}$ 分别表示当前路径的花费时间和价值。另外,使用一个长度为 $n$ 的数组 $\textit{vis}$ 记录每个节点是否被访问过。初始时,我们将节点 $0$ 标记为已访问。 -函数 $\text{dfs}(u, \text{cost}, \text{value})$ 的逻辑如下: +函数 $\textit{dfs}(u, \textit{cost}, \textit{value})$ 的逻辑如下: -- 如果当前节点编号 $u$ 等于 $0$,表示我们已经回到了起点,那么我们更新答案为 $\max(\text{ans}, \text{value})$; -- 遍历当前节点 $u$ 的所有邻居节点 $v$,如果当前路径的花费时间加上边 $(u, v)$ 的时间 $t$ 不超过 $\text{maxTime}$,那么我们可以选择继续访问节点 $v$; - - 如果节点 $v$ 已经被访问过,那么我们直接递归调用 $\text{dfs}(v, \text{cost} + t, \text{value})$; - - 如果节点 $v$ 没有被访问过,我们标记节点 $v$ 为已访问,然后递归调用 $\text{dfs}(v, \text{cost} + t, \text{value} + \text{values}[v])$,最后恢复节点 $v$ 的访问状态。 +- 如果当前节点编号 $u$ 等于 $0$,表示我们已经回到了起点,那么我们更新答案为 $\max(\textit{ans}, \textit{value})$; +- 遍历当前节点 $u$ 的所有邻居节点 $v$,如果当前路径的花费时间加上边 $(u, v)$ 的时间 $t$ 不超过 $\textit{maxTime}$,那么我们可以选择继续访问节点 $v$; + - 如果节点 $v$ 已经被访问过,那么我们直接递归调用 $\textit{dfs}(v, \textit{cost} + t, \textit{value})$; + - 如果节点 $v$ 没有被访问过,我们标记节点 $v$ 为已访问,然后递归调用 $\textit{dfs}(v, \textit{cost} + t, \textit{value} + \textit{values}[v])$,最后恢复节点 $v$ 的访问状态。 -在主函数中,我们调用 $\text{dfs}(0, 0, \text{values}[0])$,并返回答案 $\text{ans}$ 即可。 +在主函数中,我们调用 $\textit{dfs}(0, 0, \textit{values}[0])$,并返回答案 $\textit{ans}$ 即可。 -时间复杂度 $O(n + m + 4^{\frac{\text{maxTime}}{\min(time_j)}})$,空间复杂度 $O(n + m + \frac{\text{maxTime}}{\min(time_j)})$。其中 $n$ 和 $m$ 分别表示节点数和边数。 +时间复杂度 $O(n + m + 4^{\frac{\textit{maxTime}}{\min(time_j)}})$,空间复杂度 $O(n + m + \frac{\textit{maxTime}}{\min(time_j)})$。其中 $n$ 和 $m$ 分别表示节点数和边数。 diff --git a/solution/2000-2099/2065.Maximum Path Quality of a Graph/README_EN.md b/solution/2000-2099/2065.Maximum Path Quality of a Graph/README_EN.md index 65daca6cdcbc3..7597aa91d9b95 100644 --- a/solution/2000-2099/2065.Maximum Path Quality of a Graph/README_EN.md +++ b/solution/2000-2099/2065.Maximum Path Quality of a Graph/README_EN.md @@ -83,20 +83,20 @@ The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = ### Solution 1: DFS -We observe the data range of the problem and find that the number of edges in each valid path starting from $0$ does not exceed $\frac{\text{maxTime}}{\min(time_j)} = \frac{100}{10} = 10$, and each node has at most four edges. Therefore, we can directly use naive DFS to brute-force search all valid paths. +We observe the data range of the problem and find that the number of edges in each valid path starting from $0$ does not exceed $\frac{\textit{maxTime}}{\min(time_j)} = \frac{100}{10} = 10$, and each node has at most four edges. Therefore, we can directly use naive DFS to brute-force search all valid paths. -First, we store the edges of the graph in the adjacency list $g$. Then, we design a function $\text{dfs}(u, \text{cost}, \text{value})$, where $u$ represents the current node number, and $\text{cost}$ and $\text{value}$ respectively represent the cost time and value of the current path. Additionally, we use an array $\text{vis}$ of length $n$ to record whether each node has been visited. Initially, we mark node $0$ as visited. +First, we store the edges of the graph in the adjacency list $g$. Then, we design a function $\textit{dfs}(u, \textit{cost}, \textit{value})$, where $u$ represents the current node number, and $\textit{cost}$ and $\textit{value}$ respectively represent the cost time and value of the current path. Additionally, we use an array $\textit{vis}$ of length $n$ to record whether each node has been visited. Initially, we mark node $0$ as visited. -The logic of the function $\text{dfs}(u, \text{cost}, \text{value})$ is as follows: +The logic of the function $\textit{dfs}(u, \textit{cost}, \textit{value})$ is as follows: -- If the current node number $u$ equals $0$, it means we have returned to the starting point, so we update the answer to $\max(\text{ans}, \text{value})$; -- For each neighbor node $v$ of the current node $u$, if the current path's cost time plus the time $t$ of the edge $(u, v)$ does not exceed $\text{maxTime}$, then we can choose to continue visiting node $v$; - - If node $v$ has already been visited, we directly recursively call $\text{dfs}(v, \text{cost} + t, \text{value})$; - - If node $v$ has not been visited, we mark node $v$ as visited, then recursively call $\text{dfs}(v, \text{cost} + t, \text{value} + \text{values}[v])$, and finally restore the visit status of node $v$. +- If the current node number $u$ equals $0$, it means we have returned to the starting point, so we update the answer to $\max(\textit{ans}, \textit{value})$; +- For each neighbor node $v$ of the current node $u$, if the current path's cost time plus the time $t$ of the edge $(u, v)$ does not exceed $\textit{maxTime}$, then we can choose to continue visiting node $v$; + - If node $v$ has already been visited, we directly recursively call $\textit{dfs}(v, \textit{cost} + t, \textit{value})$; + - If node $v$ has not been visited, we mark node $v$ as visited, then recursively call $\textit{dfs}(v, \textit{cost} + t, \textit{value} + \textit{values}[v])$, and finally restore the visit status of node $v$. -In the main function, we call $\text{dfs}(0, 0, \text{values}[0])$ and return the answer $\text{ans}$. +In the main function, we call $\textit{dfs}(0, 0, \textit{values}[0])$ and return the answer $\textit{ans}$. -The time complexity is $O(n + m + 4^{\frac{\text{maxTime}}{\min(time_j)}})$, and the space complexity is $O(n + m + \frac{\text{maxTime}}{\min(time_j)})$. Here, $n$ and $m$ respectively represent the number of nodes and edges. +The time complexity is $O(n + m + 4^{\frac{\textit{maxTime}}{\min(time_j)}})$, and the space complexity is $O(n + m + \frac{\textit{maxTime}}{\min(time_j)})$. Here, $n$ and $m$ respectively represent the number of nodes and edges. diff --git a/solution/2000-2099/2073.Time Needed to Buy Tickets/README.md b/solution/2000-2099/2073.Time Needed to Buy Tickets/README.md index 4b7367038f07d..270790dde6a7e 100644 --- a/solution/2000-2099/2073.Time Needed to Buy Tickets/README.md +++ b/solution/2000-2099/2073.Time Needed to Buy Tickets/README.md @@ -71,7 +71,7 @@ tags: 根据题目描述,当第 $k$ 个人完成购票时,在第 $k$ 个人前面的所有人,购买的票数都不会超过第 $k$ 个人购买的票数,而在第 $k$ 个人后面的所有人,购买的票数都不会超过第 $k$ 个人购买的票数减 $1$。 -因此,我们可以遍历整个队伍,对于第 $i$ 个人,如果 $i \leq k$,购票时间为 $\min(\text{tickets}[i], \text{tickets}[k])$,否则购票时间为 $\min(\text{tickets}[i], \text{tickets}[k] - 1)$。我们将所有人的购票时间相加即可。 +因此,我们可以遍历整个队伍,对于第 $i$ 个人,如果 $i \leq k$,购票时间为 $\min(\textit{tickets}[i], \textit{tickets}[k])$,否则购票时间为 $\min(\textit{tickets}[i], \textit{tickets}[k] - 1)$。我们将所有人的购票时间相加即可。 时间复杂度 $O(n)$,其中 $n$ 为队伍的长度。空间复杂度 $O(1)$。 diff --git a/solution/2000-2099/2073.Time Needed to Buy Tickets/README_EN.md b/solution/2000-2099/2073.Time Needed to Buy Tickets/README_EN.md index ef0f47936e57a..1a6e414d287fe 100644 --- a/solution/2000-2099/2073.Time Needed to Buy Tickets/README_EN.md +++ b/solution/2000-2099/2073.Time Needed to Buy Tickets/README_EN.md @@ -71,7 +71,7 @@ The person at position 0 has successfully bought 5 tickets and it took 4 + According to the problem description, when the $k^{th}$ person finishes buying tickets, all the people in front of the $k^{th}$ person will not buy more tickets than the $k^{th}$ person, and all the people behind the $k^{th}$ person will not buy more tickets than the $k^{th}$ person minus $1$. -Therefore, we can traverse the entire queue. For the $i^{th}$ person, if $i \leq k$, the time to buy tickets is $\min(\text{tickets}[i], \text{tickets}[k])$; otherwise, the time to buy tickets is $\min(\text{tickets}[i], \text{tickets}[k] - 1)$. We sum the buying time for all people to get the result. +Therefore, we can traverse the entire queue. For the $i^{th}$ person, if $i \leq k$, the time to buy tickets is $\min(\textit{tickets}[i], \textit{tickets}[k])$; otherwise, the time to buy tickets is $\min(\textit{tickets}[i], \textit{tickets}[k] - 1)$. We sum the buying time for all people to get the result. The time complexity is $O(n)$, where $n$ is the length of the queue. The space complexity is $O(1)$. diff --git a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md index b8f28226ae247..f3f9ebd38534a 100644 --- a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md +++ b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README.md @@ -96,7 +96,7 @@ tags: ### 方法一:模拟 -我们先计算出矩阵的列数 $cols = \text{len}(encodedText) / rows$,然后按照题目描述的规则,从左上角开始遍历矩阵,将字符添加到答案中。 +我们先计算出矩阵的列数 $cols = \textit{len}(encodedText) / rows$,然后按照题目描述的规则,从左上角开始遍历矩阵,将字符添加到答案中。 最后返回答案,注意去掉末尾的空格。 diff --git a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md index 551264b06d6b4..df7a604d70d64 100644 --- a/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md +++ b/solution/2000-2099/2075.Decode the Slanted Ciphertext/README_EN.md @@ -82,7 +82,7 @@ The blue arrows show how we can find originalText from encodedText. ### Solution 1: Simulation -First, we calculate the number of columns in the matrix $cols = \text{len}(encodedText) / rows$. Then, following the rules described in the problem, we start traversing the matrix from the top left corner, adding characters to the answer. +First, we calculate the number of columns in the matrix $cols = \textit{len}(encodedText) / rows$. Then, following the rules described in the problem, we start traversing the matrix from the top left corner, adding characters to the answer. Finally, we return the answer, making sure to remove any trailing spaces. diff --git a/solution/2000-2099/2079.Watering Plants/README.md b/solution/2000-2099/2079.Watering Plants/README.md index 68eaa90014003..9406d7e023c3f 100644 --- a/solution/2000-2099/2079.Watering Plants/README.md +++ b/solution/2000-2099/2079.Watering Plants/README.md @@ -90,12 +90,12 @@ tags: ### 方法一:模拟 -我们可以模拟给植物浇水的过程,用一个变量 $\text{water}$ 表示当前水罐中的水量,初始时 $\text{water} = \text{capacity}$。 +我们可以模拟给植物浇水的过程,用一个变量 $\textit{water}$ 表示当前水罐中的水量,初始时 $\textit{water} = \textit{capacity}$。 我们遍历植物,对于每一株植物: -- 如果当前水罐中的水量足够浇灌这株植物,我们就向前移动一步,浇灌这株植物,同时更新 $\text{water} = \text{water} - \text{plants}[i]$。 -- 否则我们就需要返回河边重新装满水罐,再次走到当前位置,然后向前移动一步,此时我们需要的步数为 $i \times 2 + 1$,然后我们浇灌这株植物,更新 $\text{water} = \text{capacity} - \text{plants}[i]$。 +- 如果当前水罐中的水量足够浇灌这株植物,我们就向前移动一步,浇灌这株植物,同时更新 $\textit{water} = \textit{water} - \textit{plants}[i]$。 +- 否则我们就需要返回河边重新装满水罐,再次走到当前位置,然后向前移动一步,此时我们需要的步数为 $i \times 2 + 1$,然后我们浇灌这株植物,更新 $\textit{water} = \textit{capacity} - \textit{plants}[i]$。 最后返回总的步数即可。 diff --git a/solution/2000-2099/2079.Watering Plants/README_EN.md b/solution/2000-2099/2079.Watering Plants/README_EN.md index 7bb0ed1256881..7eb2ffb0dc806 100644 --- a/solution/2000-2099/2079.Watering Plants/README_EN.md +++ b/solution/2000-2099/2079.Watering Plants/README_EN.md @@ -89,12 +89,12 @@ Steps needed = 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 = 49. ### Solution 1: Simulation -We can simulate the process of watering the plants. We use a variable $\text{water}$ to represent the current amount of water in the watering can, initially $\text{water} = \text{capacity}$. +We can simulate the process of watering the plants. We use a variable $\textit{water}$ to represent the current amount of water in the watering can, initially $\textit{water} = \textit{capacity}$. We traverse the plants. For each plant: -- If the current amount of water in the watering can is enough to water this plant, we move forward one step, water this plant, and update $\text{water} = \text{water} - \text{plants}[i]$. -- Otherwise, we need to return to the river to refill the watering can, walk back to the current position, and then move forward one step. The number of steps we need is $i \times 2 + 1$. Then we water this plant and update $\text{water} = \text{capacity} - \text{plants}[i]$. +- If the current amount of water in the watering can is enough to water this plant, we move forward one step, water this plant, and update $\textit{water} = \textit{water} - \textit{plants}[i]$. +- Otherwise, we need to return to the river to refill the watering can, walk back to the current position, and then move forward one step. The number of steps we need is $i \times 2 + 1$. Then we water this plant and update $\textit{water} = \textit{capacity} - \textit{plants}[i]$. Finally, return the total number of steps. diff --git a/solution/2000-2099/2080.Range Frequency Queries/README.md b/solution/2000-2099/2080.Range Frequency Queries/README.md index 24521896eaa08..3e5f7b21a2f5c 100644 --- a/solution/2000-2099/2080.Range Frequency Queries/README.md +++ b/solution/2000-2099/2080.Range Frequency Queries/README.md @@ -70,9 +70,9 @@ rangeFreqQuery.query(0, 11, 33); // 返回 2 。33 在整个子数组中出现 2 ### 方法一:哈希表 -我们用一个哈希表 $g$ 来存储每个值对应的下标数组。在构造函数中,我们遍历数组 $\text{arr}$,将每个值对应的下标加入到哈希表中。 +我们用一个哈希表 $g$ 来存储每个值对应的下标数组。在构造函数中,我们遍历数组 $\textit{arr}$,将每个值对应的下标加入到哈希表中。 -在查询函数中,我们首先判断哈希表中是否存在给定的值。如果不存在,说明该值在数组中不存在,直接返回 $0$。否则,我们获取该值对应的下标数组 $\text{idx}$。然后我们使用二分查找找到下标数组中第一个大于等于 $\text{left}$ 的下标 $l$,以及第一个大于 $\text{right}$ 的下标 $r$。最后返回 $r - l$ 即可。 +在查询函数中,我们首先判断哈希表中是否存在给定的值。如果不存在,说明该值在数组中不存在,直接返回 $0$。否则,我们获取该值对应的下标数组 $\textit{idx}$。然后我们使用二分查找找到下标数组中第一个大于等于 $\textit{left}$ 的下标 $l$,以及第一个大于 $\textit{right}$ 的下标 $r$。最后返回 $r - l$ 即可。 时间复杂度方面,构造函数的时间复杂度为 $O(n)$,查询函数的时间复杂度为 $O(\log n)$。其中 $n$ 为数组的长度。空间复杂度为 $O(n)$。 diff --git a/solution/2000-2099/2080.Range Frequency Queries/README_EN.md b/solution/2000-2099/2080.Range Frequency Queries/README_EN.md index 66309cc1300a0..7945cfe3ed4ee 100644 --- a/solution/2000-2099/2080.Range Frequency Queries/README_EN.md +++ b/solution/2000-2099/2080.Range Frequency Queries/README_EN.md @@ -69,9 +69,9 @@ rangeFreqQuery.query(0, 11, 33); // return 2. The value 33 occurs 2 times in the ### Solution 1: Hash Table -We use a hash table $g$ to store the array of indices corresponding to each value. In the constructor, we traverse the array $\text{arr}$, adding the index corresponding to each value to the hash table. +We use a hash table $g$ to store the array of indices corresponding to each value. In the constructor, we traverse the array $\textit{arr}$, adding the index corresponding to each value to the hash table. -In the query function, we first check whether the given value exists in the hash table. If it does not exist, it means that the value does not exist in the array, so we directly return $0$. Otherwise, we get the index array $\text{idx}$ corresponding to the value. Then we use binary search to find the first index $l$ that is greater than or equal to $\text{left}$, and the first index $r$ that is greater than $\text{right}$. Finally, we return $r - l$. +In the query function, we first check whether the given value exists in the hash table. If it does not exist, it means that the value does not exist in the array, so we directly return $0$. Otherwise, we get the index array $\textit{idx}$ corresponding to the value. Then we use binary search to find the first index $l$ that is greater than or equal to $\textit{left}$, and the first index $r$ that is greater than $\textit{right}$. Finally, we return $r - l$. In terms of time complexity, the time complexity of the constructor is $O(n)$, and the time complexity of the query function is $O(\log n)$. The space complexity is $O(n)$. Where $n$ is the length of the array. diff --git a/solution/2000-2099/2088.Count Fertile Pyramids in a Land/README.md b/solution/2000-2099/2088.Count Fertile Pyramids in a Land/README.md index 7cecc25f4b6db..932d9f5c69d42 100644 --- a/solution/2000-2099/2088.Count Fertile Pyramids in a Land/README.md +++ b/solution/2000-2099/2088.Count Fertile Pyramids in a Land/README.md @@ -112,7 +112,7 @@ tags: 我们定义 $f[i][j]$ 表示以 $(i, j)$ 为顶点的金字塔的最大高度,那么有如下状态转移方程: $$ -f[i][j] = \begin{cases} 0 & \text{grid}[i][j] = 0 \\ \min(f[i + 1][j - 1], f[i + 1][j], f[i + 1][j + 1]) + 1 & \text{grid}[i][j] = 1 \end{cases} +f[i][j] = \begin{cases} 0 & \textit{grid}[i][j] = 0 \\ \min(f[i + 1][j - 1], f[i + 1][j], f[i + 1][j + 1]) + 1 & \textit{grid}[i][j] = 1 \end{cases} $$ 因此,我们可以从下往上、从左往右遍历网格,计算出所有的 $f[i][j]$,并累加所有的 $f[i][j]$ 即可得到正金字塔的个数。 @@ -120,7 +120,7 @@ $$ 接下来,我们考虑倒金字塔的个数。与金字塔类似,我们定义 $g[i][j]$ 表示以 $(i, j)$ 为顶点的倒金字塔的最大高度,那么有如下状态转移方程: $$ -g[i][j] = \begin{cases} 0 & \text{grid}[i][j] = 0 \\ \min(g[i - 1][j - 1], g[i - 1][j], g[i - 1][j + 1]) + 1 & \text{grid}[i][j] = 1 \end{cases} +g[i][j] = \begin{cases} 0 & \textit{grid}[i][j] = 0 \\ \min(g[i - 1][j - 1], g[i - 1][j], g[i - 1][j + 1]) + 1 & \textit{grid}[i][j] = 1 \end{cases} $$ 因此,我们可以从上往下、从左往右遍历网格,计算出所有的 $g[i][j]$,并累加所有的 $g[i][j]$ 即可得到倒金字塔的个数。 diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md b/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md index 3c166df9b2d46..2c9dc84bc5455 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/README.md @@ -90,7 +90,7 @@ tags: 我们定义一个长度为 $n$ 的数组 $g$,其中 $g[i]$ 表示炸弹 $i$ 的爆炸范围内可以引爆的所有炸弹的下标。 -然后,我们遍历所有炸弹,对于两个炸弹 $(x_1, y_1, r_1)$ 和 $(x_2, y_2, r_2)$,我们计算它们之间的距离 $\text{dist} = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$。如果 $\text{dist} \leq r_1$,那么炸弹 $i$ 的爆炸范围内可以引爆炸弹 $j$,我们就将 $j$ 添加到 $g[i]$ 中。如果 $\text{dist} \leq r_2$,那么炸弹 $j$ 的爆炸范围内可以引爆炸弹 $i$,我们就将 $i$ 添加到 $g[j]$ 中。 +然后,我们遍历所有炸弹,对于两个炸弹 $(x_1, y_1, r_1)$ 和 $(x_2, y_2, r_2)$,我们计算它们之间的距离 $\textit{dist} = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$。如果 $\textit{dist} \leq r_1$,那么炸弹 $i$ 的爆炸范围内可以引爆炸弹 $j$,我们就将 $j$ 添加到 $g[i]$ 中。如果 $\textit{dist} \leq r_2$,那么炸弹 $j$ 的爆炸范围内可以引爆炸弹 $i$,我们就将 $i$ 添加到 $g[j]$ 中。 接下来,我们遍历所有炸弹,对于每个炸弹 $k$,我们使用广度优先搜索计算炸弹 $k$ 的爆炸范围内可以引爆的所有炸弹的下标,并记录下来。如果这些炸弹的数量等于 $n$,那么我们就可以引爆所有炸弹,直接返回 $n$。否则,我们记录下来这些炸弹的数量,并返回最大值。 diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md b/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md index 178ad7fa58a09..247bc7976b1a1 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md @@ -85,7 +85,7 @@ Thus all 5 bombs are detonated. We define an array $g$ of length $n$, where $g[i]$ represents the indices of all bombs that can be triggered by bomb $i$ within its explosion range. -Next, we iterate over all bombs. For two bombs $(x_1, y_1, r_1)$ and $(x_2, y_2, r_2)$, we calculate the distance between them $\text{dist} = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$. If $\text{dist} \leq r_1$, then bomb $i$ can trigger bomb $j$ within its explosion range, so we add $j$ to $g[i]$. If $\text{dist} \leq r_2$, then bomb $j$ can trigger bomb $i$ within its explosion range, so we add $i$ to $g[j]$. +Next, we iterate over all bombs. For two bombs $(x_1, y_1, r_1)$ and $(x_2, y_2, r_2)$, we calculate the distance between them $\textit{dist} = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$. If $\textit{dist} \leq r_1$, then bomb $i$ can trigger bomb $j$ within its explosion range, so we add $j$ to $g[i]$. If $\textit{dist} \leq r_2$, then bomb $j$ can trigger bomb $i$ within its explosion range, so we add $i$ to $g[j]$. Next, we iterate over all bombs. For each bomb $k$, we use breadth-first search to calculate the indices of all bombs that can be triggered by bomb $k$ within its explosion range and record them. If the number of these bombs equals $n$, then we can trigger all bombs and directly return $n$. Otherwise, we record the number of these bombs and return the maximum value. diff --git a/solution/2100-2199/2105.Watering Plants II/README.md b/solution/2100-2199/2105.Watering Plants II/README.md index 0e26c1bfe8e92..06db9282fa101 100644 --- a/solution/2100-2199/2105.Watering Plants II/README.md +++ b/solution/2100-2199/2105.Watering Plants II/README.md @@ -88,7 +88,7 @@ tags: ### 方法一:双指针 + 模拟 -我们用两个变量 $a$ 和 $b$ 分别表示 Alice 和 Bob 的水量,初始时 $a = \text{capacityA}$, $b = \text{capacityB}$。然后用两个指针 $i$ 和 $j$ 分别指向植物数组的头尾,然后模拟 Alice 和 Bob 从两端向中间浇水的过程。 +我们用两个变量 $a$ 和 $b$ 分别表示 Alice 和 Bob 的水量,初始时 $a = \textit{capacityA}$, $b = \textit{capacityB}$。然后用两个指针 $i$ 和 $j$ 分别指向植物数组的头尾,然后模拟 Alice 和 Bob 从两端向中间浇水的过程。 当 $i < j$ 时,我们分别判断 Alice 和 Bob 的水量是否足够浇水,如果不够,我们就重新灌满水罐。然后更新 $a$ 和 $b$ 的水量,同时移动指针 $i$ 和 $j$。最后我们还需要判断 $i$ 和 $j$ 是否相等,如果相等,我们还需要判断 $\max(a, b)$ 是否小于植物的水量,如果小于,我们需要再次重新灌满水罐。 diff --git a/solution/2100-2199/2105.Watering Plants II/README_EN.md b/solution/2100-2199/2105.Watering Plants II/README_EN.md index 9667b8f70b252..42b3a76e27e4c 100644 --- a/solution/2100-2199/2105.Watering Plants II/README_EN.md +++ b/solution/2100-2199/2105.Watering Plants II/README_EN.md @@ -89,7 +89,7 @@ So, the total number of times they have to refill is 0. ### Solution 1: Two Pointers + Simulation -We use two variables $a$ and $b$ to represent the amount of water Alice and Bob have, initially $a = \text{capacityA}$, $b = \text{capacityB}$. Then we use two pointers $i$ and $j$ to point to the head and tail of the plant array, and simulate the process of Alice and Bob watering from both ends to the middle. +We use two variables $a$ and $b$ to represent the amount of water Alice and Bob have, initially $a = \textit{capacityA}$, $b = \textit{capacityB}$. Then we use two pointers $i$ and $j$ to point to the head and tail of the plant array, and simulate the process of Alice and Bob watering from both ends to the middle. When $i < j$, we judge whether Alice and Bob have enough water to water the plants. If not, we refill the watering cans. Then we update the amount of water $a$ and $b$, and move the pointers $i$ and $j$. Finally, we need to judge whether $i$ and $j$ are equal. If they are equal, we need to judge whether $\max(a, b)$ is less than the amount of water the plant needs. If it is less, we need to refill the watering cans again. diff --git a/solution/2100-2199/2109.Adding Spaces to a String/README.md b/solution/2100-2199/2109.Adding Spaces to a String/README.md index 4f75e2efc33f8..e0125ea6d6005 100644 --- a/solution/2100-2199/2109.Adding Spaces to a String/README.md +++ b/solution/2100-2199/2109.Adding Spaces to a String/README.md @@ -82,7 +82,7 @@ tags: ### 方法一:双指针 -我们可以用双指针 $i$ 和 $j$ 分别指向字符串 $s$ 和数组 $\text{spaces}$ 的头部,然后从头到尾遍历字符串 $s$,当 $i$ 等于 $\text{spaces}[j]$ 时,我们往结果字符串中添加一个空格,然后 $j$ 自增 $1$。接下来,我们将 $s[i]$ 添加到结果字符串中,然后 $i$ 自增 $1$。继续这个过程,直到遍历完字符串 $s$。 +我们可以用双指针 $i$ 和 $j$ 分别指向字符串 $s$ 和数组 $\textit{spaces}$ 的头部,然后从头到尾遍历字符串 $s$,当 $i$ 等于 $\textit{spaces}[j]$ 时,我们往结果字符串中添加一个空格,然后 $j$ 自增 $1$。接下来,我们将 $s[i]$ 添加到结果字符串中,然后 $i$ 自增 $1$。继续这个过程,直到遍历完字符串 $s$。 时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是字符串 $s$ 和数组 $spaces$ 的长度。 diff --git a/solution/2100-2199/2109.Adding Spaces to a String/README_EN.md b/solution/2100-2199/2109.Adding Spaces to a String/README_EN.md index 5b2b1df3dc776..80a9bde8db1fc 100644 --- a/solution/2100-2199/2109.Adding Spaces to a String/README_EN.md +++ b/solution/2100-2199/2109.Adding Spaces to a String/README_EN.md @@ -78,7 +78,7 @@ We are also able to place spaces before the first character of the string. ### Solution 1: Two Pointers -We can use two pointers $i$ and $j$ to point to the beginning of the string $s$ and the array $\text{spaces}$, respectively. Then, we iterate through the string $s$ from the beginning to the end. When $i$ equals $\text{spaces}[j]$, we add a space to the result string, and then increment $j$ by $1$. Next, we add $s[i]$ to the result string, and then increment $i$ by $1$. We continue this process until we have iterated through the entire string $s$. +We can use two pointers $i$ and $j$ to point to the beginning of the string $s$ and the array $\textit{spaces}$, respectively. Then, we iterate through the string $s$ from the beginning to the end. When $i$ equals $\textit{spaces}[j]$, we add a space to the result string, and then increment $j$ by $1$. Next, we add $s[i]$ to the result string, and then increment $i$ by $1$. We continue this process until we have iterated through the entire string $s$. The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$, where $n$ and $m$ are the lengths of the string $s$ and the array $spaces$, respectively. diff --git a/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README.md b/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README.md index 3cb82b49879c6..72be8ac8af7a6 100644 --- a/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README.md +++ b/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README.md @@ -92,9 +92,9 @@ tags: 我们注意到,题目中给定的字符串只包含小写字母,并且每个字符串的字母至多出现一次。因此,我们可以用一个长度为 $26$ 的二进制数表示一个字符串,其中第 $i$ 位为 $1$ 表示字符串中包含第 $i$ 个小写字母,为 $0$ 表示字符串中不包含第 $i$ 个小写字母。 -我们可以将字符串数组 $\text{startWords}$ 中的每个字符串转换为一个二进制数,并将这些二进制数存储到一个集合 $\text{s}$ 中。对于字符串数组 $\text{targetWords}$ 中的每个字符串,我们首先将其转换为一个二进制数,然后枚举这个字符串中的每个字母,将这个字母从二进制数中去掉,再检查是否存在一个二进制数在集合 $\text{s}$ 中,使得这个二进制数与去掉的字母的二进制数的异或结果在集合 $\text{s}$ 中,如果存在,那么这个字符串可以由 $\text{startWords}$ 中的某个字符串执行转换操作获得,答案加一。然后我们跳过这个字符串,继续处理下一个字符串。 +我们可以将字符串数组 $\textit{startWords}$ 中的每个字符串转换为一个二进制数,并将这些二进制数存储到一个集合 $\textit{s}$ 中。对于字符串数组 $\textit{targetWords}$ 中的每个字符串,我们首先将其转换为一个二进制数,然后枚举这个字符串中的每个字母,将这个字母从二进制数中去掉,再检查是否存在一个二进制数在集合 $\textit{s}$ 中,使得这个二进制数与去掉的字母的二进制数的异或结果在集合 $\textit{s}$ 中,如果存在,那么这个字符串可以由 $\textit{startWords}$ 中的某个字符串执行转换操作获得,答案加一。然后我们跳过这个字符串,继续处理下一个字符串。 -时间复杂度 $O(n \times |\Sigma|)$,空间复杂度 $O(n)$。其中 $n$ 为字符串数组 $\text{targetWords}$ 的长度,而 $|\Sigma|$ 为字符串中的字符集大小,本题中 $|\Sigma| = 26$。 +时间复杂度 $O(n \times |\Sigma|)$,空间复杂度 $O(n)$。其中 $n$ 为字符串数组 $\textit{targetWords}$ 的长度,而 $|\Sigma|$ 为字符串中的字符集大小,本题中 $|\Sigma| = 26$。 diff --git a/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README_EN.md b/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README_EN.md index ed5472a92d163..6f6fbf58081e9 100644 --- a/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README_EN.md +++ b/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/README_EN.md @@ -90,9 +90,9 @@ tags: We notice that the given strings only contain lowercase letters, and each letter in a string appears at most once. Therefore, we can represent a string with a binary number of length $26$, where the $i$-th bit being $1$ indicates that the string contains the $i$-th lowercase letter, and $0$ indicates the absence of the $i$-th lowercase letter. -We can convert each string in the array $\text{startWords}$ into a binary number and store these binary numbers in a set $\text{s}$. For each string in the array $\text{targetWords}$, we first convert it into a binary number, then enumerate each letter in this string, remove this letter from the binary number, and check if there exists a binary number in the set $\text{s}$ such that the XOR result of this binary number with the removed letter's binary number is in the set $\text{s}$. If such a binary number exists, then this string can be obtained by performing a transformation operation on some string in $\text{startWords}$, and we increment the answer by one. Then, we skip this string and continue processing the next string. +We can convert each string in the array $\textit{startWords}$ into a binary number and store these binary numbers in a set $\textit{s}$. For each string in the array $\textit{targetWords}$, we first convert it into a binary number, then enumerate each letter in this string, remove this letter from the binary number, and check if there exists a binary number in the set $\textit{s}$ such that the XOR result of this binary number with the removed letter's binary number is in the set $\textit{s}$. If such a binary number exists, then this string can be obtained by performing a transformation operation on some string in $\textit{startWords}$, and we increment the answer by one. Then, we skip this string and continue processing the next string. -The time complexity is $O(n \times |\Sigma|)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string array $\text{targetWords}$, and $|\Sigma|$ is the size of the character set in the string, which is $26$ in this problem. +The time complexity is $O(n \times |\Sigma|)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string array $\textit{targetWords}$, and $|\Sigma|$ is the size of the character set in the string, which is $26$ in this problem. diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md index e6b38a5adcbe0..ead4659369949 100644 --- a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md @@ -73,7 +73,7 @@ tags: ### 方法一:模拟 -我们可以直接模拟题目描述的过程,将字符串 $s$ 按照长度 $k$ 进行分组,然后对于最后一组不足 $k$ 个字符的情况,使用字符 $\text{fill}$ 进行填充。 +我们可以直接模拟题目描述的过程,将字符串 $s$ 按照长度 $k$ 进行分组,然后对于最后一组不足 $k$ 个字符的情况,使用字符 $\textit{fill}$ 进行填充。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。 diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md index 93ce54ac04d5d..80acdefcd3179 100644 --- a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md @@ -73,7 +73,7 @@ Thus, the 4 groups formed are "abc", "def", "ghi", ### Solution 1: Simulation -We can directly simulate the process described in the problem statement, dividing the string $s$ into groups of length $k$. For the last group, if it contains fewer than $k$ characters, we use the character $\text{fill}$ to pad it. +We can directly simulate the process described in the problem statement, dividing the string $s$ into groups of length $k$. For the last group, if it contains fewer than $k$ characters, we use the character $\textit{fill}$ to pad it. The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. diff --git a/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README.md b/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README.md index 3b08ead4205fc..b35b103695d80 100644 --- a/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README.md +++ b/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README.md @@ -82,7 +82,7 @@ tags: 我们可以将奇数下标和偶数下标分别取出来,然后对奇数下标的数组进行非递增排序,对偶数下标的数组进行非递减排序,最后再将两个数组合并即可。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 diff --git a/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md b/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md index 65c07384fae84..920283dc8969b 100644 --- a/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md +++ b/solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md @@ -80,7 +80,7 @@ The resultant array formed is [2,1], which is the same as the initial array. We can extract the elements at odd and even indices separately, then sort the array of odd indices in non-increasing order and the array of even indices in non-decreasing order. Finally, merge the two arrays back together. -The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\text{nums}$. +The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. diff --git a/solution/2100-2199/2187.Minimum Time to Complete Trips/README.md b/solution/2100-2199/2187.Minimum Time to Complete Trips/README.md index d7a230d9cdfb9..0b0fd49d0c98b 100644 --- a/solution/2100-2199/2187.Minimum Time to Complete Trips/README.md +++ b/solution/2100-2199/2187.Minimum Time to Complete Trips/README.md @@ -69,7 +69,7 @@ tags: 我们注意到,如果我们能在 $t$ 时间内至少完成 $totalTrips$ 趟旅途,那么在 $t' > t$ 时间内也能至少完成 $totalTrips$ 趟旅途。因此我们可以使用二分查找的方法来找到最小的 $t$。 -我们定义二分查找的左边界 $l = 1$,右边界 $r = \min(time) \times \text{totalTrips}$。每一次二分查找,我们计算中间值 $\text{mid} = \frac{l + r}{2}$,然后计算在 $\text{mid}$ 时间内能完成的旅途数目。如果这个数目大于等于 $\text{totalTrips}$,那么我们将右边界缩小到 $\text{mid}$,否则我们将左边界扩大到 $\text{mid} + 1$。 +我们定义二分查找的左边界 $l = 1$,右边界 $r = \min(time) \times \textit{totalTrips}$。每一次二分查找,我们计算中间值 $\textit{mid} = \frac{l + r}{2}$,然后计算在 $\textit{mid}$ 时间内能完成的旅途数目。如果这个数目大于等于 $\textit{totalTrips}$,那么我们将右边界缩小到 $\textit{mid}$,否则我们将左边界扩大到 $\textit{mid} + 1$。 最后返回左边界即可。 diff --git a/solution/2100-2199/2187.Minimum Time to Complete Trips/README_EN.md b/solution/2100-2199/2187.Minimum Time to Complete Trips/README_EN.md index 47bf8c42401a5..bb82d5a43b748 100644 --- a/solution/2100-2199/2187.Minimum Time to Complete Trips/README_EN.md +++ b/solution/2100-2199/2187.Minimum Time to Complete Trips/README_EN.md @@ -69,7 +69,7 @@ So the minimum time needed to complete 1 trip is 2. We notice that if we can complete at least $totalTrips$ trips in $t$ time, then we can also complete at least $totalTrips$ trips in $t' > t$ time. Therefore, we can use the method of binary search to find the smallest $t$. -We define the left boundary of the binary search as $l = 1$, and the right boundary as $r = \min(time) \times totalTrips$. For each binary search, we calculate the middle value $\text{mid} = \frac{l + r}{2}$, and then calculate the number of trips that can be completed in $\text{mid}$ time. If this number is greater than or equal to $totalTrips$, then we reduce the right boundary to $\text{mid}$, otherwise we increase the left boundary to $\text{mid} + 1$. +We define the left boundary of the binary search as $l = 1$, and the right boundary as $r = \min(time) \times totalTrips$. For each binary search, we calculate the middle value $\textit{mid} = \frac{l + r}{2}$, and then calculate the number of trips that can be completed in $\textit{mid}$ time. If this number is greater than or equal to $totalTrips$, then we reduce the right boundary to $\textit{mid}$, otherwise we increase the left boundary to $\textit{mid} + 1$. Finally, return the left boundary. diff --git a/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/README.md b/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/README.md index eee2067b848d1..032ab320f5ab4 100644 --- a/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/README.md +++ b/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/README.md @@ -81,9 +81,9 @@ tags: 构造回文串的过程,实际上是每次选择一对字母并把它们交换到字符串头尾的过程。考虑字母 $x$ 和字母 $y$ 哪个先选,分以下情况讨论: -- 字母 $x$ 和 $y$ 的位置满足 $\underbrace{\cdots}_{a\text{ 个}}x\underbrace{\cdots}_{b\text{ 个}}y\underbrace{\cdots}_{c\text{ 个}}y\underbrace{\cdots}_{d\text{ 个}}x\underbrace{\cdots}_{e\text{ 个}}$。如果先把 $x$ 换到头尾,再把 $y$ 换到头尾,那么需要 $(a + e) + (b + d)$ 次交换;如果先换 $y$ 再换 $x$,那么需要 $(a + b + 1 + d + e + 1) + (a + e)$ 次交换。显然先换 $x$ 更优。 -- 字母 $x$ 和 $y$ 的位置满足 $\underbrace{\cdots}_{a\text{ 个}}x\underbrace{\cdots}_{b\text{ 个}}y\underbrace{\cdots}_{c\text{ 个}}x\underbrace{\cdots}_{d\text{ 个}}y\underbrace{\cdots}_{e\text{ 个}}$。如果先换 $x$ 再换 $y$,那么需要 $(a + d + e + 1) + (a + b + e)$ 次交换;如果先换 $y$ 再换 $x$,那么需要 $(a + b + 1 + e) + (a + d + e)$ 次交换。先换哪个都一样。 -- 字母 $x$ 和 $y$ 的位置满足 $\underbrace{\cdots}_{a\text{ 个}}x\underbrace{\cdots}_{b\text{ 个}}x\underbrace{\cdots}_{c\text{ 个}}y\underbrace{\cdots}_{d\text{ 个}}y\underbrace{\cdots}_{e\text{ 个}}$。如果先换 $x$ 再换 $y$,那么需要 $(a + c + d + e + 2) + (a + b + c + e)$ 次交换;如果先换 $y$ 再换 $x$,那么需要 $(a + b + c + 2 + e) + (a + c + d + e)$ 次交换。先换哪个都一样。 +- 字母 $x$ 和 $y$ 的位置满足 $\underbrace{\cdots}_{a\textit{ 个}}x\underbrace{\cdots}_{b\textit{ 个}}y\underbrace{\cdots}_{c\textit{ 个}}y\underbrace{\cdots}_{d\textit{ 个}}x\underbrace{\cdots}_{e\textit{ 个}}$。如果先把 $x$ 换到头尾,再把 $y$ 换到头尾,那么需要 $(a + e) + (b + d)$ 次交换;如果先换 $y$ 再换 $x$,那么需要 $(a + b + 1 + d + e + 1) + (a + e)$ 次交换。显然先换 $x$ 更优。 +- 字母 $x$ 和 $y$ 的位置满足 $\underbrace{\cdots}_{a\textit{ 个}}x\underbrace{\cdots}_{b\textit{ 个}}y\underbrace{\cdots}_{c\textit{ 个}}x\underbrace{\cdots}_{d\textit{ 个}}y\underbrace{\cdots}_{e\textit{ 个}}$。如果先换 $x$ 再换 $y$,那么需要 $(a + d + e + 1) + (a + b + e)$ 次交换;如果先换 $y$ 再换 $x$,那么需要 $(a + b + 1 + e) + (a + d + e)$ 次交换。先换哪个都一样。 +- 字母 $x$ 和 $y$ 的位置满足 $\underbrace{\cdots}_{a\textit{ 个}}x\underbrace{\cdots}_{b\textit{ 个}}x\underbrace{\cdots}_{c\textit{ 个}}y\underbrace{\cdots}_{d\textit{ 个}}y\underbrace{\cdots}_{e\textit{ 个}}$。如果先换 $x$ 再换 $y$,那么需要 $(a + c + d + e + 2) + (a + b + c + e)$ 次交换;如果先换 $y$ 再换 $x$,那么需要 $(a + b + c + 2 + e) + (a + c + d + e)$ 次交换。先换哪个都一样。 上述讨论可以得到结论:每次交换最外边出现的字母不劣。因此贪心解法成立。 diff --git a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md index a428de4ddbd4c..00faf635c6685 100644 --- a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md +++ b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md @@ -85,7 +85,7 @@ tags: ### 方法一:栈 -如果存在三个相邻的数 $x$, $y$, $z$ 可以进行合并,那么我们先合并 $x$ 和 $y$,再合并 $z$ 的结果,与先合并 $y$ 和 $z$,再合并 $x$ 的结果是一样的,结果均为 $\text{LCM}(x, y, z)$。 +如果存在三个相邻的数 $x$, $y$, $z$ 可以进行合并,那么我们先合并 $x$ 和 $y$,再合并 $z$ 的结果,与先合并 $y$ 和 $z$,再合并 $x$ 的结果是一样的,结果均为 $\textit{LCM}(x, y, z)$。 因此,我们可以总是优先合并左侧相邻的数,再将合并后的结果与右侧相邻的数进行合并。 diff --git a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md index 860a78e33ae49..35de06154de56 100644 --- a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md +++ b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md @@ -83,7 +83,7 @@ Note that there are other ways to obtain the same resultant array. ### Solution 1: Stack -If there exist three adjacent numbers $x$, $y$, $z$ that can be merged, then the result of first merging $x$ and $y$, then merging $z$, is the same as the result of first merging $y$ and $z$, then merging $x$. Both results are $\text{LCM}(x, y, z)$. +If there exist three adjacent numbers $x$, $y$, $z$ that can be merged, then the result of first merging $x$ and $y$, then merging $z$, is the same as the result of first merging $y$ and $z$, then merging $x$. Both results are $\textit{LCM}(x, y, z)$. Therefore, we can always prefer to merge the adjacent numbers on the left, and then merge the result with the adjacent number on the right. diff --git a/solution/2200-2299/2213.Longest Substring of One Repeating Character/README.md b/solution/2200-2299/2213.Longest Substring of One Repeating Character/README.md index 212a42c21ddcc..df14378ba428d 100644 --- a/solution/2200-2299/2213.Longest Substring of One Repeating Character/README.md +++ b/solution/2200-2299/2213.Longest Substring of One Repeating Character/README.md @@ -72,7 +72,7 @@ tags: ### 方法一:线段树 -线段树将整个区间分割为多个不连续的子区间,子区间的数量不超过 $\log(\text{width})$。更新某个元素的值,只需要更新 $\log(\text{width})$ 个区间,并且这些区间都包含在一个包含该元素的大区间内。区间修改时,需要使用**懒标记**保证效率。 +线段树将整个区间分割为多个不连续的子区间,子区间的数量不超过 $\log(\textit{width})$。更新某个元素的值,只需要更新 $\log(\textit{width})$ 个区间,并且这些区间都包含在一个包含该元素的大区间内。区间修改时,需要使用**懒标记**保证效率。 - 线段树的每个节点代表一个区间; - 线段树具有唯一的根节点,代表的区间是整个统计范围,如 $[1, n]$; diff --git a/solution/2200-2299/2213.Longest Substring of One Repeating Character/README_EN.md b/solution/2200-2299/2213.Longest Substring of One Repeating Character/README_EN.md index 57b1824ce85da..0da37c9b62ed1 100644 --- a/solution/2200-2299/2213.Longest Substring of One Repeating Character/README_EN.md +++ b/solution/2200-2299/2213.Longest Substring of One Repeating Character/README_EN.md @@ -72,7 +72,7 @@ Thus, we return [2,3]. ### Solution 1: Segment Tree -The segment tree divides the entire interval into multiple non-continuous sub-intervals, and the number of sub-intervals does not exceed $\log(\text{width})$. To update the value of an element, you only need to update $\log(\text{width})$ intervals, and these intervals are all contained in a large interval that contains the element. When modifying the interval, you need to use **lazy tags** to ensure efficiency. +The segment tree divides the entire interval into multiple non-continuous sub-intervals, and the number of sub-intervals does not exceed $\log(\textit{width})$. To update the value of an element, you only need to update $\log(\textit{width})$ intervals, and these intervals are all contained in a large interval that contains the element. When modifying the interval, you need to use **lazy tags** to ensure efficiency. - Each node of the segment tree represents an interval; - The segment tree has a unique root node, which represents the entire statistical range, such as $[1, n]$; diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md b/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md index e985faeb63f3d..1e68fa33c3481 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md @@ -84,11 +84,11 @@ tags: ### 方法一:哈希表 + 排序 -我们用一个哈希表 $\text{cnt}$ 记录每个玩家输掉的比赛场次。 +我们用一个哈希表 $\textit{cnt}$ 记录每个玩家输掉的比赛场次。 -然后遍历哈希表,将输掉 $0$ 场比赛的玩家放入 $\text{ans}[0]$,将输掉 $1$ 场比赛的玩家放入 $\text{ans}[1]$。 +然后遍历哈希表,将输掉 $0$ 场比赛的玩家放入 $\textit{ans}[0]$,将输掉 $1$ 场比赛的玩家放入 $\textit{ans}[1]$。 -最后将 $\text{ans}[0]$ 和 $\text{ans}[1]$ 按照升序排序,返回结果。 +最后将 $\textit{ans}[0]$ 和 $\textit{ans}[1]$ 按照升序排序,返回结果。 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为比赛场次数。 diff --git a/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md b/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md index 11eb458892958..1648191598826 100644 --- a/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md +++ b/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md @@ -69,7 +69,7 @@ It can be proven that there is no longer path that satisfies the conditions. First, we construct an adjacency list $g$ based on the array $parent$, where $g[i]$ represents all child nodes of node $i$. -Then we start DFS from the root node. For each node $i$, we traverse each child node $j$ in $g[i]$. If $s[i] \neq s[j]$, then we can start from node $i$, pass through node $j$, and reach a leaf node. The length of this path is $x = 1 + \text{dfs}(j)$. We use $mx$ to record the longest path length starting from node $i$. At the same time, we update the answer $ans = \max(ans, mx + x)$ during the traversal process. +Then we start DFS from the root node. For each node $i$, we traverse each child node $j$ in $g[i]$. If $s[i] \neq s[j]$, then we can start from node $i$, pass through node $j$, and reach a leaf node. The length of this path is $x = 1 + \textit{dfs}(j)$. We use $mx$ to record the longest path length starting from node $i$. At the same time, we update the answer $ans = \max(ans, mx + x)$ during the traversal process. Finally, we return $ans + 1$. diff --git a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README.md b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README.md index 38cc90cc5e64b..09df5942ff2c5 100644 --- a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README.md +++ b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README.md @@ -81,7 +81,7 @@ tags: 考虑 $f[i][j]$ 如何进行状态转移。对于 $f[i]$,我们枚举所有城市 $j$,如果 $i$ 的第 $j$ 位为 $1$,那么我们就可以从其它城市 $h$ 经过公路到达城市 $j$,此时 $f[i][j]$ 的值为 $f[i][h]+cost(h, j)$ 的最大值,其中 $cost(h, j)$ 表示从城市 $h$ 到城市 $j$ 的旅行费用。因此,我们可以得到状态转移方程: $$ -f[i][j]=\max_{h \in \text{city}}\{f[i \backslash j][h]+cost(h, j)\} +f[i][j]=\max_{h \in \textit{city}}\{f[i \backslash j][h]+cost(h, j)\} $$ 其中 $i \backslash j$ 表示将 $i$ 的第 $j$ 位变为 $0$。 diff --git a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md index 41885867f0eb1..66626105ebb1f 100644 --- a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md +++ b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md @@ -79,7 +79,7 @@ We use $f[i][j]$ to represent the maximum travel cost when the cities that have Consider how $f[i][j]$ transitions. For $f[i]$, we enumerate all cities $j$. If the $j$-th bit of $i$ is $1$, then we can reach city $j$ from other city $h$ through the road, at this time the value of $f[i][j]$ is the maximum value of $f[i][h]+cost(h, j)$, where $cost(h, j)$ represents the travel cost from city $h$ to city $j$. Therefore, we can get the state transition equation: $$ -f[i][j]=\max_{h \in \text{city}}\{f[i \backslash j][h]+cost(h, j)\} +f[i][j]=\max_{h \in \textit{city}}\{f[i \backslash j][h]+cost(h, j)\} $$ where $i \backslash j$ represents changing the $j$-th bit of $i$ to $0$. diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md b/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md index ce52b7fe85081..aa8bea422c839 100644 --- a/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md @@ -83,7 +83,7 @@ tags: ### 方法一:贪心 + 排序 -我们考虑每个城市对所有道路的总重要性的贡献度,记录在数组 $\text{deg}$ 中。然后将 $\text{deg}$ 按贡献度从小到大排序,为城市依次分配 $[1, 2, ..., n]$。 +我们考虑每个城市对所有道路的总重要性的贡献度,记录在数组 $\textit{deg}$ 中。然后将 $\textit{deg}$ 按贡献度从小到大排序,为城市依次分配 $[1, 2, ..., n]$。 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。 diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md b/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md index f5553a84d30d1..fd43db7cc0e5a 100644 --- a/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md @@ -79,7 +79,7 @@ It can be shown that we cannot obtain a greater total importance than 20. ### Solution 1: Greedy + Sorting -We consider the contribution of each city to the total importance of all roads, recorded in the array $\text{deg}$. Then, we sort $\text{deg}$ by contribution from smallest to largest and allocate $[1, 2, ..., n]$ to the cities in order. +We consider the contribution of each city to the total importance of all roads, recorded in the array $\textit{deg}$. Then, we sort $\textit{deg}$ by contribution from smallest to largest and allocate $[1, 2, ..., n]$ to the cities in order. The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. diff --git a/solution/2200-2299/2296.Design a Text Editor/README_EN.md b/solution/2200-2299/2296.Design a Text Editor/README_EN.md index 18a663629271f..daa68bca89b23 100644 --- a/solution/2200-2299/2296.Design a Text Editor/README_EN.md +++ b/solution/2200-2299/2296.Design a Text Editor/README_EN.md @@ -101,7 +101,7 @@ textEditor.cursorRight(6); // return "practi" We can use two stacks, `left` and `right`, where the `left` stack stores the characters to the left of the cursor, and the `right` stack stores the characters to the right of the cursor. -- When the `addText` method is called, we push the characters from `text` onto the `left` stack one by one. The time complexity is $O(|\text{text}|)$. +- When the `addText` method is called, we push the characters from `text` onto the `left` stack one by one. The time complexity is $O(|\textit{text}|)$. - When the `deleteText` method is called, we pop characters from the `left` stack up to $k$ times. The time complexity is $O(k)$. - When the `cursorLeft` method is called, we pop characters from the `left` stack up to $k$ times, then push the popped characters onto the `right` stack, and finally return up to $10$ characters from the `left` stack. The time complexity is $O(k)$. - When the `cursorRight` method is called, we pop characters from the `right` stack up to $k$ times, then push the popped characters onto the `left` stack, and finally return up to $10$ characters from the `left` stack. The time complexity is $O(k)$. diff --git a/solution/2300-2399/2304.Minimum Path Cost in a Grid/README.md b/solution/2300-2399/2304.Minimum Path Cost in a Grid/README.md index 4f6ea9b5c3fab..5eb9b132567c7 100644 --- a/solution/2300-2399/2304.Minimum Path Cost in a Grid/README.md +++ b/solution/2300-2399/2304.Minimum Path Cost in a Grid/README.md @@ -78,10 +78,10 @@ tags: 我们定义 $f[i][j]$ 表示从第一行出发,到达第 $i$ 行第 $j$ 列的最小路径代价。由于每次只能从上一行的某一列移动到当前行的某一列,因此 $f[i][j]$ 的值可以从 $f[i - 1][k]$ 转移而来,其中 $k$ 的取值范围为 $[0, n - 1]$。因此状态转移方程为: $$ -f[i][j] = \min_{0 \leq k < n} \{f[i - 1][k] + \text{moveCost}[grid[i - 1][k]][j] + grid[i][j]\} +f[i][j] = \min_{0 \leq k < n} \{f[i - 1][k] + \textit{moveCost}[grid[i - 1][k]][j] + grid[i][j]\} $$ -其中 $\text{moveCost}[grid[i - 1][k]][j]$ 表示从第 $i - 1$ 行第 $k$ 列移动到第 $i$ 行第 $j$ 列的代价。 +其中 $\textit{moveCost}[grid[i - 1][k]][j]$ 表示从第 $i - 1$ 行第 $k$ 列移动到第 $i$ 行第 $j$ 列的代价。 最终答案即为 $\min_{0 \leq j < n} \{f[m - 1][j]\}$。 diff --git a/solution/2300-2399/2304.Minimum Path Cost in a Grid/README_EN.md b/solution/2300-2399/2304.Minimum Path Cost in a Grid/README_EN.md index a6cf4c68b7f89..9afaab6168d01 100644 --- a/solution/2300-2399/2304.Minimum Path Cost in a Grid/README_EN.md +++ b/solution/2300-2399/2304.Minimum Path Cost in a Grid/README_EN.md @@ -74,10 +74,10 @@ So the total cost of this path is 5 + 1 = 6. We define $f[i][j]$ to represent the minimum path cost from the first row to the $i$th row and $j$th column. Since we can only move from a column in the previous row to a column in the current row, the value of $f[i][j]$ can be transferred from $f[i - 1][k]$, where the range of $k$ is $[0, n - 1]$. Therefore, the state transition equation is: $$ -f[i][j] = \min_{0 \leq k < n} \{f[i - 1][k] + \text{moveCost}[grid[i - 1][k]][j] + grid[i][j]\} +f[i][j] = \min_{0 \leq k < n} \{f[i - 1][k] + \textit{moveCost}[grid[i - 1][k]][j] + grid[i][j]\} $$ -where $\text{moveCost}[grid[i - 1][k]][j]$ represents the cost of moving from the $k$th column of the $i - 1$th row to the $j$th column of the $i$th row. +where $\textit{moveCost}[grid[i - 1][k]][j]$ represents the cost of moving from the $k$th column of the $i - 1$th row to the $j$th column of the $i$th row. The final answer is $\min_{0 \leq j < n} \{f[m - 1][j]\}$. diff --git a/solution/2300-2399/2326.Spiral Matrix IV/README.md b/solution/2300-2399/2326.Spiral Matrix IV/README.md index beb216d8be181..d383fdda5be14 100644 --- a/solution/2300-2399/2326.Spiral Matrix IV/README.md +++ b/solution/2300-2399/2326.Spiral Matrix IV/README.md @@ -65,11 +65,11 @@ tags: ### 方法一:模拟 -我们定义一个二维数组 $\text{ans}$,用来存放链表中的元素,初始时全部填充为 $-1$。定义三个变量 $i, j, k$,分别表示当前的行、列和方向。定义一个数组 $\text{dirs}$,表示四个方向的偏移量。 +我们定义一个二维数组 $\textit{ans}$,用来存放链表中的元素,初始时全部填充为 $-1$。定义三个变量 $i, j, k$,分别表示当前的行、列和方向。定义一个数组 $\textit{dirs}$,表示四个方向的偏移量。 -然后我们开始遍历链表,每次遍历一个节点,就将当前节点的值填充到 $\text{ans}[i][j]$ 中,然后更新链表的指针,如果链表为空,说明所有的元素都已经填充完毕,退出循环。 +然后我们开始遍历链表,每次遍历一个节点,就将当前节点的值填充到 $\textit{ans}[i][j]$ 中,然后更新链表的指针,如果链表为空,说明所有的元素都已经填充完毕,退出循环。 -否则,我们需要找到下一个元素的位置,我们可以通过当前位置 $(i, j)$ 和当前方向 $k$ 来计算下一个位置 $(x, y)$,如果 $(x, y)$ 在矩阵的范围内,并且 $\text{ans}[x][y]$ 为 $-1$,说明 $(x, y)$ 还没有被填充过,我们就将 $(x, y)$ 作为下一个位置,否则我们需要更换方向。 +否则,我们需要找到下一个元素的位置,我们可以通过当前位置 $(i, j)$ 和当前方向 $k$ 来计算下一个位置 $(x, y)$,如果 $(x, y)$ 在矩阵的范围内,并且 $\textit{ans}[x][y]$ 为 $-1$,说明 $(x, y)$ 还没有被填充过,我们就将 $(x, y)$ 作为下一个位置,否则我们需要更换方向。 遍历完链表之后,我们就得到了一个螺旋矩阵,返回即可。 diff --git a/solution/2300-2399/2326.Spiral Matrix IV/README_EN.md b/solution/2300-2399/2326.Spiral Matrix IV/README_EN.md index 0bfc63775c8af..f53ce3ace7e53 100644 --- a/solution/2300-2399/2326.Spiral Matrix IV/README_EN.md +++ b/solution/2300-2399/2326.Spiral Matrix IV/README_EN.md @@ -65,11 +65,11 @@ The last space in the matrix is set to -1. ### Solution 1: Simulation -We define a two-dimensional array $\text{ans}$ to store the elements in the linked list, initially all filled with $-1$. We define three variables $i, j, k$, representing the current row, column, and direction respectively. We define an array $\text{dirs}$ to represent the offsets of the four directions. +We define a two-dimensional array $\textit{ans}$ to store the elements in the linked list, initially all filled with $-1$. We define three variables $i, j, k$, representing the current row, column, and direction respectively. We define an array $\textit{dirs}$ to represent the offsets of the four directions. -Then we start traversing the linked list. Each time we traverse a node, we fill the current node's value into $\text{ans}[i][j]$, then update the linked list pointer. If the linked list is empty, it means all elements have been filled and we exit the loop. +Then we start traversing the linked list. Each time we traverse a node, we fill the current node's value into $\textit{ans}[i][j]$, then update the linked list pointer. If the linked list is empty, it means all elements have been filled and we exit the loop. -Otherwise, we need to find the position of the next element. We can calculate the next position $(x, y)$ through the current position $(i, j)$ and the current direction $k$. If $(x, y)$ is within the range of the matrix, and $\text{ans}[x][y]$ is $-1$, it means $(x, y)$ has not been filled yet, so we take $(x, y)$ as the next position. Otherwise, we need to change the direction. +Otherwise, we need to find the position of the next element. We can calculate the next position $(x, y)$ through the current position $(i, j)$ and the current direction $k$. If $(x, y)$ is within the range of the matrix, and $\textit{ans}[x][y]$ is $-1$, it means $(x, y)$ has not been filled yet, so we take $(x, y)$ as the next position. Otherwise, we need to change the direction. After traversing the linked list, we get a spiral matrix and return it. diff --git a/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README.md b/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README.md index 700b600874329..d06d2e49acb68 100644 --- a/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README.md +++ b/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README.md @@ -84,7 +84,7 @@ OR 运算节点的值为 True OR False = True 。 对于当前节点 $\textit{root}$: -- 如果其左孩子为空,说明当前节点是叶子节点。如果当前节点的值为 $1$,则返回 $\text{true}$,否则返回 $\text{false}$; +- 如果其左孩子为空,说明当前节点是叶子节点。如果当前节点的值为 $1$,则返回 $\textit{true}$,否则返回 $\textit{false}$; - 如果当前节点的值为 $2$,则返回其左孩子和右孩子的递归结果的逻辑或,否则返回其左孩子和右孩子的递归结果的逻辑与。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。 diff --git a/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README_EN.md b/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README_EN.md index b137ba6200fe3..82de05e0db763 100644 --- a/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README_EN.md +++ b/solution/2300-2399/2331.Evaluate Boolean Binary Tree/README_EN.md @@ -82,7 +82,7 @@ We can use recursion to solve this problem. For the current node $\textit{root}$: -- If its left child is null, it means the current node is a leaf node. If the value of the current node is $1$, then return $\text{true}$; otherwise, return $\text{false}$; +- If its left child is null, it means the current node is a leaf node. If the value of the current node is $1$, then return $\textit{true}$; otherwise, return $\textit{false}$; - If the value of the current node is $2$, then return the logical OR of the recursion results of its left and right children; otherwise, return the logical AND of the recursion results of its left and right children. The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. diff --git a/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/README.md b/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/README.md index 2715c2822e1bf..bd2448c8485d9 100644 --- a/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/README.md +++ b/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/README.md @@ -71,7 +71,7 @@ tags: $v$ 在数组 $nums$ 中的下标为 $i$,若下标 $i-1$ 对应的元素遍历过,可以将 $i-1$ 与 $i$ 进行合并,同理,若下标 $i+1$ 对应的元素也遍历过了,将 $i$ 与 $i+1$ 合并。合并过程中更新连通块的大小。 -$v$ 作为当前连通块的最小值,当前连通块的大小为 $size[find(i)]$,若 $v>\frac{\text{threshold}}{size[find(i)]}$,说明找到了满足条件的子数组,返回 $true$。 +$v$ 作为当前连通块的最小值,当前连通块的大小为 $size[find(i)]$,若 $v>\frac{\textit{threshold}}{size[find(i)]}$,说明找到了满足条件的子数组,返回 $true$。 否则遍历结束,返回 $-1$。 @@ -277,7 +277,7 @@ func validSubarraySize(nums []int, threshold int) int { 利用单调栈,得到以当前元素 $nums[i]$ 作为最小元素的左右边界 $left[i]$(左边第一个比 $nums[i]$ 小的元素的位置), $right[i]$(右边第一个比 $nums[i]$ 小的元素的位置)。 -那么对于当前元素 $nums[i]$,有 $k=right[i]-left[i]-1$,若 $nums[i]>\frac{\text{threshold}}{k}$,说明找到了满足条件的子数组,返回 $true$。 +那么对于当前元素 $nums[i]$,有 $k=right[i]-left[i]-1$,若 $nums[i]>\frac{\textit{threshold}}{k}$,说明找到了满足条件的子数组,返回 $true$。 否则遍历结束,返回 $-1$。 diff --git a/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README.md b/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README.md index a3cc8407d5b49..d69f4bcca811f 100644 --- a/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README.md +++ b/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README.md @@ -80,12 +80,12 @@ tags: 即: $$ -dfs(i) = \text{OR} +dfs(i) = \textit{OR} \begin{cases} true,&i \ge n\\ -dfs(i+2),&i+1 < n\ \text{and}\ \textit{nums}[i] = \textit{nums}[i+1]\\ -dfs(i+3),&i+2 < n\ \text{and}\ \textit{nums}[i] = \textit{nums}[i+1] = \textit{nums}[i+2]\\ -dfs(i+3),&i+2 < n\ \text{and}\ \textit{nums}[i+1] - \textit{nums}[i] = 1\ \text{and}\ \textit{nums}[i+2] - \textit{nums}[i+1] = 1 +dfs(i+2),&i+1 < n\ \textit{and}\ \textit{nums}[i] = \textit{nums}[i+1]\\ +dfs(i+3),&i+2 < n\ \textit{and}\ \textit{nums}[i] = \textit{nums}[i+1] = \textit{nums}[i+2]\\ +dfs(i+3),&i+2 < n\ \textit{and}\ \textit{nums}[i+1] - \textit{nums}[i] = 1\ \textit{and}\ \textit{nums}[i+2] - \textit{nums}[i+1] = 1 \end{cases} $$ @@ -248,12 +248,12 @@ function validPartition(nums: number[]): boolean { 状态转移方程如下: $$ -f[i] = \text{OR} +f[i] = \textit{OR} \begin{cases} true,&i = 0\\ -f[i-2],&i-2 \ge 0\ \text{and}\ \textit{nums}[i-1] = \textit{nums}[i-2]\\ -f[i-3],&i-3 \ge 0\ \text{and}\ \textit{nums}[i-1] = \textit{nums}[i-2] = \textit{nums}[i-3]\\ -f[i-3],&i-3 \ge 0\ \text{and}\ \textit{nums}[i-1] - \textit{nums}[i-2] = 1\ \text{and}\ \textit{nums}[i-2] - \textit{nums}[i-3] = 1 +f[i-2],&i-2 \ge 0\ \textit{and}\ \textit{nums}[i-1] = \textit{nums}[i-2]\\ +f[i-3],&i-3 \ge 0\ \textit{and}\ \textit{nums}[i-1] = \textit{nums}[i-2] = \textit{nums}[i-3]\\ +f[i-3],&i-3 \ge 0\ \textit{and}\ \textit{nums}[i-1] - \textit{nums}[i-2] = 1\ \textit{and}\ \textit{nums}[i-2] - \textit{nums}[i-3] = 1 \end{cases} $$ diff --git a/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README_EN.md b/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README_EN.md index 470bf722492a4..1fb1739e20a85 100644 --- a/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README_EN.md +++ b/solution/2300-2399/2369.Check if There is a Valid Partition For The Array/README_EN.md @@ -78,12 +78,12 @@ The execution process of the function $dfs(i)$ is as follows: That is: $$ -dfs(i) = \text{OR} +dfs(i) = \textit{OR} \begin{cases} true,&i \ge n\\ -dfs(i+2),&i+1 < n\ \text{and}\ \textit{nums}[i] = \textit{nums}[i+1]\\ -dfs(i+3),&i+2 < n\ \text{and}\ \textit{nums}[i] = \textit{nums}[i+1] = \textit{nums}[i+2]\\ -dfs(i+3),&i+2 < n\ \text{and}\ \textit{nums}[i+1] - \textit{nums}[i] = 1\ \text{and}\ \textit{nums}[i+2] - \textit{nums}[i+1] = 1 +dfs(i+2),&i+1 < n\ \textit{and}\ \textit{nums}[i] = \textit{nums}[i+1]\\ +dfs(i+3),&i+2 < n\ \textit{and}\ \textit{nums}[i] = \textit{nums}[i+1] = \textit{nums}[i+2]\\ +dfs(i+3),&i+2 < n\ \textit{and}\ \textit{nums}[i+1] - \textit{nums}[i] = 1\ \textit{and}\ \textit{nums}[i+2] - \textit{nums}[i+1] = 1 \end{cases} $$ @@ -246,12 +246,12 @@ Let $f[i]$ represent whether there is a valid partition for the first $i$ elemen The state transition equation is as follows: $$ -f[i] = \text{OR} +f[i] = \textit{OR} \begin{cases} true,&i = 0\\ -f[i-2],&i-2 \ge 0\ \text{and}\ \textit{nums}[i-1] = \textit{nums}[i-2]\\ -f[i-3],&i-3 \ge 0\ \text{and}\ \textit{nums}[i-1] = \textit{nums}[i-2] = \textit{nums}[i-3]\\ -f[i-3],&i-3 \ge 0\ \text{and}\ \textit{nums}[i-1] - \textit{nums}[i-2] = 1\ \text{and}\ \textit{nums}[i-2] - \textit{nums}[i-3] = 1 +f[i-2],&i-2 \ge 0\ \textit{and}\ \textit{nums}[i-1] = \textit{nums}[i-2]\\ +f[i-3],&i-3 \ge 0\ \textit{and}\ \textit{nums}[i-1] = \textit{nums}[i-2] = \textit{nums}[i-3]\\ +f[i-3],&i-3 \ge 0\ \textit{and}\ \textit{nums}[i-1] - \textit{nums}[i-2] = 1\ \textit{and}\ \textit{nums}[i-2] - \textit{nums}[i-3] = 1 \end{cases} $$ diff --git a/solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/README.md b/solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/README.md index 6e7b9df44cbc1..c5879e5c7c7d6 100644 --- a/solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/README.md +++ b/solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/README.md @@ -74,9 +74,9 @@ tags: ### 方法一:两次 DFS -我们先通过一次 $\text{DFS}$ 建图,得到一个邻接表 $g$,其中 $g[node]$ 表示与节点 $node$ 相连的所有节点。 +我们先通过一次 $\textit{DFS}$ 建图,得到一个邻接表 $g$,其中 $g[node]$ 表示与节点 $node$ 相连的所有节点。 -然后,我们以 $start$ 作为起点,通过 $\text{DFS}$ 搜索整棵树,找到最远距离,即为答案。 +然后,我们以 $start$ 作为起点,通过 $\textit{DFS}$ 搜索整棵树,找到最远距离,即为答案。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。 diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md index 540904b95e0cc..7e95a8206c7d8 100644 --- a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README.md @@ -88,7 +88,7 @@ tags: 根据题目描述,每一辆垃圾车从房子 $0$ 出发,收集其中一种垃圾,按顺序前进,直到到达该种垃圾最后出现的房子下标为止。 -因此,我们可以用一个哈希表 $\text{last}$ 记录每种垃圾最后出现的房子下标。我们假设第 $i$ 种垃圾最后一次出现在第 $j$ 个房子,那么第 $i$ 辆车所需要的行驶时间为 $\text{travel}[0] + \text{travel}[1] + \cdots + \text{travel}[j-1]$。注意,如果 $j = 0$,则不需要行驶时间。我们通过前缀和累计所有车辆的行驶时间,加上每种垃圾的总收集时间,即可得到答案。 +因此,我们可以用一个哈希表 $\textit{last}$ 记录每种垃圾最后出现的房子下标。我们假设第 $i$ 种垃圾最后一次出现在第 $j$ 个房子,那么第 $i$ 辆车所需要的行驶时间为 $\textit{travel}[0] + \textit{travel}[1] + \cdots + \textit{travel}[j-1]$。注意,如果 $j = 0$,则不需要行驶时间。我们通过前缀和累计所有车辆的行驶时间,加上每种垃圾的总收集时间,即可得到答案。 时间复杂度 $O(n)$,空间复杂度 $O(k)$,其中 $n$ 和 $k$ 分别是垃圾的数量和种类。本题中 $k = 3$。 diff --git a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md index 4c6400d0066e5..e28b752fc57e8 100644 --- a/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md +++ b/solution/2300-2399/2391.Minimum Amount of Time to Collect Garbage/README_EN.md @@ -88,7 +88,7 @@ It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage. According to the problem description, each garbage truck starts from house $0$, collects one type of garbage, and moves forward in order until it reaches the house index where this type of garbage last appears. -Therefore, we can use a hash table $\text{last}$ to record the house index where each type of garbage last appears. We assume that the $i$-th type of garbage last appears in the $j$-th house, then the driving time required for the $i$-th truck is $\text{travel}[0] + \text{travel}[1] + \cdots + \text{travel}[j-1]$. Note, if $j = 0$, no driving time is needed. We accumulate the driving time of all vehicles, add the total collection time of each type of garbage, and we can get the answer. +Therefore, we can use a hash table $\textit{last}$ to record the house index where each type of garbage last appears. We assume that the $i$-th type of garbage last appears in the $j$-th house, then the driving time required for the $i$-th truck is $\textit{travel}[0] + \textit{travel}[1] + \cdots + \textit{travel}[j-1]$. Note, if $j = 0$, no driving time is needed. We accumulate the driving time of all vehicles, add the total collection time of each type of garbage, and we can get the answer. The time complexity is $O(n)$, and the space complexity is $O(k)$, where $n$ and $k$ are the number and types of garbage, respectively. In this problem, $k = 3$. diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md index 445c9a76d3b36..4ab489e60e02e 100644 --- a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README.md @@ -67,9 +67,9 @@ tags: ### 方法一:暴力枚举 -我们直接遍历数组 $\text{queries}$ 中的每个单词 $s$,再遍历数组 $\text{dictionary}$ 中的每个单词 $t$,如果存在一个单词 $t$ 与 $s$ 的编辑距离小于 $3$,则将 $s$ 加入答案数组中,然后退出内层循环的遍历。如果不存在这样的单词 $t$,则继续遍历下一个单词 $s$。 +我们直接遍历数组 $\textit{queries}$ 中的每个单词 $s$,再遍历数组 $\textit{dictionary}$ 中的每个单词 $t$,如果存在一个单词 $t$ 与 $s$ 的编辑距离小于 $3$,则将 $s$ 加入答案数组中,然后退出内层循环的遍历。如果不存在这样的单词 $t$,则继续遍历下一个单词 $s$。 -时间复杂度 $O(m \times n \times l)$,其中 $m$ 和 $n$ 分别是数组 $\text{queries}$ 和 $\text{dictionary}$ 的长度,而 $l$ 是单词的长度。 +时间复杂度 $O(m \times n \times l)$,其中 $m$ 和 $n$ 分别是数组 $\textit{queries}$ 和 $\textit{dictionary}$ 的长度,而 $l$ 是单词的长度。 diff --git a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md index 7d08b933b83d0..129e720d9db6d 100644 --- a/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md +++ b/solution/2400-2499/2452.Words Within Two Edits of Dictionary/README_EN.md @@ -66,9 +66,9 @@ Applying any two edits to "yes" cannot make it equal to "not" ### Solution 1: Brute Force Enumeration -We directly traverse each word $s$ in the array $\text{queries}$, and then traverse each word $t$ in the array $\text{dictionary}$. If there exists a word $t$ whose edit distance from $s$ is less than $3$, we add $s$ to the answer array and then exit the inner loop. If there is no such word $t$, we continue to traverse the next word $s$. +We directly traverse each word $s$ in the array $\textit{queries}$, and then traverse each word $t$ in the array $\textit{dictionary}$. If there exists a word $t$ whose edit distance from $s$ is less than $3$, we add $s$ to the answer array and then exit the inner loop. If there is no such word $t$, we continue to traverse the next word $s$. -The time complexity is $O(m \times n \times l)$, where $m$ and $n$ are the lengths of the arrays $\text{queries}$ and $\text{dictionary}$ respectively, and $l$ is the length of the word. +The time complexity is $O(m \times n \times l)$, where $m$ and $n$ are the lengths of the arrays $\textit{queries}$ and $\textit{dictionary}$ respectively, and $l$ is the length of the word. diff --git a/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README.md b/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README.md index 6b7769b4edb40..6c9b10368fd0b 100644 --- a/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README.md +++ b/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README.md @@ -81,9 +81,9 @@ tags: 综上,可以得到: $$ -\text{ans} = \begin{cases} -n, & n \gt 1 \text{ 且 } n \text{ 为奇数} \\ -\frac{n}{2}, & n \text{ 为其它} \\ +\textit{ans} = \begin{cases} +n, & n \gt 1 \textit{ 且 } n \textit{ 为奇数} \\ +\frac{n}{2}, & n \textit{ 为其它} \\ \end{cases} $$ diff --git a/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README_EN.md b/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README_EN.md index d71da0ef58172..b715562f2429e 100644 --- a/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README_EN.md +++ b/solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README_EN.md @@ -73,9 +73,9 @@ Also note that the first cut will not divide the circle into distinct parts. In summary, we can get: $$ -\text{ans} = \begin{cases} -n, & n \gt 1 \text{ and } n \text{ is odd} \\ -\frac{n}{2}, & n \text{ is even} \\ +\textit{ans} = \begin{cases} +n, & n \gt 1 \textit{ and } n \textit{ is odd} \\ +\frac{n}{2}, & n \textit{ is even} \\ \end{cases} $$ diff --git a/solution/2500-2599/2518.Number of Great Partitions/README.md b/solution/2500-2599/2518.Number of Great Partitions/README.md index badaa2171d00a..d2f2e12f1d83e 100644 --- a/solution/2500-2599/2518.Number of Great Partitions/README.md +++ b/solution/2500-2599/2518.Number of Great Partitions/README.md @@ -80,8 +80,8 @@ tags: $$ f[i][j] = \left\{ \begin{aligned} -&f[i - 1][j] & \text{如果不选第 } i \text{ 个元素} \\ -&f[i - 1][j - nums[i - 1]] & \text{如果选第 } i \text{ 个元素} +&f[i - 1][j] & \textit{如果不选第 } i \textit{ 个元素} \\ +&f[i - 1][j - nums[i - 1]] & \textit{如果选第 } i \textit{ 个元素} \end{aligned} \right. $$ diff --git a/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README.md b/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README.md index 156ce91fde6a3..9ceb53f29ed73 100644 --- a/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README.md +++ b/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README.md @@ -88,7 +88,7 @@ tags: 在这个过程中,我们可以维护两个数组 `rowMax` 和 `colMax`,分别记录每一行和每一列的最大递增长度。初始时,这两个数组的所有元素都为 $0$。 -对于每个值对应的所有单元格位置,我们按照位置顺序遍历,对于每个位置 $(i, j)$,我们可以计算出以该位置为终点的最大递增长度为 $1 + \max(\text{rowMax}[i], \text{colMax}[j])$,更新答案,然后更新 `rowMax[i]` 和 `colMax[j]`。 +对于每个值对应的所有单元格位置,我们按照位置顺序遍历,对于每个位置 $(i, j)$,我们可以计算出以该位置为终点的最大递增长度为 $1 + \max(\textit{rowMax}[i], \textit{colMax}[j])$,更新答案,然后更新 `rowMax[i]` 和 `colMax[j]`。 最后返回答案即可。 diff --git a/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README_EN.md b/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README_EN.md index 00a2665eeb8f1..dd24f265ea7f7 100644 --- a/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README_EN.md +++ b/solution/2700-2799/2713.Maximum Strictly Increasing Cells in a Matrix/README_EN.md @@ -87,7 +87,7 @@ Based on the problem description, the value of the cells we move through in sequ During this process, we can maintain two arrays `rowMax` and `colMax`, which record the maximum increasing length of each row and column, respectively. Initially, all elements of these two arrays are $0$. -For all cell positions corresponding to each value, we traverse them in order of position. For each position $(i, j)$, we can calculate the maximum increasing length ending at that position as $1 + \max(\text{rowMax}[i], \text{colMax}[j])$, update the answer, and then update `rowMax[i]` and `colMax[j]`. +For all cell positions corresponding to each value, we traverse them in order of position. For each position $(i, j)$, we can calculate the maximum increasing length ending at that position as $1 + \max(\textit{rowMax}[i], \textit{colMax}[j])$, update the answer, and then update `rowMax[i]` and `colMax[j]`. Finally, return the answer. diff --git a/solution/2700-2799/2741.Special Permutations/README.md b/solution/2700-2799/2741.Special Permutations/README.md index 0ece2b8547030..37965fd979f2f 100644 --- a/solution/2700-2799/2741.Special Permutations/README.md +++ b/solution/2700-2799/2741.Special Permutations/README.md @@ -72,7 +72,7 @@ $$ f[i][j]= \begin{cases} 1, & i=2^j\\ -\sum_{k=0}^{n-1}f[i \oplus 2^j][k], & i \neq 2^j \text{且} \text{nums}[j] \text{与} \text{nums}[k] \text{满足题目要求}\\ +\sum_{k=0}^{n-1}f[i \oplus 2^j][k], & i \neq 2^j \textit{且} \textit{nums}[j] \textit{与} \textit{nums}[k] \textit{满足题目要求}\\ \end{cases} $$ diff --git a/solution/2700-2799/2741.Special Permutations/README_EN.md b/solution/2700-2799/2741.Special Permutations/README_EN.md index daa009047eecd..4e28f9ab8191e 100644 --- a/solution/2700-2799/2741.Special Permutations/README_EN.md +++ b/solution/2700-2799/2741.Special Permutations/README_EN.md @@ -72,7 +72,7 @@ $$ f[i][j]= \begin{cases} 1, & i=2^j\\ -\sum_{k=0}^{n-1}f[i \oplus 2^j][k], & i \neq 2^j \text{ and nums}[j] \text{ and nums}[k] \text{ meet the requirements of the problem}\\ +\sum_{k=0}^{n-1}f[i \oplus 2^j][k], & i \neq 2^j \textit{ and nums}[j] \textit{ and nums}[k] \textit{ meet the requirements of the problem}\\ \end{cases} $$ diff --git a/solution/2700-2799/2747.Count Zero Request Servers/README.md b/solution/2700-2799/2747.Count Zero Request Servers/README.md index 4620843d987cf..9bbe130247898 100644 --- a/solution/2700-2799/2747.Count Zero Request Servers/README.md +++ b/solution/2700-2799/2747.Count Zero Request Servers/README.md @@ -78,7 +78,7 @@ tags: 对于每个查询 $q = (r, i)$,其窗口左边界为 $l = r - x$,我们需要统计在窗口 $[l, r]$ 内有多少个服务器收到了请求。我们用双指针 $j$ 和 $k$ 分别维护窗口的左右边界,初始时 $j = k = 0$。每一次,如果 $k$ 指向的日志的时间小于等于 $r$,我们就将其加入到窗口中,然后将 $k$ 向右移动一位。如果 $j$ 指向的日志的时间小于 $l$,我们就将其从窗口中移除,然后将 $j$ 向右移动一位。在移动的过程中,我们需要统计窗口中有多少个不同的服务器,这可以使用哈希表来实现。移动结束后,当前时间区间中没有收到请求的服务器数目就是 $n$ 减去哈希表中不同的服务器数目。 -时间复杂度 $O(l \times \log l + m \times \log m + n)$,空间复杂度 $O(l + m)$。其中 $l$ 和 $n$ 分别是数组 $\text{logs}$ 的长度和服务器的数量,而 $m$ 是数组 $\text{queries}$ 的长度。 +时间复杂度 $O(l \times \log l + m \times \log m + n)$,空间复杂度 $O(l + m)$。其中 $l$ 和 $n$ 分别是数组 $\textit{logs}$ 的长度和服务器的数量,而 $m$ 是数组 $\textit{queries}$ 的长度。 diff --git a/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md b/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md index 1c6a234423f98..86c17b3d8188f 100644 --- a/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md +++ b/solution/2700-2799/2747.Count Zero Request Servers/README_EN.md @@ -78,7 +78,7 @@ We can sort all the queries by time from smallest to largest, and then process e For each query $q = (r, i)$, its window left boundary is $l = r - x$, and we need to count how many servers received requests within the window $[l, r]$. We use two pointers $j$ and $k$ to maintain the left and right boundaries of the window, initially $j = k = 0$. Each time, if the log time pointed by $k$ is less than or equal to $r$, we add it to the window, and then move $k$ to the right by one. If the log time pointed by $j$ is less than $l$, we remove it from the window, and then move $j$ to the right by one. During the movement, we need to count how many different servers are in the window, which can be implemented using a hash table. After the movement, the number of servers that did not receive requests in the current time interval is $n$ minus the number of different servers in the hash table. -The time complexity is $O(l \times \log l + m \times \log m + n)$, and the space complexity is $O(l + m)$. Here, $l$ and $n$ are the lengths of the arrays $\text{logs}$ and the number of servers, respectively, while $m$ is the length of the array $\text{queries}$. +The time complexity is $O(l \times \log l + m \times \log m + n)$, and the space complexity is $O(l + m)$. Here, $l$ and $n$ are the lengths of the arrays $\textit{logs}$ and the number of servers, respectively, while $m$ is the length of the array $\textit{queries}$. diff --git a/solution/2700-2799/2748.Number of Beautiful Pairs/README.md b/solution/2700-2799/2748.Number of Beautiful Pairs/README.md index 4cf22d60ac534..3772d10b027fd 100644 --- a/solution/2700-2799/2748.Number of Beautiful Pairs/README.md +++ b/solution/2700-2799/2748.Number of Beautiful Pairs/README.md @@ -72,13 +72,13 @@ i = 0 和 j = 2 :nums[0] 的第一个数字是 1 ,nums[2] 的最后一个数 ### 方法一:计数 -我们可以用一个长度为 $10$ 的数组 $\text{cnt}$ 来记录每个数字的第一个数字出现的次数。 +我们可以用一个长度为 $10$ 的数组 $\textit{cnt}$ 来记录每个数字的第一个数字出现的次数。 -遍历数组 $\text{nums}$,对于每个数字 $x$,我们枚举 $0$ 到 $9$ 的每个数字 $y$,如果 $\text{cnt}[y]$ 不为 $0$ 且 $\text{gcd}(x b\mod 10, y) = 1$,则答案加上 $\text{cnt}[y]$。然后,我们将 $x$ 的第一个数字出现的次数加 $1$。 +遍历数组 $\textit{nums}$,对于每个数字 $x$,我们枚举 $0$ 到 $9$ 的每个数字 $y$,如果 $\textit{cnt}[y]$ 不为 $0$ 且 $\textit{gcd}(x b\mod 10, y) = 1$,则答案加上 $\textit{cnt}[y]$。然后,我们将 $x$ 的第一个数字出现的次数加 $1$。 遍历结束后,返回答案即可。 -时间复杂度 $O(n \times (k + \log M))$,空间复杂度 $O(k + \log M)$。其中 $n$ 为数组 $\text{nums}$ 的长度,而 $k$ 和 $M$ 分别表示数组 $\text{nums}$ 中的数字的种类以及最大值。 +时间复杂度 $O(n \times (k + \log M))$,空间复杂度 $O(k + \log M)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $k$ 和 $M$ 分别表示数组 $\textit{nums}$ 中的数字的种类以及最大值。 diff --git a/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md b/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md index 5b9a2ea903ea6..4a8ee4a47c18b 100644 --- a/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md +++ b/solution/2700-2799/2748.Number of Beautiful Pairs/README_EN.md @@ -71,13 +71,13 @@ Thus, we return 2. ### Solution 1: Counting -We can use an array $\text{cnt}$ of length $10$ to record the count of the first digit of each number. +We can use an array $\textit{cnt}$ of length $10$ to record the count of the first digit of each number. -Iterate through the array $\text{nums}$. For each number $x$, we enumerate each digit $y$ from $0$ to $9$. If $\text{cnt}[y]$ is not $0$ and $\text{gcd}(x \mod 10, y) = 1$, then the answer is incremented by $\text{cnt}[y]$. Then, we increment the count of the first digit of $x$ by $1$. +Iterate through the array $\textit{nums}$. For each number $x$, we enumerate each digit $y$ from $0$ to $9$. If $\textit{cnt}[y]$ is not $0$ and $\textit{gcd}(x \mod 10, y) = 1$, then the answer is incremented by $\textit{cnt}[y]$. Then, we increment the count of the first digit of $x$ by $1$. After the iteration, return the answer. -The time complexity is $O(n \times (k + \log M))$, and the space complexity is $O(k + \log M)$. Here, $n$ is the length of the array $\text{nums}$, while $k$ and $M$ respectively represent the number of distinct numbers and the maximum value in the array $\text{nums}$. +The time complexity is $O(n \times (k + \log M))$, and the space complexity is $O(k + \log M)$. Here, $n$ is the length of the array $\textit{nums}$, while $k$ and $M$ respectively represent the number of distinct numbers and the maximum value in the array $\textit{nums}$. diff --git a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md index 991a8d0fe2faa..7b4bba65a2fea 100644 --- a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md +++ b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md @@ -66,9 +66,9 @@ tags: ### 方法一:枚举 -如果我们操作了 $k$ 次,那么问题实际上就变成了:判断 $\text{num1} - k \times \text{num2}$ 能否拆分成 $k$ 个 $2^i$ 之和。 +如果我们操作了 $k$ 次,那么问题实际上就变成了:判断 $\textit{num1} - k \times \textit{num2}$ 能否拆分成 $k$ 个 $2^i$ 之和。 -我们不妨假设 $x = \text{num1} - k \times \text{num2}$,接下来分类讨论: +我们不妨假设 $x = \textit{num1} - k \times \textit{num2}$,接下来分类讨论: - 如果 $x \lt 0$,那么 $x$ 无法拆分成 $k$ 个 $2^i$ 之和,因为 $2^i \gt 0$,显然无解; - 如果 $x$ 的二进制表示中 $1$ 的个数大于 $k$,此时也是无解; @@ -151,6 +151,23 @@ func makeTheIntegerZero(num1 int, num2 int) int { } ``` +#### TypeScript + +```ts +function makeTheIntegerZero(num1: number, num2: number): number { + for (let k = 1; ; ++k) { + let x = num1 - k * num2; + if (x < 0) { + break; + } + if (x.toString(2).replace(/0/g, '').length <= k && k <= x) { + return k; + } + } + return -1; +} +``` + diff --git a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md index a130bb5ac3137..a017d3e80647a 100644 --- a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md +++ b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md @@ -64,9 +64,9 @@ It can be proven, that 3 is the minimum number of operations that we need to per ### Solution 1: Enumeration -If we operate $k$ times, then the problem essentially becomes: determining whether $\text{num1} - k \times \text{num2}$ can be split into the sum of $k$ $2^i$s. +If we operate $k$ times, then the problem essentially becomes: determining whether $\textit{num1} - k \times \textit{num2}$ can be split into the sum of $k$ $2^i$s. -Let's assume $x = \text{num1} - k \times \text{num2}$. Next, we discuss in categories: +Let's assume $x = \textit{num1} - k \times \textit{num2}$. Next, we discuss in categories: - If $x < 0$, then $x$ cannot be split into the sum of $k$ $2^i$s, because $2^i > 0$, which obviously has no solution; - If the number of $1$s in the binary representation of $x$ is greater than $k$, there is also no solution in this case; @@ -149,6 +149,23 @@ func makeTheIntegerZero(num1 int, num2 int) int { } ``` +#### TypeScript + +```ts +function makeTheIntegerZero(num1: number, num2: number): number { + for (let k = 1; ; ++k) { + let x = num1 - k * num2; + if (x < 0) { + break; + } + if (x.toString(2).replace(/0/g, '').length <= k && k <= x) { + return k; + } + } + return -1; +} +``` + diff --git a/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/Solution.ts b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/Solution.ts new file mode 100644 index 0000000000000..ff5d8db66c334 --- /dev/null +++ b/solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/Solution.ts @@ -0,0 +1,12 @@ +function makeTheIntegerZero(num1: number, num2: number): number { + for (let k = 1; ; ++k) { + let x = num1 - k * num2; + if (x < 0) { + break; + } + if (x.toString(2).replace(/0/g, '').length <= k && k <= x) { + return k; + } + } + return -1; +} diff --git a/solution/2700-2799/2766.Relocate Marbles/README.md b/solution/2700-2799/2766.Relocate Marbles/README.md index 297f0e86fcb5e..046cfce71bb88 100644 --- a/solution/2700-2799/2766.Relocate Marbles/README.md +++ b/solution/2700-2799/2766.Relocate Marbles/README.md @@ -168,9 +168,7 @@ function relocateMarbles(nums: number[], moveFrom: number[], moveTo: number[]): pos.delete(moveFrom[i]); pos.add(moveTo[i]); } - const ans = [...pos]; - ans.sort((a, b) => a - b); - return ans; + return [...pos].sort((a, b) => a - b); } ``` diff --git a/solution/2700-2799/2766.Relocate Marbles/README_EN.md b/solution/2700-2799/2766.Relocate Marbles/README_EN.md index 2d4cddb4f23bd..d7e70c8f2913d 100644 --- a/solution/2700-2799/2766.Relocate Marbles/README_EN.md +++ b/solution/2700-2799/2766.Relocate Marbles/README_EN.md @@ -166,9 +166,7 @@ function relocateMarbles(nums: number[], moveFrom: number[], moveTo: number[]): pos.delete(moveFrom[i]); pos.add(moveTo[i]); } - const ans = [...pos]; - ans.sort((a, b) => a - b); - return ans; + return [...pos].sort((a, b) => a - b); } ``` diff --git a/solution/2700-2799/2766.Relocate Marbles/Solution.ts b/solution/2700-2799/2766.Relocate Marbles/Solution.ts index d70f4163b3fce..8ee7ccf892112 100644 --- a/solution/2700-2799/2766.Relocate Marbles/Solution.ts +++ b/solution/2700-2799/2766.Relocate Marbles/Solution.ts @@ -4,7 +4,5 @@ function relocateMarbles(nums: number[], moveFrom: number[], moveTo: number[]): pos.delete(moveFrom[i]); pos.add(moveTo[i]); } - const ans = [...pos]; - ans.sort((a, b) => a - b); - return ans; + return [...pos].sort((a, b) => a - b); } diff --git a/solution/2800-2899/2865.Beautiful Towers I/README.md b/solution/2800-2899/2865.Beautiful Towers I/README.md index 421a86d5f4635..485a5227c4a58 100644 --- a/solution/2800-2899/2865.Beautiful Towers I/README.md +++ b/solution/2800-2899/2865.Beautiful Towers I/README.md @@ -230,8 +230,8 @@ function maximumSumOfHeights(maxHeights: number[]): number { $$ f[i]= \begin{cases} -f[i-1]+heights[i],&\text{if } heights[i]\geq heights[i-1]\\ -heights[i]\times(i-j)+f[j],&\text{if } heights[i] diff --git a/solution/3000-3099/3052.Maximize Items/README_EN.md b/solution/3000-3099/3052.Maximize Items/README_EN.md index 6e0df2f90172a..0bb755c893d5a 100644 --- a/solution/3000-3099/3052.Maximize Items/README_EN.md +++ b/solution/3000-3099/3052.Maximize Items/README_EN.md @@ -88,7 +88,7 @@ Output table is ordered by item count in descending order. First, we calculate the total area of all items of type `prime_eligible` and record it in the `s` field of table `T`. -Next, we calculate the number of items of type `prime_eligible` and `not_prime` respectively. For items of type `prime_eligible`, the number of portions we can store is $\lfloor \frac{500000}{s} \rfloor$. For items of type `not_prime`, the number of portions we can store is $\lfloor \frac{500000 \mod s}{\sum \text{s1}} \rfloor$. Where $\sum \text{s1}$ is the total area of all items of type `not_prime`. Multiplying by the number of items of type `prime_eligible` and `not_prime` respectively gives us our result. +Next, we calculate the number of items of type `prime_eligible` and `not_prime` respectively. For items of type `prime_eligible`, the number of portions we can store is $\lfloor \frac{500000}{s} \rfloor$. For items of type `not_prime`, the number of portions we can store is $\lfloor \frac{500000 \mod s}{\sum \textit{s1}} \rfloor$. Where $\sum \textit{s1}$ is the total area of all items of type `not_prime`. Multiplying by the number of items of type `prime_eligible` and `not_prime` respectively gives us our result. diff --git a/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README.md b/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README.md index 6d9c6b2a4207c..bf4c1bbf14044 100644 --- a/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README.md +++ b/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README.md @@ -94,10 +94,10 @@ tags: - 首先,如果位置 $i$ 的数字为 $1$,我们可以直接拾取一个 $1$,不需要行动次数。 - 然后,我们对 $i$ 的左右两侧位置的数字 $1$ 进行拾取,执行的是行动 $2$,即把位置 $i-1$ 的 $1$ 移到位置 $i$,然后拾取;把位置 $i+1$ 的 $1$ 移到位置 $i$,然后拾取。每拾取一个 $1$,需要 $1$ 次行动。 -- 接下来,我们最大限度地将 $i-1$ 或 $i+1$ 上的 $0$,利用行动 $1$,将其置为 $1$,然后利用行动 $2$,将其移动到位置 $i$,拾取。直到拾取的 $1$ 的数量达到 $k$ 或者行动 $1$ 的次数达到 $\text{maxChanges}$。我们假设行动 $1$ 的次数为 $c$,那么总共需要 $2c$ 次行动。 +- 接下来,我们最大限度地将 $i-1$ 或 $i+1$ 上的 $0$,利用行动 $1$,将其置为 $1$,然后利用行动 $2$,将其移动到位置 $i$,拾取。直到拾取的 $1$ 的数量达到 $k$ 或者行动 $1$ 的次数达到 $\textit{maxChanges}$。我们假设行动 $1$ 的次数为 $c$,那么总共需要 $2c$ 次行动。 - 利用完行动 $1$,如果拾取的 $1$ 的数量还没有达到 $k$,我们需要继续考虑在 $[1,..i-2]$ 和 $[i+2,..n]$ 的区间内,进行行动 $2$,将 $1$ 移动到位置 $i$,拾取。我们可以使用二分查找来确定这个区间的大小,使得拾取的 $1$ 的数量达到 $k$。具体地,我们二分枚举一个区间的大小 $d$,然后在区间 $[i-d,..i-2]$ 和 $[i+2,..i+d]$ 内,进行行动 $2$,将 $1$ 移动到位置 $i$,拾取。如果拾取的 $1$ 的数量达到 $k$,我们就更新答案。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 diff --git a/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README_EN.md b/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README_EN.md index 6ce12dd2b913d..98d8d0d29c85c 100644 --- a/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README_EN.md +++ b/solution/3000-3099/3086.Minimum Moves to Pick K Ones/README_EN.md @@ -92,10 +92,10 @@ We consider enumerating Alice's standing position $i$. For each $i$, we follow t - First, if the number at position $i$ is $1$, we can directly pick up a $1$ without needing any moves. - Then, we pick up the number $1$ from both sides of position $i$, which is action $2$, i.e., move the $1$ from position $i-1$ to position $i$, then pick it up; move the $1$ from position $i+1$ to position $i$, then pick it up. Each pick up of a $1$ requires $1$ move. -- Next, we maximize the conversion of $0$s at positions $i-1$ or $i+1$ to $1$s using action $1$, then move them to position $i$ using action $2$ to pick them up. This continues until the number of $1$s picked up reaches $k$ or the number of times action $1$ is used reaches $\text{maxChanges}$. Assuming the number of times action $1$ is used is $c$, then a total of $2c$ moves are needed. +- Next, we maximize the conversion of $0$s at positions $i-1$ or $i+1$ to $1$s using action $1$, then move them to position $i$ using action $2$ to pick them up. This continues until the number of $1$s picked up reaches $k$ or the number of times action $1$ is used reaches $\textit{maxChanges}$. Assuming the number of times action $1$ is used is $c$, then a total of $2c$ moves are needed. - After utilizing action $1$, if the number of $1$s picked up has not reached $k$, we need to continue considering moving $1$s to position $i$ from the intervals $[1,..i-2]$ and $[i+2,..n]$ using action $2$ to pick them up. We can use binary search to determine the size of this interval so that the number of $1$s picked up reaches $k$. Specifically, we binary search for an interval size $d$, then within the intervals $[i-d,..i-2]$ and $[i+2,..i+d]$, we perform action $2$ to move $1$s to position $i$ for pickup. If the number of $1$s picked up reaches $k$, we update the answer. -The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\text{nums}$. +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md index e585448d802ce..3ca9f25bb9fd5 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md @@ -93,7 +93,7 @@ tags: - 如果 $i \geq n$,表示已经处理完了所有的元素,如果 $k = 0$,返回 $mi$,否则返回 $0$; - 如果剩余的元素个数 $n - i$ 不足 $k$ 个,返回 $0$; - 否则,我们可以选择不选取第 $i$ 个元素,可以获得的能量和为 $dfs(i + 1, j, k, mi)$; -- 也可以选择选取第 $i$ 个元素。如果 $j = n$,表示之前没有选取过元素,那么可以获得的能量和为 $dfs(i + 1, i, k - 1, mi)$;否则,可以获得的能量和为 $dfs(i + 1, i, k - 1, \min(mi, \text{nums}[i] - \text{nums}[j]))$。 +- 也可以选择选取第 $i$ 个元素。如果 $j = n$,表示之前没有选取过元素,那么可以获得的能量和为 $dfs(i + 1, i, k - 1, mi)$;否则,可以获得的能量和为 $dfs(i + 1, i, k - 1, \min(mi, \textit{nums}[i] - \textit{nums}[j]))$。 - 我们累加上述结果,并对 $10^9 + 7$ 取模后返回。 为了避免重复计算,我们可以使用记忆化搜索的方法,将已经计算过的结果保存起来。 diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md index 0196c07020c34..db46b8a10d376 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md @@ -91,7 +91,7 @@ The execution process of the function $dfs(i, j, k, mi)$ is as follows: - If $i \geq n$, it means all elements have been processed. If $k = 0$, return $mi$; otherwise, return $0$. - If the remaining number of elements $n - i$ is less than $k$, return $0$. - Otherwise, we can choose not to select the $i$-th element, and the energy sum obtained is $dfs(i + 1, j, k, mi)$. -- We can also choose to select the $i$-th element. If $j = n$, it means no element has been selected before, then the energy sum obtained is $dfs(i + 1, i, k - 1, mi)$; otherwise, the energy sum obtained is $dfs(i + 1, i, k - 1, \min(mi, \text{nums}[i] - \text{nums}[j]))$. +- We can also choose to select the $i$-th element. If $j = n$, it means no element has been selected before, then the energy sum obtained is $dfs(i + 1, i, k - 1, mi)$; otherwise, the energy sum obtained is $dfs(i + 1, i, k - 1, \min(mi, \textit{nums}[i] - \textit{nums}[j]))$. - We add up the above results and return the result modulo $10^9 + 7$. To avoid repeated calculations, we can use memoization, saving the results that have already been calculated. diff --git a/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README.md b/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README.md index 290a62ef8c631..84108cae86498 100644 --- a/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README.md +++ b/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README.md @@ -82,7 +82,7 @@ tags: 由于我们要尽可能多地修补坑洼,而对于长度为 $k$ 的连续坑洼,我们需要花费 $k + 1$ 的代价,应该优先修补长度较长的坑洼,这样可以使得代价最小。 -因此,我们从最长的坑洼开始修补,对于长度为 $k$ 的坑洼,我们最多可以修补的个数为 $t = \min(\text{budget} / (k + 1), \text{cnt}[k])$,我们将修补的个数乘以长度 $k$ 加到答案中,然后更新剩余的预算。对于长度为 $k$ 的其余 $cnt[k] - t$ 个坑洼,我们将它们合并到长度为 $k - 1$ 的坑洼中。继续这个过程,直到遍历完所有的坑洼。 +因此,我们从最长的坑洼开始修补,对于长度为 $k$ 的坑洼,我们最多可以修补的个数为 $t = \min(\textit{budget} / (k + 1), \textit{cnt}[k])$,我们将修补的个数乘以长度 $k$ 加到答案中,然后更新剩余的预算。对于长度为 $k$ 的其余 $cnt[k] - t$ 个坑洼,我们将它们合并到长度为 $k - 1$ 的坑洼中。继续这个过程,直到遍历完所有的坑洼。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $road$ 的长度。 diff --git a/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README_EN.md b/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README_EN.md index 8b937a8299651..0ae2d4782d423 100644 --- a/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README_EN.md +++ b/solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README_EN.md @@ -82,7 +82,7 @@ First, we count the number of each continuous pothole, recorded in the array $cn Since we want to repair as many potholes as possible, and for a continuous pothole of length $k$, we need to spend a cost of $k + 1$, we should prioritize repairing longer potholes to minimize the cost. -Therefore, we start repairing from the longest pothole. For a pothole of length $k$, the maximum number we can repair is $t = \min(\text{budget} / (k + 1), \text{cnt}[k])$. We add the number of repairs multiplied by the length $k$ to the answer, then update the remaining budget. For the remaining $cnt[k] - t$ potholes of length $k$, we merge them into the potholes of length $k - 1$. Continue this process until all potholes are traversed. +Therefore, we start repairing from the longest pothole. For a pothole of length $k$, the maximum number we can repair is $t = \min(\textit{budget} / (k + 1), \textit{cnt}[k])$. We add the number of repairs multiplied by the length $k$ to the answer, then update the remaining budget. For the remaining $cnt[k] - t$ potholes of length $k$, we merge them into the potholes of length $k - 1$. Continue this process until all potholes are traversed. The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $road$. diff --git a/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README.md b/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README.md index f7451e714ecfe..e1527b9e6a037 100644 --- a/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README.md +++ b/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README.md @@ -101,10 +101,10 @@ tags: 我们定义状态 $f[i][j]$ 表示前 $[0,..i]$ 列数字,且第 $i$ 列数字为 $j$ 的最小操作次数。那么我们可以得到状态转移方程: $$ -f[i][j] = \min_{k \neq j} f[i-1][k] + m - \text{cnt}[j] +f[i][j] = \min_{k \neq j} f[i-1][k] + m - \textit{cnt}[j] $$ -其中 $\text{cnt}[j]$ 表示第 $i$ 列数字为 $j$ 的个数。 +其中 $\textit{cnt}[j]$ 表示第 $i$ 列数字为 $j$ 的个数。 最后我们只需要求出 $f[n-1][j]$ 的最小值即可。 diff --git a/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README_EN.md b/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README_EN.md index 08af516eed734..35f57b8c6f2af 100644 --- a/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README_EN.md +++ b/solution/3100-3199/3122.Minimum Number of Operations to Satisfy Conditions/README_EN.md @@ -99,10 +99,10 @@ We notice that the values in the cells of the matrix only have 10 possibilities. We define the state $f[i][j]$ to represent the minimum number of operations for the numbers in the first $[0,..i]$ columns, and the number in the $i$-th column is $j$. Then we can get the state transition equation: $$ -f[i][j] = \min_{k \neq j} (f[i-1][k] + m - \text{cnt}[j]) +f[i][j] = \min_{k \neq j} (f[i-1][k] + m - \textit{cnt}[j]) $$ -Where $\text{cnt}[j]$ represents the number of cells in the $i$-th column that are $j$. +Where $\textit{cnt}[j]$ represents the number of cells in the $i$-th column that are $j$. Finally, we only need to find the minimum value of $f[n-1][j]$. diff --git a/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README.md b/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README.md index 8653f4aad4bde..42c153dae461f 100644 --- a/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README.md +++ b/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README.md @@ -80,7 +80,7 @@ tags: ### 方法一:位运算 -我们可以找到 $n$ 的二进制表示中最高位的 $1$,那么最大的 $x$ 一定小于 $n$ 且该位为 $0$,其他低位均为 $1$,即 $x = 2^{\text{最高位的位数} - 1} - 1$。这是因为 $x \text{ and } (x + 1) = 0$ 一定成立。 +我们可以找到 $n$ 的二进制表示中最高位的 $1$,那么最大的 $x$ 一定小于 $n$ 且该位为 $0$,其他低位均为 $1$,即 $x = 2^{\textit{最高位的位数} - 1} - 1$。这是因为 $x \textit{ and } (x + 1) = 0$ 一定成立。 时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。 diff --git a/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README_EN.md b/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README_EN.md index e211a32d8909a..0c40c9ba9b4aa 100644 --- a/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README_EN.md +++ b/solution/3100-3199/3125.Maximum Number That Makes Result of Bitwise AND Zero/README_EN.md @@ -75,7 +75,7 @@ The bitwise AND of [3, 4, 5, 6, 7] is 0.

### Solution 1: Bit Manipulation -We can find the highest bit of $1$ in the binary representation of $n$. The maximum $x$ must be less than $n$ and this bit is $0$, and all other lower bits are $1$, i.e., $x = 2^{\text{number of the highest bit}} - 1$. This is because $x \text{ and } (x + 1) = 0$ must hold. +We can find the highest bit of $1$ in the binary representation of $n$. The maximum $x$ must be less than $n$ and this bit is $0$, and all other lower bits are $1$, i.e., $x = 2^{\textit{number of the highest bit}} - 1$. This is because $x \textit{ and } (x + 1) = 0$ must hold. The time complexity is $O(\log n)$, and the space complexity is $O(1)$. diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md index 8d975b6c1078b..cf541f2d459f9 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README.md @@ -94,10 +94,10 @@ tags: 函数 $dfs(i, j, k)$ 的计算过程如下: - 如果 $i \lt 0$ 或 $j \lt 0$,返回 $0$。 -- 如果 $i = 0$,那么当 $k = 1$ 且 $j \leq \text{limit}$ 时返回 $1$,否则返回 $0$。 -- 如果 $j = 0$,那么当 $k = 0$ 且 $i \leq \text{limit}$ 时返回 $1$,否则返回 $0$。 -- 如果 $k = 0$,我们考虑前一个数字是 $0$ 的情况 $dfs(i - 1, j, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i - 1, j, 1)$,如果前一个数是 $0$,那么有可能使得子数组中有超过 $\text{limit}$ 个 $0$,即不允许出现倒数第 $\text{limit} + 1$ 个数是 $1$ 的情况,所以我们要减去这种情况,即 $dfs(i - \text{limit} - 1, j, 1)$。 -- 如果 $k = 1$,我们考虑前一个数字是 $0$ 的情况 $dfs(i, j - 1, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i, j - 1, 1)$,如果前一个数是 $1$,那么有可能使得子数组中有超过 $\text{limit}$ 个 $1$,即不允许出现倒数第 $\text{limit} + 1$ 个数是 $0$ 的情况,所以我们要减去这种情况,即 $dfs(i, j - \text{limit} - 1, 0)$。 +- 如果 $i = 0$,那么当 $k = 1$ 且 $j \leq \textit{limit}$ 时返回 $1$,否则返回 $0$。 +- 如果 $j = 0$,那么当 $k = 0$ 且 $i \leq \textit{limit}$ 时返回 $1$,否则返回 $0$。 +- 如果 $k = 0$,我们考虑前一个数字是 $0$ 的情况 $dfs(i - 1, j, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i - 1, j, 1)$,如果前一个数是 $0$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $0$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $1$ 的情况,所以我们要减去这种情况,即 $dfs(i - \textit{limit} - 1, j, 1)$。 +- 如果 $k = 1$,我们考虑前一个数字是 $0$ 的情况 $dfs(i, j - 1, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i, j - 1, 1)$,如果前一个数是 $1$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $1$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $0$ 的情况,所以我们要减去这种情况,即 $dfs(i, j - \textit{limit} - 1, 0)$。 为了避免重复计算,我们使用记忆化搜索的方法。 @@ -271,12 +271,12 @@ func numberOfStableArrays(zero int, one int, limit int) int { 我们定义 $f[i][j][k]$ 表示使用 $i$ 个 $0$ 和 $j$ 个 $1$ 且最后一个数字是 $k$ 的稳定二进制数组的个数。那么答案就是 $f[zero][one][0] + f[zero][one][1]$。 -初始时,我们有 $f[i][0][0] = 1$,其中 $1 \leq i \leq \min(\text{limit}, \text{zero})$;有 $f[0][j][1] = 1$,其中 $1 \leq j \leq \min(\text{limit}, \text{one})$。 +初始时,我们有 $f[i][0][0] = 1$,其中 $1 \leq i \leq \min(\textit{limit}, \textit{zero})$;有 $f[0][j][1] = 1$,其中 $1 \leq j \leq \min(\textit{limit}, \textit{one})$。 状态转移方程如下: -- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \text{limit} - 1][j][1]$。 -- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \text{limit} - 1][0]$。 +- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \textit{limit} - 1][j][1]$。 +- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \textit{limit} - 1][0]$。 时间复杂度 $O(zero \times one)$,空间复杂度 $O(zero \times one)$。 diff --git a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md index 35924ccabd7d6..68d2a07b0947e 100644 --- a/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md +++ b/solution/3100-3199/3129.Find All Possible Stable Binary Arrays I/README_EN.md @@ -92,9 +92,9 @@ We design a function $dfs(i, j, k)$ to represent the number of stable binary arr The calculation process of the function $dfs(i, j, k)$ is as follows: - If $i < 0$ or $j < 0$, return $0$. -- If $i = 0$, return $1$ when $k = 1$ and $j \leq \text{limit}$, otherwise return $0$. -- If $j = 0$, return $1$ when $k = 0$ and $i \leq \text{limit}$, otherwise return $0$. -- If $k = 0$, we consider the case where the previous number is $0$, $dfs(i - 1, j, 0)$, and the case where the previous number is $1$, $dfs(i - 1, j, 1)$. If the previous number is $0$, it may cause more than $\text{limit}$ $0$s in the subarray, i.e., the situation where the $\text{limit} + 1$ +- If $i = 0$, return $1$ when $k = 1$ and $j \leq \textit{limit}$, otherwise return $0$. +- If $j = 0$, return $1$ when $k = 0$ and $i \leq \textit{limit}$, otherwise return $0$. +- If $k = 0$, we consider the case where the previous number is $0$, $dfs(i - 1, j, 0)$, and the case where the previous number is $1$, $dfs(i - 1, j, 1)$. If the previous number is $0$, it may cause more than $\textit{limit}$ $0$s in the subarray, i.e., the situation where the $\textit{limit} + 1$ @@ -262,12 +262,12 @@ We can also convert the memoization search of Solution 1 into dynamic programmin We define $f[i][j][k]$ as the number of stable binary arrays using $i$ $0$s and $j$ $1$s, and the last number is $k$. So the answer is $f[zero][one][0] + f[zero][one][1]$. -Initially, we have $f[i][0][0] = 1$, where $1 \leq i \leq \min(\text{limit}, \text{zero})$; and $f[0][j][1] = 1$, where $1 \leq j \leq \min(\text{limit}, \text{one})$. +Initially, we have $f[i][0][0] = 1$, where $1 \leq i \leq \min(\textit{limit}, \textit{zero})$; and $f[0][j][1] = 1$, where $1 \leq j \leq \min(\textit{limit}, \textit{one})$. The state transition equation is as follows: -- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \text{limit} - 1][j][1]$. -- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \text{limit} - 1][0]$. +- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \textit{limit} - 1][j][1]$. +- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \textit{limit} - 1][0]$. The time complexity is $O(zero \times one)$, and the space complexity is $O(zero \times one)$. diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md index 992561be84377..ed884ea3380c1 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README.md @@ -94,10 +94,10 @@ tags: 函数 $dfs(i, j, k)$ 的计算过程如下: - 如果 $i \lt 0$ 或 $j \lt 0$,返回 $0$。 -- 如果 $i = 0$,那么当 $k = 1$ 且 $j \leq \text{limit}$ 时返回 $1$,否则返回 $0$。 -- 如果 $j = 0$,那么当 $k = 0$ 且 $i \leq \text{limit}$ 时返回 $1$,否则返回 $0$。 -- 如果 $k = 0$,我们考虑前一个数字是 $0$ 的情况 $dfs(i - 1, j, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i - 1, j, 1)$,如果前一个数是 $0$,那么有可能使得子数组中有超过 $\text{limit}$ 个 $0$,即不允许出现倒数第 $\text{limit} + 1$ 个数是 $1$ 的情况,所以我们要减去这种情况,即 $dfs(i - \text{limit} - 1, j, 1)$。 -- 如果 $k = 1$,我们考虑前一个数字是 $0$ 的情况 $dfs(i, j - 1, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i, j - 1, 1)$,如果前一个数是 $1$,那么有可能使得子数组中有超过 $\text{limit}$ 个 $1$,即不允许出现倒数第 $\text{limit} + 1$ 个数是 $0$ 的情况,所以我们要减去这种情况,即 $dfs(i, j - \text{limit} - 1, 0)$。 +- 如果 $i = 0$,那么当 $k = 1$ 且 $j \leq \textit{limit}$ 时返回 $1$,否则返回 $0$。 +- 如果 $j = 0$,那么当 $k = 0$ 且 $i \leq \textit{limit}$ 时返回 $1$,否则返回 $0$。 +- 如果 $k = 0$,我们考虑前一个数字是 $0$ 的情况 $dfs(i - 1, j, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i - 1, j, 1)$,如果前一个数是 $0$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $0$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $1$ 的情况,所以我们要减去这种情况,即 $dfs(i - \textit{limit} - 1, j, 1)$。 +- 如果 $k = 1$,我们考虑前一个数字是 $0$ 的情况 $dfs(i, j - 1, 0)$ 和前一个数字是 $1$ 的情况 $dfs(i, j - 1, 1)$,如果前一个数是 $1$,那么有可能使得子数组中有超过 $\textit{limit}$ 个 $1$,即不允许出现倒数第 $\textit{limit} + 1$ 个数是 $0$ 的情况,所以我们要减去这种情况,即 $dfs(i, j - \textit{limit} - 1, 0)$。 为了避免重复计算,我们使用记忆化搜索的方法。 @@ -271,12 +271,12 @@ func numberOfStableArrays(zero int, one int, limit int) int { 我们定义 $f[i][j][k]$ 表示使用 $i$ 个 $0$ 和 $j$ 个 $1$ 且最后一个数字是 $k$ 的稳定二进制数组的个数。那么答案就是 $f[zero][one][0] + f[zero][one][1]$。 -初始时,我们有 $f[i][0][0] = 1$,其中 $1 \leq i \leq \min(\text{limit}, \text{zero})$;有 $f[0][j][1] = 1$,其中 $1 \leq j \leq \min(\text{limit}, \text{one})$。 +初始时,我们有 $f[i][0][0] = 1$,其中 $1 \leq i \leq \min(\textit{limit}, \textit{zero})$;有 $f[0][j][1] = 1$,其中 $1 \leq j \leq \min(\textit{limit}, \textit{one})$。 状态转移方程如下: -- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \text{limit} - 1][j][1]$。 -- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \text{limit} - 1][0]$。 +- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \textit{limit} - 1][j][1]$。 +- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \textit{limit} - 1][0]$。 时间复杂度 $O(zero \times one)$,空间复杂度 $O(zero \times one)$。 diff --git a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md index cfc043bbd5b40..7ee1772ce1bfd 100644 --- a/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md +++ b/solution/3100-3199/3130.Find All Possible Stable Binary Arrays II/README_EN.md @@ -90,9 +90,9 @@ We design a function $dfs(i, j, k)$ to represent the number of stable binary arr The calculation process of the function $dfs(i, j, k)$ is as follows: - If $i < 0$ or $j < 0$, return $0$. -- If $i = 0$, return $1$ when $k = 1$ and $j \leq \text{limit}$, otherwise return $0$. -- If $j = 0$, return $1$ when $k = 0$ and $i \leq \text{limit}$, otherwise return $0$. -- If $k = 0$, we consider the case where the previous number is $0$, $dfs(i - 1, j, 0)$, and the case where the previous number is $1$, $dfs(i - 1, j, 1)$. If the previous number is $0$, it may cause more than $\text{limit}$ $0$s in the subarray, i.e., the situation where the $\text{limit} + 1$ +- If $i = 0$, return $1$ when $k = 1$ and $j \leq \textit{limit}$, otherwise return $0$. +- If $j = 0$, return $1$ when $k = 0$ and $i \leq \textit{limit}$, otherwise return $0$. +- If $k = 0$, we consider the case where the previous number is $0$, $dfs(i - 1, j, 0)$, and the case where the previous number is $1$, $dfs(i - 1, j, 1)$. If the previous number is $0$, it may cause more than $\textit{limit}$ $0$s in the subarray, i.e., the situation where the $\textit{limit} + 1$ @@ -262,12 +262,12 @@ We can also convert the memoization search of Solution 1 into dynamic programmin We define $f[i][j][k]$ as the number of stable binary arrays using $i$ $0$s and $j$ $1$s, and the last number is $k$. So the answer is $f[zero][one][0] + f[zero][one][1]$. -Initially, we have $f[i][0][0] = 1$, where $1 \leq i \leq \min(\text{limit}, \text{zero})$; and $f[0][j][1] = 1$, where $1 \leq j \leq \min(\text{limit}, \text{one})$. +Initially, we have $f[i][0][0] = 1$, where $1 \leq i \leq \min(\textit{limit}, \textit{zero})$; and $f[0][j][1] = 1$, where $1 \leq j \leq \min(\textit{limit}, \textit{one})$. The state transition equation is as follows: -- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \text{limit} - 1][j][1]$. -- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \text{limit} - 1][0]$. +- $f[i][j][0] = f[i - 1][j][0] + f[i - 1][j][1] - f[i - \textit{limit} - 1][j][1]$. +- $f[i][j][1] = f[i][j - 1][0] + f[i][j - 1][1] - f[i][j - \textit{limit} - 1][0]$. The time complexity is $O(zero \times one)$, and the space complexity is $O(zero \times one)$. diff --git a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md index 5922952b0abac..795b7f5f05de3 100644 --- a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md +++ b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README.md @@ -120,8 +120,8 @@ tags: $$ f[i][j] = \begin{cases} -f[i - 1][j - 1] + 1, & \text{if } \text{initial}[i - 1] = \text{target}[j - 1], \\ -0, & \text{otherwise}. +f[i - 1][j - 1] + 1, & \textit{if } \textit{initial}[i - 1] = \textit{target}[j - 1], \\ +0, & \textit{otherwise}. \end{cases} $$ diff --git a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md index 511240f056ad1..d3d972bd89df4 100644 --- a/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md +++ b/solution/3100-3199/3135.Equalize Strings by Adding or Removing Characters at Ends/README_EN.md @@ -118,8 +118,8 @@ We can use dynamic programming to find the length $mx$ of the longest common sub $$ f[i][j] = \begin{cases} -f[i - 1][j - 1] + 1, & \text{if } \text{initial}[i - 1] = \text{target}[j - 1], \\ -0, & \text{otherwise}. +f[i - 1][j - 1] + 1, & \textit{if } \textit{initial}[i - 1] = \textit{target}[j - 1], \\ +0, & \textit{otherwise}. \end{cases} $$ diff --git a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md index b2dc2f96bd8ce..9293983451278 100644 --- a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md +++ b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md @@ -69,13 +69,13 @@ tags: ### 方法一:枚举 -根据题目描述,字符串 $\text{t}$ 的长度一定是 $\text{s}$ 的长度的因子,我们可以从小到大枚举字符串 $\text{t}$ 的长度 $k$,然后检查是否满足题目要求,如果满足则返回。因此,问题转化为如何检查字符串 $\text{t}$ 的长度 $k$ 是否满足题目要求。 +根据题目描述,字符串 $\textit{t}$ 的长度一定是 $\textit{s}$ 的长度的因子,我们可以从小到大枚举字符串 $\textit{t}$ 的长度 $k$,然后检查是否满足题目要求,如果满足则返回。因此,问题转化为如何检查字符串 $\textit{t}$ 的长度 $k$ 是否满足题目要求。 -我们首先统计字符串 $\text{s}$ 中每个字符出现的次数,记录在数组或哈希表 $\text{cnt}$ 中。 +我们首先统计字符串 $\textit{s}$ 中每个字符出现的次数,记录在数组或哈希表 $\textit{cnt}$ 中。 -然后,我们定义一个函数 $\text{check}(k)$,用来检查字符串 $\text{t}$ 的长度 $k$ 是否满足题目要求。我们可以遍历字符串 $\text{s}$,每次取长度为 $k$ 的子串,然后统计每个字符出现的次数,如果每个字符出现的次数乘以 $\frac{n}{k}$ 不等于 $\text{cnt}$ 中的值,则返回 $\text{false}$。遍历结束,如果都满足,则返回 $\text{true}$。 +然后,我们定义一个函数 $\textit{check}(k)$,用来检查字符串 $\textit{t}$ 的长度 $k$ 是否满足题目要求。我们可以遍历字符串 $\textit{s}$,每次取长度为 $k$ 的子串,然后统计每个字符出现的次数,如果每个字符出现的次数乘以 $\frac{n}{k}$ 不等于 $\textit{cnt}$ 中的值,则返回 $\textit{false}$。遍历结束,如果都满足,则返回 $\textit{true}$。 -时间复杂度 $O(n \times A)$,其中 $n$ 是字符串 $\text{s}$ 的长度,而 $A$ 是 $n$ 的因子个数。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,本题中为小写字母集合。 +时间复杂度 $O(n \times A)$,其中 $n$ 是字符串 $\textit{s}$ 的长度,而 $A$ 是 $n$ 的因子个数。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,本题中为小写字母集合。 diff --git a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md index 33c9ae0748657..e602923c74d70 100644 --- a/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md +++ b/solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md @@ -67,13 +67,13 @@ tags: ### Solution 1: Enumeration -Based on the problem description, the length of string $\text{t}$ must be a factor of the length of string $\text{s}$. We can enumerate the length $k$ of string $\text{t}$ from small to large, and then check if it meets the requirements of the problem. If it does, we return. Thus, the problem is transformed into how to check whether the length $k$ of string $\text{t}$ meets the requirements. +Based on the problem description, the length of string $\textit{t}$ must be a factor of the length of string $\textit{s}$. We can enumerate the length $k$ of string $\textit{t}$ from small to large, and then check if it meets the requirements of the problem. If it does, we return. Thus, the problem is transformed into how to check whether the length $k$ of string $\textit{t}$ meets the requirements. -First, we count the occurrence of each character in string $\text{s}$ and record it in an array or hash table $\text{cnt}$. +First, we count the occurrence of each character in string $\textit{s}$ and record it in an array or hash table $\textit{cnt}$. -Next, we define a function $\text{check}(k)$ to check whether the length $k$ of string $\text{t}$ meets the requirements. We can traverse string $\text{s}$, taking a substring of length $k$ each time, and then count the occurrence of each character. If the occurrence of each character multiplied by $\frac{n}{k}$ does not equal the value in $\text{cnt}$, then return $\text{false}$. If all checks pass by the end of the traversal, return $\text{true}$. +Next, we define a function $\textit{check}(k)$ to check whether the length $k$ of string $\textit{t}$ meets the requirements. We can traverse string $\textit{s}$, taking a substring of length $k$ each time, and then count the occurrence of each character. If the occurrence of each character multiplied by $\frac{n}{k}$ does not equal the value in $\textit{cnt}$, then return $\textit{false}$. If all checks pass by the end of the traversal, return $\textit{true}$. -The time complexity is $O(n \times A)$, where $n$ is the length of string $\text{s}$, and $A$ is the number of factors of $n$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set, which in this case is the set of lowercase letters. +The time complexity is $O(n \times A)$, where $n$ is the length of string $\textit{s}$, and $A$ is the number of factors of $n$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set, which in this case is the set of lowercase letters. diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/README.md b/solution/3100-3199/3141.Maximum Hamming Distances/README.md index b36eda6f9c07d..957087931a478 100644 --- a/solution/3100-3199/3141.Maximum Hamming Distances/README.md +++ b/solution/3100-3199/3141.Maximum Hamming Distances/README.md @@ -90,12 +90,12 @@ tags: 具体步骤如下: -1. 初始化一个数组 $\text{dist}$,数组长度为 $2^m$,用来记录每个取反后的元素到其他元素的最小海明距离,初始时全部置为 $-1$。 -2. 遍历数组 $\text{nums}$,将每个元素的取反值置为 $0$,并将其加入队列 $\text{q}$。 -3. 从 $k = 1$ 开始,不断遍历队列 $\text{q}$,每次遍历时,将队列中的元素取出,然后对其进行 $m$ 次取反操作,将取反后的元素加入队列 $\text{t}$,并将其到原元素的最小海明距离置为 $k$。 +1. 初始化一个数组 $\textit{dist}$,数组长度为 $2^m$,用来记录每个取反后的元素到其他元素的最小海明距离,初始时全部置为 $-1$。 +2. 遍历数组 $\textit{nums}$,将每个元素的取反值置为 $0$,并将其加入队列 $\textit{q}$。 +3. 从 $k = 1$ 开始,不断遍历队列 $\textit{q}$,每次遍历时,将队列中的元素取出,然后对其进行 $m$ 次取反操作,将取反后的元素加入队列 $\textit{t}$,并将其到原元素的最小海明距离置为 $k$。 4. 重复步骤 3,直到队列为空。 -最后,我们遍历数组 $\text{nums}$,将每个元素取反后的值作为下标,从 $\text{dist}$ 数组中取出对应的最小海明距离,然后用 $m$ 减去这个值,就是我们要求的最大海明距离。 +最后,我们遍历数组 $\textit{nums}$,将每个元素取反后的值作为下标,从 $\textit{dist}$ 数组中取出对应的最小海明距离,然后用 $m$ 减去这个值,就是我们要求的最大海明距离。 时间复杂度 $O(2^m)$,空间复杂度 $O(2^m)$。其中 $m$ 为题目给定的整数。 diff --git a/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md b/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md index 4978dba7eb1c8..172be39cb2aa8 100644 --- a/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md +++ b/solution/3100-3199/3141.Maximum Hamming Distances/README_EN.md @@ -88,12 +88,12 @@ We can use Breadth-First Search (BFS) to find the minimum Hamming distance from The specific steps are as follows: -1. Initialize an array $\text{dist}$ with a length of $2^m$ to record the minimum Hamming distance from each complemented element to other elements. Initially, all are set to $-1$. -2. Traverse the array $\text{nums}$, set the complement of each element to $0$, and add it to the queue $\text{q}$. -3. Starting from $k = 1$, continuously traverse the queue $\text{q}$. Each time, take out the elements in the queue, perform $m$ complement operations on them, add the complemented elements to the queue $\text{t}$, and set the minimum Hamming distance to the original element to $k$. +1. Initialize an array $\textit{dist}$ with a length of $2^m$ to record the minimum Hamming distance from each complemented element to other elements. Initially, all are set to $-1$. +2. Traverse the array $\textit{nums}$, set the complement of each element to $0$, and add it to the queue $\textit{q}$. +3. Starting from $k = 1$, continuously traverse the queue $\textit{q}$. Each time, take out the elements in the queue, perform $m$ complement operations on them, add the complemented elements to the queue $\textit{t}$, and set the minimum Hamming distance to the original element to $k$. 4. Repeat step 3 until the queue is empty. -Finally, we traverse the array $\text{nums}$, take the complement of each element as the index, and take out the corresponding minimum Hamming distance from the $\text{dist}$ array. Then, $m$ minus this value is the maximum Hamming distance we are looking for. +Finally, we traverse the array $\textit{nums}$, take the complement of each element as the index, and take out the corresponding minimum Hamming distance from the $\textit{dist}$ array. Then, $m$ minus this value is the maximum Hamming distance we are looking for. The time complexity is $O(2^m)$, and the space complexity is $O(2^m)$, where $m$ is the integer given in the problem. diff --git a/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README.md b/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README.md index a668078a18556..ad875a1f26e7c 100644 --- a/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README.md +++ b/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README.md @@ -70,15 +70,15 @@ tags: ### 方法一:记忆化搜索 + 哈希表 -我们设计一个函数 $\text{dfs}(i)$,表示从字符串 $s[i]$ 开始分割的最少子字符串数量。那么答案就是 $\text{dfs}(0)$。 +我们设计一个函数 $\textit{dfs}(i)$,表示从字符串 $s[i]$ 开始分割的最少子字符串数量。那么答案就是 $\textit{dfs}(0)$。 -函数 $\text{dfs}(i)$ 的计算过程如下: +函数 $\textit{dfs}(i)$ 的计算过程如下: 如果 $i \geq n$,表示已经处理完了所有字符,返回 $0$。 -否则,我们维护一个哈希表 $\text{cnt}$,表示当前子字符串中每个字符出现的次数。另外,我们还维护一个哈希表 $\text{freq}$,表示每个字符出现的次数的频率。 +否则,我们维护一个哈希表 $\textit{cnt}$,表示当前子字符串中每个字符出现的次数。另外,我们还维护一个哈希表 $\textit{freq}$,表示每个字符出现的次数的频率。 -然后我们枚举 $j$ 从 $i$ 到 $n-1$,表示当前子字符串的结束位置。对于每个 $j$,我们更新 $\text{cnt}$ 和 $\text{freq}$,然后判断 $\text{freq}$ 的大小是否为 $1$,如果是的话,我们可以从 $j+1$ 开始分割,此时答案为 $1 + \text{dfs}(j+1)$,我们取所有 $j$ 中答案的最小值作为函数的返回值。 +然后我们枚举 $j$ 从 $i$ 到 $n-1$,表示当前子字符串的结束位置。对于每个 $j$,我们更新 $\textit{cnt}$ 和 $\textit{freq}$,然后判断 $\textit{freq}$ 的大小是否为 $1$,如果是的话,我们可以从 $j+1$ 开始分割,此时答案为 $1 + \textit{dfs}(j+1)$,我们取所有 $j$ 中答案的最小值作为函数的返回值。 为了避免重复计算,我们使用记忆化搜索。 diff --git a/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README_EN.md b/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README_EN.md index b719a3f5d289c..4ca16f095265c 100644 --- a/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README_EN.md +++ b/solution/3100-3199/3144.Minimum Substring Partition of Equal Character Frequency/README_EN.md @@ -68,15 +68,15 @@ tags: ### Solution 1: Memoization Search + Hash Table -We design a function $\text{dfs}(i)$, which represents the minimum number of substrings starting from the string $s[i]$. So the answer is $\text{dfs}(0)$. +We design a function $\textit{dfs}(i)$, which represents the minimum number of substrings starting from the string $s[i]$. So the answer is $\textit{dfs}(0)$. -The calculation process of the function $\text{dfs}(i)$ is as follows: +The calculation process of the function $\textit{dfs}(i)$ is as follows: If $i \geq n$, it means that all characters have been processed, return $0$. -Otherwise, we maintain a hash table $\text{cnt}$, which represents the number of occurrences of each character in the current substring. In addition, we also maintain a hash table $\text{freq}$, which represents the frequency of the number of occurrences of each character. +Otherwise, we maintain a hash table $\textit{cnt}$, which represents the number of occurrences of each character in the current substring. In addition, we also maintain a hash table $\textit{freq}$, which represents the frequency of the number of occurrences of each character. -Then we enumerate $j$ from $i$ to $n-1$, which represents the end position of the current substring. For each $j$, we update $\text{cnt}$ and $\text{freq}$, then check whether the size of $\text{freq}$ is $1$. If so, we can start splitting from $j+1$, at this time the answer is $1 + \text{dfs}(j+1)$. We take the minimum value of the answer among all $j$ as the return value of the function. +Then we enumerate $j$ from $i$ to $n-1$, which represents the end position of the current substring. For each $j$, we update $\textit{cnt}$ and $\textit{freq}$, then check whether the size of $\textit{freq}$ is $1$. If so, we can start splitting from $j+1$, at this time the answer is $1 + \textit{dfs}(j+1)$. We take the minimum value of the answer among all $j$ as the return value of the function. To avoid repeated calculations, we use memoization search. diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md index a449b6239b2b2..fbc6dd3be277a 100644 --- a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README.md @@ -81,7 +81,7 @@ $$ f[i][j] = \min(f[i-1][j], f[i][j-1], grid[i][j]) $$ -那么答案为 $\text{grid}[i][j] - \min(f[i-1][j], f[i][j-1])$ 的最大值。 +那么答案为 $\textit{grid}[i][j] - \min(f[i-1][j], f[i][j-1])$ 的最大值。 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。 diff --git a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md index 8c361aec099e6..59cc08214a3e9 100644 --- a/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md +++ b/solution/3100-3199/3148.Maximum Difference Score in a Grid/README_EN.md @@ -79,7 +79,7 @@ $$ f[i][j] = \min(f[i-1][j], f[i][j-1], grid[i][j]) $$ -So the answer is the maximum value of $\text{grid}[i][j] - \min(f[i-1][j], f[i][j-1])$. +So the answer is the maximum value of $\textit{grid}[i][j] - \min(f[i-1][j], f[i][j-1])$. The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the matrix, respectively. diff --git a/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README.md b/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README.md index 3a807344e82b4..11890636496d4 100644 --- a/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README.md +++ b/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README.md @@ -74,20 +74,20 @@ tags: ### 方法一:记忆化搜索 -我们注意到,对于任意一个排列 $\text{perm}$,把它循环向左移动任意次,得到的排列分数依然是相同的。由于题目要求返回字典序最小的排列,因此我们可以确定排列的第一个元素一定是 $0$。 +我们注意到,对于任意一个排列 $\textit{perm}$,把它循环向左移动任意次,得到的排列分数依然是相同的。由于题目要求返回字典序最小的排列,因此我们可以确定排列的第一个元素一定是 $0$。 -另外,由于题目的数据范围不超过 $14$,我们可以考虑使用状态压缩的方法,来表示当前排列选取的数字集合。我们用一个长度为 $n$ 的二进制数 $\text{mask}$ 来表示当前排列选取的数字集合,其中 $\text{mask}$ 的第 $i$ 位为 $1$ 表示数字 $i$ 已经被选取,为 $0$ 表示数字 $i$ 还未被选取。 +另外,由于题目的数据范围不超过 $14$,我们可以考虑使用状态压缩的方法,来表示当前排列选取的数字集合。我们用一个长度为 $n$ 的二进制数 $\textit{mask}$ 来表示当前排列选取的数字集合,其中 $\textit{mask}$ 的第 $i$ 位为 $1$ 表示数字 $i$ 已经被选取,为 $0$ 表示数字 $i$ 还未被选取。 -我们设计一个函数 $\text{dfs}(\text{mask}, \text{pre})$,表示当前排列选取的数字集合为 $\text{mask}$,且最后一个选取的数字为 $\text{pre}$ 时,得到的排列的最小分数。初始时我们将数字 $0$ 加入到排列中。 +我们设计一个函数 $\textit{dfs}(\textit{mask}, \textit{pre})$,表示当前排列选取的数字集合为 $\textit{mask}$,且最后一个选取的数字为 $\textit{pre}$ 时,得到的排列的最小分数。初始时我们将数字 $0$ 加入到排列中。 -函数 $\text{dfs}(\text{mask}, \text{pre})$ 的计算过程如下: +函数 $\textit{dfs}(\textit{mask}, \textit{pre})$ 的计算过程如下: -- 如果 $\text{mask}$ 的二进制表示中 $1$ 的个数为 $n$,即 $\text{mask} = 2^n - 1$,表示所有数字都已经被选取,此时返回 $|\text{pre} - \text{nums}[0]|$; -- 否则,我们枚举下一个选取的数字 $\text{cur}$,如果数字 $\text{cur}$ 还未被选取,那么我们可以将数字 $\text{cur}$ 加入到排列中,此时排列的分数为 $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$,我们需要取所有 $\text{cur}$ 中分数的最小值。 +- 如果 $\textit{mask}$ 的二进制表示中 $1$ 的个数为 $n$,即 $\textit{mask} = 2^n - 1$,表示所有数字都已经被选取,此时返回 $|\textit{pre} - \textit{nums}[0]|$; +- 否则,我们枚举下一个选取的数字 $\textit{cur}$,如果数字 $\textit{cur}$ 还未被选取,那么我们可以将数字 $\textit{cur}$ 加入到排列中,此时排列的分数为 $|\textit{pre} - \textit{nums}[\textit{cur}]| + \textit{dfs}(\textit{mask} \, | \, 1 << \textit{cur}, \textit{cur})$,我们需要取所有 $\textit{cur}$ 中分数的最小值。 -最后,我们利用一个函数 $\text{g}(\text{mask}, \text{pre})$ 来构造得到最小分数的排列。我们首先将数字 $\text{pre}$ 加入到排列中,然后枚举下一个选取的数字 $\text{cur}$,如果数字 $\text{cur}$ 还未被选取,且满足 $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$ 的值等于 $\text{dfs}(\text{mask}, \text{pre})$,那么我们就可以将数字 $\text{cur}$ 加入到排列中。 +最后,我们利用一个函数 $\textit{g}(\textit{mask}, \textit{pre})$ 来构造得到最小分数的排列。我们首先将数字 $\textit{pre}$ 加入到排列中,然后枚举下一个选取的数字 $\textit{cur}$,如果数字 $\textit{cur}$ 还未被选取,且满足 $|\textit{pre} - \textit{nums}[\textit{cur}]| + \textit{dfs}(\textit{mask} \, | \, 1 << \textit{cur}, \textit{cur})$ 的值等于 $\textit{dfs}(\textit{mask}, \textit{pre})$,那么我们就可以将数字 $\textit{cur}$ 加入到排列中。 -时间复杂度 $(n^2 \times 2^n)$,空间复杂度 $O(n \times 2^n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 +时间复杂度 $(n^2 \times 2^n)$,空间复杂度 $O(n \times 2^n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 diff --git a/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README_EN.md b/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README_EN.md index 9c085d7a06677..b4c14ba1551a6 100644 --- a/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README_EN.md +++ b/solution/3100-3199/3149.Find the Minimum Cost Array Permutation/README_EN.md @@ -72,20 +72,20 @@ tags: ### Solution 1: Memoization Search -We notice that for any permutation $\text{perm}$, if we cyclically shift it to the left any number of times, the score of the permutation remains the same. Since the problem requires returning the lexicographically smallest permutation, we can determine that the first element of the permutation must be $0$. +We notice that for any permutation $\textit{perm}$, if we cyclically shift it to the left any number of times, the score of the permutation remains the same. Since the problem requires returning the lexicographically smallest permutation, we can determine that the first element of the permutation must be $0$. -Also, since the data range of the problem does not exceed $14$, we can consider using the method of state compression to represent the set of numbers selected in the current permutation. We use a binary number $\text{mask}$ of length $n$ to represent the set of numbers selected in the current permutation, where the $i$-th bit of $\text{mask}$ is $1$ indicates that the number $i$ has been selected, and $0$ indicates that the number $i$ has not been selected yet. +Also, since the data range of the problem does not exceed $14$, we can consider using the method of state compression to represent the set of numbers selected in the current permutation. We use a binary number $\textit{mask}$ of length $n$ to represent the set of numbers selected in the current permutation, where the $i$-th bit of $\textit{mask}$ is $1$ indicates that the number $i$ has been selected, and $0$ indicates that the number $i$ has not been selected yet. -We design a function $\text{dfs}(\text{mask}, \text{pre})$, which represents the minimum score of the permutation obtained when the set of numbers selected in the current permutation is $\text{mask}$ and the last selected number is $\text{pre}$. Initially, we add the number $0$ to the permutation. +We design a function $\textit{dfs}(\textit{mask}, \textit{pre})$, which represents the minimum score of the permutation obtained when the set of numbers selected in the current permutation is $\textit{mask}$ and the last selected number is $\textit{pre}$. Initially, we add the number $0$ to the permutation. -The calculation process of the function $\text{dfs}(\text{mask}, \text{pre})$ is as follows: +The calculation process of the function $\textit{dfs}(\textit{mask}, \textit{pre})$ is as follows: -- If the number of $1$s in the binary representation of $\text{mask}$ is $n$, that is, $\text{mask} = 2^n - 1$, it means that all numbers have been selected, then return $|\text{pre} - \text{nums}[0]|$; -- Otherwise, we enumerate the next selected number $\text{cur}$. If the number $\text{cur}$ has not been selected yet, then we can add the number $\text{cur}$ to the permutation. At this time, the score of the permutation is $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$. We need to take the minimum score among all $\text{cur}$. +- If the number of $1$s in the binary representation of $\textit{mask}$ is $n$, that is, $\textit{mask} = 2^n - 1$, it means that all numbers have been selected, then return $|\textit{pre} - \textit{nums}[0]|$; +- Otherwise, we enumerate the next selected number $\textit{cur}$. If the number $\textit{cur}$ has not been selected yet, then we can add the number $\textit{cur}$ to the permutation. At this time, the score of the permutation is $|\textit{pre} - \textit{nums}[\textit{cur}]| + \textit{dfs}(\textit{mask} \, | \, 1 << \textit{cur}, \textit{cur})$. We need to take the minimum score among all $\textit{cur}$. -Finally, we use a function $\text{g}(\text{mask}, \text{pre})$ to construct the permutation that gets the minimum score. We first add the number $\text{pre}$ to the permutation, and then enumerate the next selected number $\text{cur}$. If the number $\text{cur}$ has not been selected yet, and it satisfies that the value of $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$ is equal to $\text{dfs}(\text{mask}, \text{pre})$, then we can add the number $\text{cur}$ to the permutation. +Finally, we use a function $\textit{g}(\textit{mask}, \textit{pre})$ to construct the permutation that gets the minimum score. We first add the number $\textit{pre}$ to the permutation, and then enumerate the next selected number $\textit{cur}$. If the number $\textit{cur}$ has not been selected yet, and it satisfies that the value of $|\textit{pre} - \textit{nums}[\textit{cur}]| + \textit{dfs}(\textit{mask} \, | \, 1 << \textit{cur}, \textit{cur})$ is equal to $\textit{dfs}(\textit{mask}, \textit{pre})$, then we can add the number $\textit{cur}$ to the permutation. -The time complexity is $(n^2 \times 2^n)$, and the space complexity is $O(n \times 2^n)$. Where $n$ is the length of the array $\text{nums}$. +The time complexity is $(n^2 \times 2^n)$, and the space complexity is $O(n \times 2^n)$. Where $n$ is the length of the array $\textit{nums}$. diff --git a/solution/3100-3199/3154.Find Number of Ways to Reach the K-th Stair/README.md b/solution/3100-3199/3154.Find Number of Ways to Reach the K-th Stair/README.md index 857b6a73d2c6c..0f1a47832cb8d 100644 --- a/solution/3100-3199/3154.Find Number of Ways to Reach the K-th Stair/README.md +++ b/solution/3100-3199/3154.Find Number of Ways to Reach the K-th Stair/README.md @@ -117,14 +117,14 @@ tags: ### 方法一:记忆化搜索 -我们设计一个函数 $\text{dfs}(i, j, \text{jump})$,表示当前位于第 $i$ 级台阶,且进行了 $j$ 次操作 $1$ 和 $\text{jump}$ 次操作 $2$,到达第 $k$ 级台阶的方案数。那么答案就是 $\text{dfs}(1, 0, 0)$。 +我们设计一个函数 $\textit{dfs}(i, j, \textit{jump})$,表示当前位于第 $i$ 级台阶,且进行了 $j$ 次操作 $1$ 和 $\textit{jump}$ 次操作 $2$,到达第 $k$ 级台阶的方案数。那么答案就是 $\textit{dfs}(1, 0, 0)$。 -函数 $\text{dfs}(i, j, \text{jump})$ 的计算过程如下: +函数 $\textit{dfs}(i, j, \textit{jump})$ 的计算过程如下: - 如果 $i > k + 1$,由于无法连续两次向下走,所以无法再到达第 $k$ 级台阶,返回 $0$; - 如果 $i = k$,表示已经到达第 $k$ 级台阶,答案初始化为 $1$,然后继续计算; -- 如果 $i > 0$ 且 $j = 0$,表示可以向下走,递归计算 $\text{dfs}(i - 1, 1, \text{jump})$; -- 递归计算 $\text{dfs}(i + 2^{\text{jump}}, 0, \text{jump} + 1)$,累加到答案中。 +- 如果 $i > 0$ 且 $j = 0$,表示可以向下走,递归计算 $\textit{dfs}(i - 1, 1, \textit{jump})$; +- 递归计算 $\textit{dfs}(i + 2^{\textit{jump}}, 0, \textit{jump} + 1)$,累加到答案中。 为了避免重复计算,我们使用记忆化搜索,将已经计算过的状态保存起来。 diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md index 6a59554da214f..72e0934490fc0 100644 --- a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md @@ -74,7 +74,7 @@ tags: ### 方法一:数学 -对于每个数据中心,我们假设可以升级 $\text{x}$ 台服务器,那么 $\text{x} \times \text{upgrade[i]} \leq \text{count[i]} \times \text{sell[i]} + \text{money[i]}$。即 $\text{x} \leq \frac{\text{count[i]} \times \text{sell[i]} + \text{money[i]}}{\text{upgrade[i]} + \text{sell[i]}}$。又因为 $\text{x} \leq \text{count[i]}$,所以我们取两者的最小值即可。 +对于每个数据中心,我们假设可以升级 $\textit{x}$ 台服务器,那么 $\textit{x} \times \textit{upgrade[i]} \leq \textit{count[i]} \times \textit{sell[i]} + \textit{money[i]}$。即 $\textit{x} \leq \frac{\textit{count[i]} \times \textit{sell[i]} + \textit{money[i]}}{\textit{upgrade[i]} + \textit{sell[i]}}$。又因为 $\textit{x} \leq \textit{count[i]}$,所以我们取两者的最小值即可。 时间复杂度 $O(n)$,其中 $n$ 为数组的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md index 7272531b497cf..8fa54e1696acd 100644 --- a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md @@ -74,7 +74,7 @@ tags: ### Solution 1: Mathematics -For each data center, we assume that we can upgrade $x$ servers, then $x \times \text{upgrade[i]} \leq \text{count[i]} \times \text{sell[i]} + \text{money[i]}$. That is, $x \leq \frac{\text{count[i]} \times \text{sell[i]} + \text{money[i]}}{\text{upgrade[i]} + \text{sell[i]}}$. Also, $x \leq \text{count[i]}$, so we can take the minimum of the two. +For each data center, we assume that we can upgrade $x$ servers, then $x \times \textit{upgrade[i]} \leq \textit{count[i]} \times \textit{sell[i]} + \textit{money[i]}$. That is, $x \leq \frac{\textit{count[i]} \times \textit{sell[i]} + \textit{money[i]}}{\textit{upgrade[i]} + \textit{sell[i]}}$. Also, $x \leq \textit{count[i]}$, so we can take the minimum of the two. The time complexity is $O(n)$, where $n$ is the length of the array. Ignoring the space consumption of the answer array, the space complexity is $O(1)$. diff --git a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md index 54e3498390cfa..ee517afe32fe7 100644 --- a/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md +++ b/solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md @@ -80,13 +80,13 @@ tags: ### 方法一:计数 -我们定义一个数组或哈希表 $\text{cnt}$ 记录每个数字出现的次数。 +我们定义一个数组或哈希表 $\textit{cnt}$ 记录每个数字出现的次数。 -接下来,遍历数组 $\text{nums}$,当某个数字出现两次时,我们将其与答案进行异或运算。 +接下来,遍历数组 $\textit{nums}$,当某个数字出现两次时,我们将其与答案进行异或运算。 最后返回答案即可。 -时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 是数组 $\text{nums}$ 的长度,而 $M$ 是数组 $\text{nums}$ 中的最大值或数组 $\text{nums}$ 不同数字的个数。 +时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 是数组 $\textit{nums}$ 的长度,而 $M$ 是数组 $\textit{nums}$ 中的最大值或数组 $\textit{nums}$ 不同数字的个数。 diff --git a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md index e40f0c105fb26..8c0901f51d4e5 100644 --- a/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md +++ b/solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md @@ -76,11 +76,11 @@ tags: ### 方法一:模拟 -根据题目描述,我们可以先遍历一遍数组 $\text{nums}$,找出所有值为 $x$ 的元素的下标,记录在数组 $\text{ids}$ 中。 +根据题目描述,我们可以先遍历一遍数组 $\textit{nums}$,找出所有值为 $x$ 的元素的下标,记录在数组 $\textit{ids}$ 中。 -接着遍历数组 $\text{queries}$,对于每个查询 $i$,如果 $i - 1$ 小于 $\text{ids}$ 的长度,那么答案就是 $\text{ids}[i - 1]$,否则答案就是 $-1$。 +接着遍历数组 $\textit{queries}$,对于每个查询 $i$,如果 $i - 1$ 小于 $\textit{ids}$ 的长度,那么答案就是 $\textit{ids}[i - 1]$,否则答案就是 $-1$。 -时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $\text{nums}$ 和数组 $\text{queries}$ 的长度。 +时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $\textit{nums}$ 和数组 $\textit{queries}$ 的长度。 diff --git a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md index f67d5d4a3f5c4..7c2a9221b2611 100644 --- a/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md +++ b/solution/3100-3199/3160.Find the Number of Distinct Colors Among the Balls/README.md @@ -89,13 +89,13 @@ tags: ### 方法一:双哈希表 -我们使用一个哈希表 $\text{g}$ 记录每个球的颜色,使用一个哈希表 $\text{cnt}$ 记录每种颜色的球的个数。 +我们使用一个哈希表 $\textit{g}$ 记录每个球的颜色,使用一个哈希表 $\textit{cnt}$ 记录每种颜色的球的个数。 -接下来,遍历数组 $\text{queries}$,对于每个查询 $(x, y)$,我们将颜色 $y$ 的球的个数加 $1$,然后判断球 $x$ 是否已经染色,如果已经染色,我们将球 $x$ 的颜色的球的个数减 $1$,如果减到 $0$,我们将其从哈希表 $\text{cnt}$ 中移除。接下来,我们将球 $x$ 的颜色更新为 $y$,并将当前哈希表 $\text{cnt}$ 的大小加入答案数组。 +接下来,遍历数组 $\textit{queries}$,对于每个查询 $(x, y)$,我们将颜色 $y$ 的球的个数加 $1$,然后判断球 $x$ 是否已经染色,如果已经染色,我们将球 $x$ 的颜色的球的个数减 $1$,如果减到 $0$,我们将其从哈希表 $\textit{cnt}$ 中移除。接下来,我们将球 $x$ 的颜色更新为 $y$,并将当前哈希表 $\textit{cnt}$ 的大小加入答案数组。 遍历结束后,返回答案数组即可。 -时间复杂度 $O(m)$,空间复杂度 $O(m)$,其中 $m$ 为数组 $\text{queries}$ 的长度。 +时间复杂度 $O(m)$,空间复杂度 $O(m)$,其中 $m$ 为数组 $\textit{queries}$ 的长度。 diff --git a/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md b/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md index a99efdfd8a83e..bf962a213512e 100644 --- a/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md +++ b/solution/3100-3199/3162.Find the Number of Good Pairs I/README.md @@ -161,11 +161,11 @@ function numberOfPairs(nums1: number[], nums2: number[], k: number): number { ### 方法二:哈希表 + 枚举倍数 -我们用一个哈希表 $\text{cnt1}$ 记录数组 $\text{nums1}$ 中每个数除以 $k$ 的商的出现次数,用一个哈希表 $\text{cnt2}$ 记录数组 $\text{nums2}$ 中每个数的出现次数。 +我们用一个哈希表 $\textit{cnt1}$ 记录数组 $\textit{nums1}$ 中每个数除以 $k$ 的商的出现次数,用一个哈希表 $\textit{cnt2}$ 记录数组 $\textit{nums2}$ 中每个数的出现次数。 -接下来,我们枚举数组 $\text{nums2}$ 中的每个数 $x$,对于每个数 $x$,我们枚举 $x$ 的倍数 $y$,其中 $y$ 的范围是 $[x, \text{mx}]$,其中 $\text{mx}$ 是 $\text{cnt1}$ 中的最大键值,然后我们统计 $\text{cnt1}[y]$ 的和,记为 $s$,最后我们将 $s \times v$ 累加到答案中,其中 $v$ 是 $\text{cnt2}[x]$。 +接下来,我们枚举数组 $\textit{nums2}$ 中的每个数 $x$,对于每个数 $x$,我们枚举 $x$ 的倍数 $y$,其中 $y$ 的范围是 $[x, \textit{mx}]$,其中 $\textit{mx}$ 是 $\textit{cnt1}$ 中的最大键值,然后我们统计 $\textit{cnt1}[y]$ 的和,记为 $s$,最后我们将 $s \times v$ 累加到答案中,其中 $v$ 是 $\textit{cnt2}[x]$。 -时间复杂度 $O(n + m + (M / k) \times \log m)$,空间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是数组 $\text{nums1}$ 和 $\text{nums2}$ 的长度,而 $M$ 是数组 $\text{nums1}$ 中的最大值。 +时间复杂度 $O(n + m + (M / k) \times \log m)$,空间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的长度,而 $M$ 是数组 $\textit{nums1}$ 中的最大值。 diff --git a/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md b/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md index 194216adbaa3c..c7da5c2e19a70 100644 --- a/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md +++ b/solution/3100-3199/3162.Find the Number of Good Pairs I/README_EN.md @@ -159,7 +159,7 @@ function numberOfPairs(nums1: number[], nums2: number[], k: number): number { We use a hash table `cnt1` to record the occurrence times of each number divided by $k$ in array `nums1`, and a hash table `cnt2` to record the occurrence times of each number in array `nums2`. -Next, we enumerate each number $x$ in array `nums2`. For each number $x$, we enumerate its multiples $y$, where the range of $y$ is $[x, \text{mx}]$, where `mx` is the maximum key value in `cnt1`. Then we count the sum of `cnt1[y]`, denoted as $s$. Finally, we add $s \times v$ to the answer, where $v$ is `cnt2[x]`. +Next, we enumerate each number $x$ in array `nums2`. For each number $x$, we enumerate its multiples $y$, where the range of $y$ is $[x, \textit{mx}]$, where `mx` is the maximum key value in `cnt1`. Then we count the sum of `cnt1[y]`, denoted as $s$. Finally, we add $s \times v$ to the answer, where $v$ is `cnt2[x]`. The time complexity is $O(n + m + (M / k) \times \log m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the lengths of arrays `nums1` and `nums2` respectively, and $M$ is the maximum value in array `nums1`. diff --git a/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md b/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md index 8fd39fac99c32..0dbd980e14d36 100644 --- a/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md +++ b/solution/3100-3199/3164.Find the Number of Good Pairs II/README.md @@ -69,11 +69,11 @@ tags: ### 方法一:哈希表 + 枚举倍数 -我们用一个哈希表 $\text{cnt1}$ 记录数组 $\text{nums1}$ 中每个数除以 $k$ 的商的出现次数,用一个哈希表 $\text{cnt2}$ 记录数组 $\text{nums2}$ 中每个数的出现次数。 +我们用一个哈希表 $\textit{cnt1}$ 记录数组 $\textit{nums1}$ 中每个数除以 $k$ 的商的出现次数,用一个哈希表 $\textit{cnt2}$ 记录数组 $\textit{nums2}$ 中每个数的出现次数。 -接下来,我们枚举数组 $\text{nums2}$ 中的每个数 $x$,对于每个数 $x$,我们枚举 $x$ 的倍数 $y$,其中 $y$ 的范围是 $[x, \text{mx}]$,其中 $\text{mx}$ 是 $\text{cnt1}$ 中的最大键值,然后我们统计 $\text{cnt1}[y]$ 的和,记为 $s$,最后我们将 $s \times v$ 累加到答案中,其中 $v$ 是 $\text{cnt2}[x]$。 +接下来,我们枚举数组 $\textit{nums2}$ 中的每个数 $x$,对于每个数 $x$,我们枚举 $x$ 的倍数 $y$,其中 $y$ 的范围是 $[x, \textit{mx}]$,其中 $\textit{mx}$ 是 $\textit{cnt1}$ 中的最大键值,然后我们统计 $\textit{cnt1}[y]$ 的和,记为 $s$,最后我们将 $s \times v$ 累加到答案中,其中 $v$ 是 $\textit{cnt2}[x]$。 -时间复杂度 $O(n + m + (M / k) \times \log m)$,空间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是数组 $\text{nums1}$ 和 $\text{nums2}$ 的长度,而 $M$ 是数组 $\text{nums1}$ 中的最大值。 +时间复杂度 $O(n + m + (M / k) \times \log m)$,空间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的长度,而 $M$ 是数组 $\textit{nums1}$ 中的最大值。 diff --git a/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md b/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md index b15407f7cdf8e..1a798ecde7d06 100644 --- a/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md +++ b/solution/3100-3199/3164.Find the Number of Good Pairs II/README_EN.md @@ -67,7 +67,7 @@ The 5 good pairs are (0, 0), (1, 0), (1, 1) diff --git a/solution/3100-3199/3169.Count Days Without Meetings/README.md b/solution/3100-3199/3169.Count Days Without Meetings/README.md index bb0e8d21cc9b2..62d0f4d43c51d 100644 --- a/solution/3100-3199/3169.Count Days Without Meetings/README.md +++ b/solution/3100-3199/3169.Count Days Without Meetings/README.md @@ -82,11 +82,11 @@ tags: ### 方法一:排序 -我们不妨将所有会议按照开始时间排序,用一个变量 $\text{last}$ 记录此前会议的最晚结束时间。 +我们不妨将所有会议按照开始时间排序,用一个变量 $\textit{last}$ 记录此前会议的最晚结束时间。 -然后我们遍历所有会议,对于每一个会议 $(\text{st}, \text{ed})$,如果 $\text{last} < \text{st}$,说明 $\text{last}$ 到 $\text{st}$ 之间的时间段是员工可以工作且没有安排会议的时间,我们将这段时间加入答案。然后我们更新 $\text{last} = \max(\text{last}, \text{ed})$。 +然后我们遍历所有会议,对于每一个会议 $(\textit{st}, \textit{ed})$,如果 $\textit{last} < \textit{st}$,说明 $\textit{last}$ 到 $\textit{st}$ 之间的时间段是员工可以工作且没有安排会议的时间,我们将这段时间加入答案。然后我们更新 $\textit{last} = \max(\textit{last}, \textit{ed})$。 -最后,如果 $\text{last} < \text{days}$,说明最后一次会议结束后还有时间段是员工可以工作且没有安排会议的时间,我们将这段时间加入答案。 +最后,如果 $\textit{last} < \textit{days}$,说明最后一次会议结束后还有时间段是员工可以工作且没有安排会议的时间,我们将这段时间加入答案。 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为会议的数量。 diff --git a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md index 40158e36eba5b..ae6054d888a23 100644 --- a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md +++ b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README.md @@ -75,8 +75,8 @@ tags: $$ f[i][h]= \begin{cases} -\max(f[i][h], f[j][h] + 1), & \text{if } nums[i] = nums[j], \\ -\max(f[i][h], f[j][h - 1] + 1), & \text{if } h > 0. +\max(f[i][h], f[j][h] + 1), & \textit{if } nums[i] = nums[j], \\ +\max(f[i][h], f[j][h - 1] + 1), & \textit{if } h > 0. \end{cases} $$ diff --git a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md index 3f9a6db909578..e153a19dd2df6 100644 --- a/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md +++ b/solution/3100-3199/3176.Find the Maximum Length of a Good Subsequence I/README_EN.md @@ -73,8 +73,8 @@ We consider how to calculate $f[i][h]$. We can enumerate $0 \le j < i$, if $nums $$ f[i][h]= \begin{cases} -\max(f[i][h], f[j][h] + 1), & \text{if } nums[i] = nums[j], \\ -\max(f[i][h], f[j][h - 1] + 1), & \text{if } h > 0. +\max(f[i][h], f[j][h] + 1), & \textit{if } nums[i] = nums[j], \\ +\max(f[i][h], f[j][h - 1] + 1), & \textit{if } h > 0. \end{cases} $$ diff --git a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md index 161d08460dd5e..6d4f6f32254c9 100644 --- a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md +++ b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README.md @@ -75,8 +75,8 @@ tags: $$ f[i][h]= \begin{cases} -\max(f[i][h], f[j][h] + 1), & \text{if } nums[i] = nums[j], \\ -\max(f[i][h], f[j][h - 1] + 1), & \text{if } h > 0. +\max(f[i][h], f[j][h] + 1), & \textit{if } nums[i] = nums[j], \\ +\max(f[i][h], f[j][h - 1] + 1), & \textit{if } h > 0. \end{cases} $$ diff --git a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md index 62f0620bbecec..da52faadc5569 100644 --- a/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md +++ b/solution/3100-3199/3177.Find the Maximum Length of a Good Subsequence II/README_EN.md @@ -73,8 +73,8 @@ We consider how to calculate $f[i][h]$. We can enumerate $0 \le j < i$, if $nums $$ f[i][h]= \begin{cases} -\max(f[i][h], f[j][h] + 1), & \text{if } nums[i] = nums[j], \\ -\max(f[i][h], f[j][h - 1] + 1), & \text{if } h > 0. +\max(f[i][h], f[j][h] + 1), & \textit{if } nums[i] = nums[j], \\ +\max(f[i][h], f[j][h - 1] + 1), & \textit{if } h > 0. \end{cases} $$ diff --git a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md index 5f8735a3c3082..059ddcce253d6 100644 --- a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md +++ b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README.md @@ -75,12 +75,12 @@ tags: 我们可以对奖励值数组 `rewardValues` 进行排序,然后使用记忆化搜索的方法求解最大总奖励。 -我们定义一个函数 $\text{dfs}(x)$,表示当前总奖励为 $x$ 时,能够获得的最大总奖励。那么答案为 $\text{dfs}(0)$。 +我们定义一个函数 $\textit{dfs}(x)$,表示当前总奖励为 $x$ 时,能够获得的最大总奖励。那么答案为 $\textit{dfs}(0)$。 -函数 $\text{dfs}(x)$ 的执行过程如下: +函数 $\textit{dfs}(x)$ 的执行过程如下: 1. 二分查找数组 `rewardValues` 中第一个大于 $x$ 的元素的下标 $i$; -2. 遍历数组 `rewardValues` 中从下标 $i$ 开始的元素,对于每个元素 $v$,计算 $v + \text{dfs}(x + v)$ 的最大值。 +2. 遍历数组 `rewardValues` 中从下标 $i$ 开始的元素,对于每个元素 $v$,计算 $v + \textit{dfs}(x + v)$ 的最大值。 3. 将结果返回。 为了避免重复计算,我们使用记忆化数组 `f` 记录已经计算过的结果。 @@ -228,7 +228,7 @@ function maxTotalReward(rewardValues: number[]): number { ### 方法二:动态规划 -我们定义 $f[i][j]$ 表示用前 $i$ 个奖励值,能否得到总奖励 $j$。初始时 $f[0][0] = \text{True}$,其余值均为 $\text{False}$。 +我们定义 $f[i][j]$ 表示用前 $i$ 个奖励值,能否得到总奖励 $j$。初始时 $f[0][0] = \textit{True}$,其余值均为 $\textit{False}$。 我们考虑第 $i$ 个奖励值 $v$,如果我们不选择它,那么 $f[i][j] = f[i - 1][j]$;如果我们选择它,那么 $f[i][j] = f[i - 1][j - v]$,其中 $0 \leq j - v \lt v$。即状态转移方程为: @@ -236,7 +236,7 @@ $$ f[i][j] = f[i - 1][j] \vee f[i - 1][j - v] $$ -最终答案为 $\max\{j \mid f[n][j] = \text{True}\}$。 +最终答案为 $\max\{j \mid f[n][j] = \textit{True}\}$。 由于 $f[i][j]$ 只与 $f[i - 1][j]$ 和 $f[i - 1][j - v]$ 有关,我们可以优化掉第一维,只使用一个一维数组进行状态转移。 diff --git a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md index 10cd748978f98..8a39b4d1a95b0 100644 --- a/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md +++ b/solution/3100-3199/3180.Maximum Total Reward Using Operations I/README_EN.md @@ -73,12 +73,12 @@ tags: We can sort the `rewardValues` array and then use memoization to solve for the maximum total reward. -We define a function $\text{dfs}(x)$, representing the maximum total reward that can be obtained when the current total reward is $x$. Thus, the answer is $\text{dfs}(0)$. +We define a function $\textit{dfs}(x)$, representing the maximum total reward that can be obtained when the current total reward is $x$. Thus, the answer is $\textit{dfs}(0)$. -The execution process of the function $\text{dfs}(x)$ is as follows: +The execution process of the function $\textit{dfs}(x)$ is as follows: 1. Perform a binary search in the `rewardValues` array for the index $i$ of the first element greater than $x$; -2. Iterate over the elements in the `rewardValues` array starting from index $i$, and for each element $v$, calculate the maximum value of $v + \text{dfs}(x + v)$. +2. Iterate over the elements in the `rewardValues` array starting from index $i$, and for each element $v$, calculate the maximum value of $v + \textit{dfs}(x + v)$. 3. Return the result. To avoid repeated calculations, we use a memoization array `f` to record the results that have already been computed. @@ -226,7 +226,7 @@ function maxTotalReward(rewardValues: number[]): number { ### Solution 2: Dynamic Programming -We define $f[i][j]$ as whether it is possible to obtain a total reward of $j$ using the first $i$ reward values. Initially, $f[0][0] = \text{True}$, and all other values are $\text{False}$. +We define $f[i][j]$ as whether it is possible to obtain a total reward of $j$ using the first $i$ reward values. Initially, $f[0][0] = \textit{True}$, and all other values are $\textit{False}$. We consider the $i$-th reward value $v$. If we do not choose it, then $f[i][j] = f[i - 1][j]$. If we choose it, then $f[i][j] = f[i - 1][j - v]$, where $0 \leq j - v < v$. Thus, the state transition equation is: @@ -234,7 +234,7 @@ $$ f[i][j] = f[i - 1][j] \vee f[i - 1][j - v] $$ -The final answer is $\max\{j \mid f[n][j] = \text{True}\}$. +The final answer is $\max\{j \mid f[n][j] = \textit{True}\}$. Since $f[i][j]$ only depends on $f[i - 1][j]$ and $f[i - 1][j - v]$, we can optimize away the first dimension and use only a one-dimensional array for state transitions. diff --git a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md index be8aeacc8e602..94676efc4b914 100644 --- a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md +++ b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README.md @@ -74,7 +74,7 @@ tags: ### 方法一:动态规划 + 位运算 -我们定义 $f[i][j]$ 表示用前 $i$ 个奖励值,能否得到总奖励 $j$。初始时 $f[0][0] = \text{True}$,其余值均为 $\text{False}$。 +我们定义 $f[i][j]$ 表示用前 $i$ 个奖励值,能否得到总奖励 $j$。初始时 $f[0][0] = \textit{True}$,其余值均为 $\textit{False}$。 我们考虑第 $i$ 个奖励值 $v$,如果我们不选择它,那么 $f[i][j] = f[i - 1][j]$;如果我们选择它,那么 $f[i][j] = f[i - 1][j - v]$,其中 $0 \leq j - v \lt v$。即状态转移方程为: @@ -82,7 +82,7 @@ $$ f[i][j] = f[i - 1][j] \vee f[i - 1][j - v] $$ -最终答案为 $\max\{j \mid f[n][j] = \text{True}\}$。 +最终答案为 $\max\{j \mid f[n][j] = \textit{True}\}$。 由于 $f[i][j]$ 只与 $f[i - 1][j]$ 和 $f[i - 1][j - v]$ 有关,我们可以优化掉第一维,只使用一个一维数组进行状态转移。另外,由于本题数据范围较大,我们需要使用位运算来优化状态转移的效率。 diff --git a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md index f745d0c947de1..358708a1eff5b 100644 --- a/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md +++ b/solution/3100-3199/3181.Maximum Total Reward Using Operations II/README_EN.md @@ -72,7 +72,7 @@ tags: ### Solution 1: Dynamic Programming + Bit Manipulation -We define $f[i][j]$ as whether it is possible to obtain a total reward of $j$ using the first $i$ reward values. Initially, $f[0][0] = \text{True}$, and all other values are $\text{False}$. +We define $f[i][j]$ as whether it is possible to obtain a total reward of $j$ using the first $i$ reward values. Initially, $f[0][0] = \textit{True}$, and all other values are $\textit{False}$. We consider the $i$-th reward value $v$. If we do not choose it, then $f[i][j] = f[i - 1][j]$; if we choose it, then $f[i][j] = f[i - 1][j - v]$, where $0 \leq j - v < v$. Thus, the state transition equation is: @@ -80,7 +80,7 @@ $$ f[i][j] = f[i - 1][j] \vee f[i - 1][j - v] $$ -The final answer is $\max\{j \mid f[n][j] = \text{True}\}$. +The final answer is $\max\{j \mid f[n][j] = \textit{True}\}$. Since $f[i][j]$ only depends on $f[i - 1][j]$ and $f[i - 1][j - v]$, we can optimize away the first dimension and use only a one-dimensional array for state transitions. Additionally, due to the large data range of this problem, we need to use bit manipulation to optimize the efficiency of state transitions. diff --git a/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README.md b/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README.md index ce80cd4013523..c930628bc2e2c 100644 --- a/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README.md +++ b/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README.md @@ -69,13 +69,13 @@ tags: ### 方法一:计数 -我们可以用一个哈希表或者一个长度为 $24$ 的数组 $\text{cnt}$ 来记录每个小时数模 $24$ 的出现次数。 +我们可以用一个哈希表或者一个长度为 $24$ 的数组 $\textit{cnt}$ 来记录每个小时数模 $24$ 的出现次数。 -遍历数组 $\text{hours}$,对于每个小时数 $x$,我们可以得出与 $x$ 相加为 $24$ 的倍数,且模 $24$ 之后的数为 $(24 - x \bmod 24) \bmod 24$。累加这个数在哈希表或者数组中的出现次数即可。然后我们将 $x$ 的模 $24$ 的出现次数加一。 +遍历数组 $\textit{hours}$,对于每个小时数 $x$,我们可以得出与 $x$ 相加为 $24$ 的倍数,且模 $24$ 之后的数为 $(24 - x \bmod 24) \bmod 24$。累加这个数在哈希表或者数组中的出现次数即可。然后我们将 $x$ 的模 $24$ 的出现次数加一。 -遍历完数组 $\text{hours}$ 后,我们就可以得到满足题意的下标对数目。 +遍历完数组 $\textit{hours}$ 后,我们就可以得到满足题意的下标对数目。 -时间复杂度 $O(n)$,其中 $n$ 为数组 $\text{hours}$ 的长度。空间复杂度 $O(C)$,其中 $C=24$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{hours}$ 的长度。空间复杂度 $O(C)$,其中 $C=24$。 diff --git a/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README_EN.md b/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README_EN.md index ea32cc1e5aa58..c40c413ca542a 100644 --- a/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README_EN.md +++ b/solution/3100-3199/3184.Count Pairs That Form a Complete Day I/README_EN.md @@ -67,13 +67,13 @@ tags: ### Solution 1: Counting -We can use a hash table or an array $\text{cnt}$ of length $24$ to record the occurrence count of each hour modulo $24$. +We can use a hash table or an array $\textit{cnt}$ of length $24$ to record the occurrence count of each hour modulo $24$. -Iterate through the array $\text{hours}$. For each hour $x$, we can find the number that, when added to $x$, results in a multiple of $24$, and after modulo $24$, this number is $(24 - x \bmod 24) \bmod 24$. We then accumulate the occurrence count of this number from the hash table or array. After that, we increment the occurrence count of $x$ modulo $24$ by one. +Iterate through the array $\textit{hours}$. For each hour $x$, we can find the number that, when added to $x$, results in a multiple of $24$, and after modulo $24$, this number is $(24 - x \bmod 24) \bmod 24$. We then accumulate the occurrence count of this number from the hash table or array. After that, we increment the occurrence count of $x$ modulo $24$ by one. -After iterating through the array $\text{hours}$, we can obtain the number of index pairs that meet the problem requirements. +After iterating through the array $\textit{hours}$, we can obtain the number of index pairs that meet the problem requirements. -The time complexity is $O(n)$, where $n$ is the length of the array $\text{hours}$. The space complexity is $O(C)$, where $C=24$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{hours}$. The space complexity is $O(C)$, where $C=24$. diff --git a/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README.md b/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README.md index 7298a0f76cc48..61dcd201bce13 100644 --- a/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README.md +++ b/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README.md @@ -69,13 +69,13 @@ tags: ### 方法一:计数 -我们可以用一个哈希表或者一个长度为 $24$ 的数组 $\text{cnt}$ 来记录每个小时数模 $24$ 的出现次数。 +我们可以用一个哈希表或者一个长度为 $24$ 的数组 $\textit{cnt}$ 来记录每个小时数模 $24$ 的出现次数。 -遍历数组 $\text{hours}$,对于每个小时数 $x$,我们可以得出与 $x$ 相加为 $24$ 的倍数,且模 $24$ 之后的数为 $(24 - x \bmod 24) \bmod 24$。累加这个数在哈希表或者数组中的出现次数即可。然后我们将 $x$ 的模 $24$ 的出现次数加一。 +遍历数组 $\textit{hours}$,对于每个小时数 $x$,我们可以得出与 $x$ 相加为 $24$ 的倍数,且模 $24$ 之后的数为 $(24 - x \bmod 24) \bmod 24$。累加这个数在哈希表或者数组中的出现次数即可。然后我们将 $x$ 的模 $24$ 的出现次数加一。 -遍历完数组 $\text{hours}$ 后,我们就可以得到满足题意的下标对数目。 +遍历完数组 $\textit{hours}$ 后,我们就可以得到满足题意的下标对数目。 -时间复杂度 $O(n)$,其中 $n$ 为数组 $\text{hours}$ 的长度。空间复杂度 $O(C)$,其中 $C=24$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{hours}$ 的长度。空间复杂度 $O(C)$,其中 $C=24$。 diff --git a/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README_EN.md b/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README_EN.md index 7d25568c84c05..ea322133fd681 100644 --- a/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README_EN.md +++ b/solution/3100-3199/3185.Count Pairs That Form a Complete Day II/README_EN.md @@ -63,13 +63,13 @@ tags: ### Solution 1: Counting -We can use a hash table or an array $\text{cnt}$ of length $24$ to record the occurrence count of each hour modulo $24$. +We can use a hash table or an array $\textit{cnt}$ of length $24$ to record the occurrence count of each hour modulo $24$. -Iterate through the array $\text{hours}$. For each hour $x$, we can find the number that, when added to $x$, results in a multiple of $24$, and after modulo $24$, this number is $(24 - x \bmod 24) \bmod 24$. We then accumulate the occurrence count of this number from the hash table or array. After that, we increment the occurrence count of $x$ modulo $24$ by one. +Iterate through the array $\textit{hours}$. For each hour $x$, we can find the number that, when added to $x$, results in a multiple of $24$, and after modulo $24$, this number is $(24 - x \bmod 24) \bmod 24$. We then accumulate the occurrence count of this number from the hash table or array. After that, we increment the occurrence count of $x$ modulo $24$ by one. -After iterating through the array $\text{hours}$, we can obtain the number of index pairs that meet the problem requirements. +After iterating through the array $\textit{hours}$, we can obtain the number of index pairs that meet the problem requirements. -The time complexity is $O(n)$, where $n$ is the length of the array $\text{hours}$. The space complexity is $O(C)$, where $C=24$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{hours}$. The space complexity is $O(C)$, where $C=24$. diff --git a/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README.md b/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README.md index 1703ebb823f69..0f90dde7130b7 100644 --- a/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README.md +++ b/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README.md @@ -77,17 +77,17 @@ tags: ### 方法一:二分查找 + 记忆化搜索 -我们可以先对数组 $\text{power}$ 进行排序,用一个哈希表 $\text{cnt}$ 来记录每个伤害值的出现次数,然后遍历数组 $\text{power}$,对于每个伤害值 $x$,我们可以得出使用伤害值为 $x$ 的咒语时,可以使用的下一个伤害值的索引,即第一个大于 $x + 2$ 的伤害值的索引,我们可以使用二分查找来找到这个索引,记录在数组 $\text{nxt}$ 中。 +我们可以先对数组 $\textit{power}$ 进行排序,用一个哈希表 $\textit{cnt}$ 来记录每个伤害值的出现次数,然后遍历数组 $\textit{power}$,对于每个伤害值 $x$,我们可以得出使用伤害值为 $x$ 的咒语时,可以使用的下一个伤害值的索引,即第一个大于 $x + 2$ 的伤害值的索引,我们可以使用二分查找来找到这个索引,记录在数组 $\textit{nxt}$ 中。 -接下来,我们定义一个函数 $\text{dfs}$,用来计算从第 $i$ 个伤害值开始,可以获得的最大伤害值。 +接下来,我们定义一个函数 $\textit{dfs}$,用来计算从第 $i$ 个伤害值开始,可以获得的最大伤害值。 -在 $\text{dfs}$ 函数中,我们可以选择跳过当前伤害值,那么我们可以跳过当前伤害值的所有相同伤害值,直接跳到 $i + \text{cnt}[x]$,可以获得的伤害值为 $\text{dfs}(i + \text{cnt}[x])$;或者我们可以选择使用当前伤害值,那么我们可以使用当前伤害值的所有相同伤害值,然后跳到下一个伤害值的索引,可以获得的伤害值为 $x \times \text{cnt}[x] + \text{dfs}(\text{nxt}[i])$,其中 $\text{nxt}[i]$ 表示第一个大于 $x + 2$ 的伤害值的索引。我们取这两种情况的最大值作为函数的返回值。 +在 $\textit{dfs}$ 函数中,我们可以选择跳过当前伤害值,那么我们可以跳过当前伤害值的所有相同伤害值,直接跳到 $i + \textit{cnt}[x]$,可以获得的伤害值为 $\textit{dfs}(i + \textit{cnt}[x])$;或者我们可以选择使用当前伤害值,那么我们可以使用当前伤害值的所有相同伤害值,然后跳到下一个伤害值的索引,可以获得的伤害值为 $x \times \textit{cnt}[x] + \textit{dfs}(\textit{nxt}[i])$,其中 $\textit{nxt}[i]$ 表示第一个大于 $x + 2$ 的伤害值的索引。我们取这两种情况的最大值作为函数的返回值。 -为了避免重复计算,我们可以使用记忆化搜索,将已经计算过的结果保存在数组 $\text{f}$ 中,这样在计算 $\text{dfs}(i)$ 时,如果 $\text{f}[i]$ 不为 $0$,则直接返回 $\text{f}[i]$。 +为了避免重复计算,我们可以使用记忆化搜索,将已经计算过的结果保存在数组 $\textit{f}$ 中,这样在计算 $\textit{dfs}(i)$ 时,如果 $\textit{f}[i]$ 不为 $0$,则直接返回 $\textit{f}[i]$。 -答案即为 $\text{dfs}(0)$。 +答案即为 $\textit{dfs}(0)$。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{power}$ 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{power}$ 的长度。 diff --git a/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README_EN.md b/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README_EN.md index 5dd33b6f6c8ec..ba65c0307dfa9 100644 --- a/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README_EN.md +++ b/solution/3100-3199/3186.Maximum Total Damage With Spell Casting/README_EN.md @@ -75,17 +75,17 @@ tags: ### Solution 1: Binary Search + Memoization -We can first sort the array $\text{power}$, use a hash table $\text{cnt}$ to record the occurrence count of each damage value, and then iterate through the array $\text{power}$. For each damage value $x$, we can determine the index of the next damage value that can be used when using a spell with damage value $x$, which is the index of the first damage value greater than $x + 2$. We can use binary search to find this index and record it in the array $\text{nxt}$. +We can first sort the array $\textit{power}$, use a hash table $\textit{cnt}$ to record the occurrence count of each damage value, and then iterate through the array $\textit{power}$. For each damage value $x$, we can determine the index of the next damage value that can be used when using a spell with damage value $x$, which is the index of the first damage value greater than $x + 2$. We can use binary search to find this index and record it in the array $\textit{nxt}$. -Next, we define a function $\text{dfs}$ to calculate the maximum damage value that can be obtained starting from the $i$-th damage value. +Next, we define a function $\textit{dfs}$ to calculate the maximum damage value that can be obtained starting from the $i$-th damage value. -In the $\text{dfs}$ function, we can choose to skip the current damage value, so we can skip all the same damage values of the current one and directly jump to $i + \text{cnt}[x]$, obtaining a damage value of $\text{dfs}(i + \text{cnt}[x])$; or we can choose to use the current damage value, so we can use all the same damage values of the current one and then jump to the index of the next damage value, obtaining a damage value of $x \times \text{cnt}[x] + \text{dfs}(\text{nxt}[i])$, where $\text{nxt}[i]$ represents the index of the first damage value greater than $x + 2$. We take the maximum of these two cases as the return value of the function. +In the $\textit{dfs}$ function, we can choose to skip the current damage value, so we can skip all the same damage values of the current one and directly jump to $i + \textit{cnt}[x]$, obtaining a damage value of $\textit{dfs}(i + \textit{cnt}[x])$; or we can choose to use the current damage value, so we can use all the same damage values of the current one and then jump to the index of the next damage value, obtaining a damage value of $x \times \textit{cnt}[x] + \textit{dfs}(\textit{nxt}[i])$, where $\textit{nxt}[i]$ represents the index of the first damage value greater than $x + 2$. We take the maximum of these two cases as the return value of the function. -To avoid repeated calculations, we can use memoization, storing the results that have already been calculated in an array $\text{f}$. Thus, when calculating $\text{dfs}(i)$, if $\text{f}[i]$ is not $0$, we directly return $\text{f}[i]$. +To avoid repeated calculations, we can use memoization, storing the results that have already been calculated in an array $\textit{f}$. Thus, when calculating $\textit{dfs}(i)$, if $\textit{f}[i]$ is not $0$, we directly return $\textit{f}[i]$. -The answer is $\text{dfs}(0)$. +The answer is $\textit{dfs}(0)$. -The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\text{power}$. +The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{power}$. diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md index f0d4d812eff64..68fe4c358ac70 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README.md @@ -68,9 +68,9 @@ tags: ### 方法一:数学 -我们直接遍历数组 $\text{nums}$,对于每个元素 $x$,我们计算 $x$ 除以 3 的余数 $x \bmod 3$,如果余数不为 0,我们需要将 $x$ 变为能被 3 整除且操作次数最少,那么我们可以选择将 $x$ 减少 $x \bmod 3$ 或者增加 $3 - x \bmod 3$,取两者的最小值累加到答案中。 +我们直接遍历数组 $\textit{nums}$,对于每个元素 $x$,我们计算 $x$ 除以 3 的余数 $x \bmod 3$,如果余数不为 0,我们需要将 $x$ 变为能被 3 整除且操作次数最少,那么我们可以选择将 $x$ 减少 $x \bmod 3$ 或者增加 $3 - x \bmod 3$,取两者的最小值累加到答案中。 -时间复杂度 $O(n)$,其中 $n$ 为数组 $\text{nums}$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md index 200be668ee70a..95003efe76f8d 100644 --- a/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md +++ b/solution/3100-3199/3190.Find Minimum Operations to Make All Elements Divisible by Three/README_EN.md @@ -66,9 +66,9 @@ tags: ### Solution 1: Mathematics -We directly iterate through the array $\text{nums}$. For each element $x$, we calculate the remainder of $x$ divided by 3, $x \bmod 3$. If the remainder is not 0, we need to make $x$ divisible by 3 with the minimum number of operations. Therefore, we can choose to either decrease $x$ by $x \bmod 3$ or increase $x$ by $3 - x \bmod 3$, and we accumulate the minimum of these two values to the answer. +We directly iterate through the array $\textit{nums}$. For each element $x$, we calculate the remainder of $x$ divided by 3, $x \bmod 3$. If the remainder is not 0, we need to make $x$ divisible by 3 with the minimum number of operations. Therefore, we can choose to either decrease $x$ by $x \bmod 3$ or increase $x$ by $3 - x \bmod 3$, and we accumulate the minimum of these two values to the answer. -The time complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. diff --git a/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README.md b/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README.md index aaf96006e839f..1e2917435d4bb 100644 --- a/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README.md +++ b/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README.md @@ -85,7 +85,7 @@ tags: 遍历结束后,返回答案即可。 -时间复杂度 $O(n)$,其中 $n$ 为数组 $\text{nums}$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README_EN.md b/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README_EN.md index 3aba89da6e116..24960403e2ffb 100644 --- a/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README_EN.md +++ b/solution/3100-3199/3191.Minimum Operations to Make Binary Array Elements Equal to One I/README_EN.md @@ -83,7 +83,7 @@ We notice that the first position in the array that is $0$ must undergo a flip o After the traversal, we return the answer. -The time complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. diff --git a/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README.md b/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README.md index 42b9bff2e2266..aa576122e8ecc 100644 --- a/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README.md +++ b/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README.md @@ -86,11 +86,11 @@ tags: 我们注意到,每当我们将某个位置的元素变为 1 时,它的右侧的所有元素都会被反转。因此,我们可以用一个变量 $v$ 来记录当前位置及其右侧的元素是否被反转,如果被反转,那么 $v$ 的值为 1,否则为 0。 -我们遍历数组 $\text{nums}$,对于每个元素 $x$,我们将 $x$ 与 $v$ 进行异或运算,如果 $x$ 为 0,那么我们需要将 $x$ 变为 1,我们需要进行反转操作,我们将答案加一,并将 $v$ 取反。 +我们遍历数组 $\textit{nums}$,对于每个元素 $x$,我们将 $x$ 与 $v$ 进行异或运算,如果 $x$ 为 0,那么我们需要将 $x$ 变为 1,我们需要进行反转操作,我们将答案加一,并将 $v$ 取反。 遍历结束后,我们就可以得到最少操作次数。 -时间复杂度 $O(n)$,其中 $n$ 为数组 $\text{nums}$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README_EN.md b/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README_EN.md index b7cb922014b0e..0342e644a6bc6 100644 --- a/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README_EN.md +++ b/solution/3100-3199/3192.Minimum Operations to Make Binary Array Elements Equal to One II/README_EN.md @@ -84,11 +84,11 @@ We can do the following operation:

We notice that whenever we change an element at a certain position to 1, all the elements to its right are flipped. Therefore, we can use a variable $v$ to record whether the current position and all elements to its right have been flipped. If flipped, the value of $v$ is 1, otherwise, it is 0. -We iterate through the array $\text{nums}$. For each element $x$, we perform an XOR operation between $x$ and $v$. If $x$ is 0, then we need to change $x$ to 1, which requires a flip operation. We increment the answer by one and flip the value of $v$. +We iterate through the array $\textit{nums}$. For each element $x$, we perform an XOR operation between $x$ and $v$. If $x$ is 0, then we need to change $x$ to 1, which requires a flip operation. We increment the answer by one and flip the value of $v$. After the iteration, we can obtain the minimum number of operations. -The time complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$. The space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. diff --git a/solution/3100-3199/3193.Count the Number of Inversions/README.md b/solution/3100-3199/3193.Count the Number of Inversions/README.md index 94adf2ad6f097..8d939c1912e37 100644 --- a/solution/3100-3199/3193.Count the Number of Inversions/README.md +++ b/solution/3100-3199/3193.Count the Number of Inversions/README.md @@ -129,7 +129,7 @@ $$ f[i][j] = \sum_{k=0}^{\min(i, j)} f[i-1][j-k] $$ -由于题目要求 $[0..\text{end}_i]$ 的逆序对数量为 $\text{cnt}_i$,因此,当我们计算 $i = \text{end}_i$ 时,我们只需要计算 $f[i][\text{cnt}_i]$ 即可。其余的 $f[i][..]$ 都为 $0$。 +由于题目要求 $[0..\textit{end}_i]$ 的逆序对数量为 $\textit{cnt}_i$,因此,当我们计算 $i = \textit{end}_i$ 时,我们只需要计算 $f[i][\textit{cnt}_i]$ 即可。其余的 $f[i][..]$ 都为 $0$。 时间复杂度 $O(n \times m \times \min(n, m))$,空间复杂度 $O(n \times m)$。其中 $m$ 是逆序对数量的最大值。本题中 $m \le 400$。 diff --git a/solution/3100-3199/3193.Count the Number of Inversions/README_EN.md b/solution/3100-3199/3193.Count the Number of Inversions/README_EN.md index ca4c899e1d34e..1f423e476ea9a 100644 --- a/solution/3100-3199/3193.Count the Number of Inversions/README_EN.md +++ b/solution/3100-3199/3193.Count the Number of Inversions/README_EN.md @@ -123,7 +123,7 @@ $$ f[i][j] = \sum_{k=0}^{\min(i, j)} f[i-1][j-k] $$ -Since the problem requires the number of inversions in $[0..\text{end}_i]$ to be $\text{cnt}_i$, when we calculate for $i = \text{end}_i$, we only need to compute $f[i][\text{cnt}_i]$. The rest of $f[i][..]$ will be $0$. +Since the problem requires the number of inversions in $[0..\textit{end}_i]$ to be $\textit{cnt}_i$, when we calculate for $i = \textit{end}_i$, we only need to compute $f[i][\textit{cnt}_i]$. The rest of $f[i][..]$ will be $0$. The time complexity is $O(n \times m \times \min(n, m))$, and the space complexity is $O(n \times m)$. Here, $m$ is the maximum number of inversions, and in this problem, $m \le 400$. diff --git a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md index 30bde6ae8b2fe..77387f14b8b53 100644 --- a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md +++ b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md @@ -176,9 +176,9 @@ tags: ### 方法一:排序 -我们首先对数组 $\text{nums}$ 进行排序,然后从数组的两端开始取元素,分别计算两个元素的和,取最小值。最后将最小值除以 2 作为答案返回即可。 +我们首先对数组 $\textit{nums}$ 进行排序,然后从数组的两端开始取元素,分别计算两个元素的和,取最小值。最后将最小值除以 2 作为答案返回即可。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 diff --git a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md index eed9d1aaf71e1..98cb91d17c98f 100644 --- a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md +++ b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md @@ -174,9 +174,9 @@ The smallest element of averages, 5.5, is returned. ### Solution 1: Sorting -First, we sort the array $\text{nums}$. Then, we start taking elements from both ends of the array, calculating the sum of the two elements, and taking the minimum value. Finally, we return the minimum value divided by 2 as the answer. +First, we sort the array $\textit{nums}$. Then, we start taking elements from both ends of the array, calculating the sum of the two elements, and taking the minimum value. Finally, we return the minimum value divided by 2 as the answer. -The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\text{nums}$. +The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{nums}$. diff --git a/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README.md b/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README.md index 7796704108b56..3670c65248a33 100644 --- a/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README.md +++ b/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README.md @@ -104,12 +104,12 @@ tags: 根据题目描述,如果当前数没取反,那么下一个可以取反,也可以不取反;如果当前数取反了,那么下一个只能不取反。 -因此,我们定义一个函数 $\text{dfs}(i, j)$,表示从第 $i$ 个数开始,第 $i$ 个数是否能取反,其中 $j$ 表示第 $i$ 个数是否取反。如果 $j = 0$,表示第 $i$ 个数不能取反,否则可以取反。答案为 $\text{dfs}(0, 0)$。 +因此,我们定义一个函数 $\textit{dfs}(i, j)$,表示从第 $i$ 个数开始,第 $i$ 个数是否能取反,其中 $j$ 表示第 $i$ 个数是否取反。如果 $j = 0$,表示第 $i$ 个数不能取反,否则可以取反。答案为 $\textit{dfs}(0, 0)$。 函数 $dfs(i, j)$ 的执行过程如下: -- 如果 $i \geq \text{len}(nums)$,表示已经遍历完了数组,返回 $0$; -- 否则,第 $i$ 个数可以不取反,此时答案为 $nums[i] + \text{dfs}(i + 1, 1)$;如果 $j = 1$,表示第 $i$ 个数可以取反,此时答案为 $\max(\text{dfs}(i + 1, 0) - nums[i])$。我们取两者的最大值即可。 +- 如果 $i \geq \textit{len}(nums)$,表示已经遍历完了数组,返回 $0$; +- 否则,第 $i$ 个数可以不取反,此时答案为 $nums[i] + \textit{dfs}(i + 1, 1)$;如果 $j = 1$,表示第 $i$ 个数可以取反,此时答案为 $\max(\textit{dfs}(i + 1, 0) - nums[i])$。我们取两者的最大值即可。 为了避免重复计算,我们可以使用记忆化搜索,将已经计算过的结果保存起来。 diff --git a/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README_EN.md b/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README_EN.md index 824bb8ef8043b..17ac12706d72f 100644 --- a/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README_EN.md +++ b/solution/3100-3199/3196.Maximize Total Cost of Alternating Subarrays/README_EN.md @@ -102,12 +102,12 @@ tags: Based on the problem description, if the current number has not been flipped, then the next one can either be flipped or not flipped; if the current number has been flipped, then the next one can only remain unflipped. -Therefore, we define a function $\text{dfs}(i, j)$, which represents starting from the $i$-th number, whether the $i$-th number can be flipped, where $j$ indicates whether the $i$-th number is flipped. If $j = 0$, it means the $i$-th number cannot be flipped, otherwise, it can be flipped. The answer is $\text{dfs}(0, 0)$. +Therefore, we define a function $\textit{dfs}(i, j)$, which represents starting from the $i$-th number, whether the $i$-th number can be flipped, where $j$ indicates whether the $i$-th number is flipped. If $j = 0$, it means the $i$-th number cannot be flipped, otherwise, it can be flipped. The answer is $\textit{dfs}(0, 0)$. The execution process of the function $dfs(i, j)$ is as follows: -- If $i \geq \text{len}(nums)$, it means the array has been fully traversed, return $0$; -- Otherwise, the $i$-th number can remain unflipped, in which case the answer is $nums[i] + \text{dfs}(i + 1, 1)$; if $j = 1$, it means the $i$-th number can be flipped, in which case the answer is $\max(\text{dfs}(i + 1, 0) - nums[i])$. We take the maximum of the two. +- If $i \geq \textit{len}(nums)$, it means the array has been fully traversed, return $0$; +- Otherwise, the $i$-th number can remain unflipped, in which case the answer is $nums[i] + \textit{dfs}(i + 1, 1)$; if $j = 1$, it means the $i$-th number can be flipped, in which case the answer is $\max(\textit{dfs}(i + 1, 0) - nums[i])$. We take the maximum of the two. To avoid repeated calculations, we can use memoization to save the results that have already been computed. diff --git a/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README.md b/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README.md index ab5fbc4777655..acce2c72fabd0 100644 --- a/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README.md +++ b/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README.md @@ -93,7 +93,7 @@ tags: 1. 先进行一次纵向分割,再对左部分进行一次横向分割 1. 先进行一次纵向分割,再对右部分进行一次横向分割 -我们可以用一个函数 $\text{f}(i_1, j_1, i_2, j_2)$ 来计算矩形 $(i_1, j_1)$ 到 $(i_2, j_2)$ 包含所有 $1$ 的最小矩形面积。 +我们可以用一个函数 $\textit{f}(i_1, j_1, i_2, j_2)$ 来计算矩形 $(i_1, j_1)$ 到 $(i_2, j_2)$ 包含所有 $1$ 的最小矩形面积。 时间复杂度 $O(m^2 \times n^2)$,其中 $m$ 和 $n$ 分别是矩形的行数和列数。空间复杂度 $O(1)$。 diff --git a/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README_EN.md b/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README_EN.md index 016dc22bcda93..8ca1f29792f59 100644 --- a/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README_EN.md +++ b/solution/3100-3199/3197.Find the Minimum Area to Cover All Ones II/README_EN.md @@ -91,7 +91,7 @@ We can enumerate the positions of the two dividing lines, which have $6$ possibi 5. First perform a vertical split, then a horizontal split on the left part 6. First perform a vertical split, then a horizontal split on the right part -We can use a function $\text{f}(i_1, j_1, i_2, j_2)$ to calculate the minimum rectangular area containing all $1$s from $(i_1, j_1)$ to $(i_2, j_2)$. +We can use a function $\textit{f}(i_1, j_1, i_2, j_2)$ to calculate the minimum rectangular area containing all $1$s from $(i_1, j_1)$ to $(i_2, j_2)$. The time complexity is $O(m^2 \times n^2)$, where $m$ and $n$ are the number of rows and columns of the rectangle, respectively. The space complexity is $O(1)$. diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md index 16c45d92dee36..e13927dcf72ac 100644 --- a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README.md @@ -96,7 +96,7 @@ tags: 答案为所有 $f[x][y]$ 中的最大值。 -时间复杂度 $O(n \times k)$,空间复杂度 $O(k^2)$。其中 $n$ 为数组 $\text{nums}$ 的长度,而 $k=2$。 +时间复杂度 $O(n \times k)$,空间复杂度 $O(k^2)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $k=2$。 diff --git a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md index 5467bc82d0318..44454ee1cdb72 100644 --- a/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md +++ b/solution/3200-3299/3201.Find the Maximum Length of Valid Subsequence I/README_EN.md @@ -94,7 +94,7 @@ Iterate through the array $nums$, and for each number $x$, we get $x = x \bmod k The answer is the maximum value among all $f[x][y]$. -The time complexity is $O(n \times k)$, and the space complexity is $O(k^2)$. Here, $n$ is the length of the array $\text{nums}$, and $k=2$. +The time complexity is $O(n \times k)$, and the space complexity is $O(k^2)$. Here, $n$ is the length of the array $\textit{nums}$, and $k=2$. diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md index 08c640f048251..46d2c442bb8f2 100644 --- a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README.md @@ -80,7 +80,7 @@ tags: 答案为所有 $f[x][y]$ 中的最大值。 -时间复杂度 $O(n \times k)$,空间复杂度 $O(k^2)$。其中 $n$ 为数组 $\text{nums}$ 的长度,而 $k$ 为给定的正整数。 +时间复杂度 $O(n \times k)$,空间复杂度 $O(k^2)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $k$ 为给定的正整数。 #### Python3 diff --git a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md index 13f63d3c7700b..d95fb4e4de202 100644 --- a/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md +++ b/solution/3200-3299/3202.Find the Maximum Length of Valid Subsequence II/README_EN.md @@ -77,7 +77,7 @@ Iterate through the array $nums$, and for each number $x$, we get $x = x \bmod k The answer is the maximum value among all $f[x][y]$. -The time complexity is $O(n \times k)$, and the space complexity is $O(k^2)$. Here, $n$ is the length of the array $\text{nums}$, and $k$ is the given positive integer. +The time complexity is $O(n \times k)$, and the space complexity is $O(k^2)$. Here, $n$ is the length of the array $\textit{nums}$, and $k$ is the given positive integer. diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md b/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md index 4ab230fa68b32..c37847f6587bd 100644 --- a/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md @@ -74,13 +74,13 @@ tags: ### 方法一:记忆化搜索 -我们设计一个函数 $\text{dfs}(i)$,表示从下标 $i$ 出发,能够获得的最大分数。那么答案就是 $\text{dfs}(0)$。 +我们设计一个函数 $\textit{dfs}(i)$,表示从下标 $i$ 出发,能够获得的最大分数。那么答案就是 $\textit{dfs}(0)$。 -函数 $\text{dfs}(i)$ 的执行过程如下: +函数 $\textit{dfs}(i)$ 的执行过程如下: -我们枚举下一个跳跃的位置 $j$,那么从下标 $i$ 出发,能够获得的分数就是 $(j - i) \times \text{nums}[j]$,加上从下标 $j$ 出发,能够获得的最大分数,总分数就是 $(j - i) \times \text{nums}[j] + \text{dfs}(j)$。我们枚举所有的 $j$,取分数的最大值即可。 +我们枚举下一个跳跃的位置 $j$,那么从下标 $i$ 出发,能够获得的分数就是 $(j - i) \times \textit{nums}[j]$,加上从下标 $j$ 出发,能够获得的最大分数,总分数就是 $(j - i) \times \textit{nums}[j] + \textit{dfs}(j)$。我们枚举所有的 $j$,取分数的最大值即可。 -为了避免重复计算,我们使用记忆化搜索的方法,将已经计算过的 $\text{dfs}(i)$ 的值保存起来,下次直接返回即可。 +为了避免重复计算,我们使用记忆化搜索的方法,将已经计算过的 $\textit{dfs}(i)$ 的值保存起来,下次直接返回即可。 时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 @@ -204,7 +204,7 @@ function maxScore(nums: number[]): number { 状态转移方程为: $$ -f[j] = \max_{0 \leq i < j} \{ f[i] + (j - i) \times \text{nums}[j] \} +f[j] = \max_{0 \leq i < j} \{ f[i] + (j - i) \times \textit{nums}[j] \} $$ 时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 @@ -299,11 +299,11 @@ function maxScore(nums: number[]): number { 我们观察发现,对于当前位置 $i$,我们应该跳到下一个值最大的位置 $j$,这样才能获得最大的分数。 -因此,我们遍历数组 $\text{nums}$,维护一个从栈底到栈顶单调递减的栈 $\text{stk}$。对于当前遍历到的位置 $i$,如果栈顶元素对应的值小于等于 $\text{nums}[i]$,我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素对应的值大于 $\text{nums}[i]$,然后将 $i$ 入栈。 +因此,我们遍历数组 $\textit{nums}$,维护一个从栈底到栈顶单调递减的栈 $\textit{stk}$。对于当前遍历到的位置 $i$,如果栈顶元素对应的值小于等于 $\textit{nums}[i]$,我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素对应的值大于 $\textit{nums}[i]$,然后将 $i$ 入栈。 -然后,我们初始化答案 $\text{ans}$ 和当前位置 $i = 0$,遍历栈中的元素,每次取出栈顶元素 $j$,更新答案 $\text{ans} += \text{nums}[j] \times (j - i)$,然后更新 $i = j$。 +然后,我们初始化答案 $\textit{ans}$ 和当前位置 $i = 0$,遍历栈中的元素,每次取出栈顶元素 $j$,更新答案 $\textit{ans} += \textit{nums}[j] \times (j - i)$,然后更新 $i = j$。 -最后返回答案 $\text{ans}$。 +最后返回答案 $\textit{ans}$。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md b/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md index e790f7c90ee5a..1ce14013d7b5e 100644 --- a/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md @@ -72,13 +72,13 @@ tags: ### Solution 1: Memoization Search -We design a function $\text{dfs}(i)$, which represents the maximum score that can be obtained starting from index $i$. Therefore, the answer is $\text{dfs}(0)$. +We design a function $\textit{dfs}(i)$, which represents the maximum score that can be obtained starting from index $i$. Therefore, the answer is $\textit{dfs}(0)$. -The execution process of the function $\text{dfs}(i)$ is as follows: +The execution process of the function $\textit{dfs}(i)$ is as follows: -We enumerate the next jump position $j$. Thus, the score that can be obtained starting from index $i$ is $(j - i) \times \text{nums}[j]$, plus the maximum score that can be obtained starting from index $j$, making the total score $(j - i) \times \text{nums}[j] + \text{dfs}(j)$. We enumerate all possible $j$ and take the maximum score. +We enumerate the next jump position $j$. Thus, the score that can be obtained starting from index $i$ is $(j - i) \times \textit{nums}[j]$, plus the maximum score that can be obtained starting from index $j$, making the total score $(j - i) \times \textit{nums}[j] + \textit{dfs}(j)$. We enumerate all possible $j$ and take the maximum score. -To avoid redundant calculations, we use memoization search. We save the calculated value of $\text{dfs}(i)$, so it can be directly returned next time. +To avoid redundant calculations, we use memoization search. We save the calculated value of $\textit{dfs}(i)$, so it can be directly returned next time. The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. @@ -202,7 +202,7 @@ Define $f[j]$ as the maximum score that can be obtained starting from index $0$ The state transition equation is: $$ -f[j] = \max_{0 \leq i < j} \{ f[i] + (j - i) \times \text{nums}[j] \} +f[j] = \max_{0 \leq i < j} \{ f[i] + (j - i) \times \textit{nums}[j] \} $$ The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. @@ -297,11 +297,11 @@ function maxScore(nums: number[]): number { We observe that for the current position $i$, we should jump to the next position $j$ with the maximum value to obtain the maximum score. -Therefore, we traverse the array $\text{nums}$, maintaining a stack $\text{stk}$ that is monotonically decreasing from the bottom to the top of the stack. For the current position $i$ being traversed, if the value corresponding to the top element of the stack is less than or equal to $\text{nums}[i]$, we continuously pop the top element of the stack until the stack is empty or the value corresponding to the top element of the stack is greater than $\text{nums}[i]$, and then push $i$ into the stack. +Therefore, we traverse the array $\textit{nums}$, maintaining a stack $\textit{stk}$ that is monotonically decreasing from the bottom to the top of the stack. For the current position $i$ being traversed, if the value corresponding to the top element of the stack is less than or equal to $\textit{nums}[i]$, we continuously pop the top element of the stack until the stack is empty or the value corresponding to the top element of the stack is greater than $\textit{nums}[i]$, and then push $i$ into the stack. -Next, we initialize the answer $\text{ans}$ and the current position $i = 0$, traverse the elements in the stack, each time taking out the top element $j$, updating the answer $\text{ans} += \text{nums}[j] \times (j - i)$, and then updating $i = j$. +Next, we initialize the answer $\textit{ans}$ and the current position $i = 0$, traverse the elements in the stack, each time taking out the top element $j$, updating the answer $\textit{ans} += \textit{nums}[j] \times (j - i)$, and then updating $i = j$. -Finally, return the answer $\text{ans}$. +Finally, return the answer $\textit{ans}$. The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md b/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md index 3d515cc38f08c..992af95816864 100644 --- a/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md @@ -73,11 +73,11 @@ tags: 我们观察发现,对于当前位置 $i$,我们应该跳到下一个值最大的位置 $j$,这样才能获得最大的分数。 -因此,我们遍历数组 $\text{nums}$,维护一个从栈底到栈顶单调递减的栈 $\text{stk}$。对于当前遍历到的位置 $i$,如果栈顶元素对应的值小于等于 $\text{nums}[i]$,我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素对应的值大于 $\text{nums}[i]$,然后将 $i$ 入栈。 +因此,我们遍历数组 $\textit{nums}$,维护一个从栈底到栈顶单调递减的栈 $\textit{stk}$。对于当前遍历到的位置 $i$,如果栈顶元素对应的值小于等于 $\textit{nums}[i]$,我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素对应的值大于 $\textit{nums}[i]$,然后将 $i$ 入栈。 -然后,我们初始化答案 $\text{ans}$ 和当前位置 $i = 0$,遍历栈中的元素,每次取出栈顶元素 $j$,更新答案 $\text{ans} += \text{nums}[j] \times (j - i)$,然后更新 $i = j$。 +然后,我们初始化答案 $\textit{ans}$ 和当前位置 $i = 0$,遍历栈中的元素,每次取出栈顶元素 $j$,更新答案 $\textit{ans} += \textit{nums}[j] \times (j - i)$,然后更新 $i = j$。 -最后返回答案 $\text{ans}$。 +最后返回答案 $\textit{ans}$。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md b/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md index e19680963ad34..6fa27a4696492 100644 --- a/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md @@ -73,9 +73,9 @@ tags: We observe that for the current position $i$, we should jump to the next position $j$ with the maximum value to obtain the maximum score. -Therefore, we traverse the array $\text{nums}$, maintaining a stack $\text{stk}$ that is monotonically decreasing from the bottom to the top of the stack. For the current position $i$ being traversed, if the value corresponding to the top element of the stack is less than or equal to $\text{nums}[i]$, we continuously pop the top element of the stack until the stack is empty or the value corresponding to the top element of the stack is greater than $\text{nums}[i]$, and then push $i$ into the stack. +Therefore, we traverse the array $\textit{nums}$, maintaining a stack $\textit{stk}$ that is monotonically decreasing from the bottom to the top of the stack. For the current position $i$ being traversed, if the value corresponding to the top element of the stack is less than or equal to $\textit{nums}[i]$, we continuously pop the top element of the stack until the stack is empty or the value corresponding to the top element of the stack is greater than $\textit{nums}[i]$, and then push $i$ into the stack. -Next, we initialize the answer $\text{ans}$ and the current position $i = 0$, traverse the elements in the stack, each time taking out the top element $j$, updating the answer $\text{ans} += \text{nums}[j] \times (j - i)$, and then updating $i = j$. +Next, we initialize the answer $\textit{ans}$ and the current position $i = 0$, traverse the elements in the stack, each time taking out the top element $j$, updating the answer $\textit{ans} += \textit{nums}[j] \times (j - i)$, and then updating $i = j$.