Skip to content

feat: update lc problems #3833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions solution/0800-0899/0805.Split Array With Same Average/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ tags:

### 方法一:折半查找 + 二进制枚举

根据题目要求,要判断是否可以将数组 `nums` 划分为两个子数组 $A$ 和 $B$,使得两个子数组的平均值相等。
根据题目要求,要判断是否可以将数组 $\textit{nums}$ 划分为两个子数组 $A$ 和 $B$,使得两个子数组的平均值相等。

我们记数组 `nums` 的和为 $s$,元素个数为 $n$。子数组 $A$ 的和以及个数分别为 $s_1$ 和 $k$,那么子数组 $B$ 的和为 $s_2 = s - s_1$,个数为 $n - k$,即:
我们记数组 $\textit{nums}$ 的和为 $s$,元素个数为 $n$。子数组 $A$ 的和以及个数分别为 $s_1$ 和 $k$,那么子数组 $B$ 的和为 $s_2 = s - s_1$,个数为 $n - k$,即:

$$
\frac{s_1}{k} = \frac{s_2}{n - k} = \frac{s-s_1}{n-k}
Expand All @@ -82,25 +82,25 @@ $$
\frac{s_1}{k} = \frac{s}{n}
$$

也就是说,要我们找出一个子数组 $A$,使得其平均值等于数组 `nums` 的平均值。我们考虑将数组 `nums` 每个元素都减去数组 `nums` 的平均值,这样问题就转化为了在数组 `nums` 中找出一个子数组,使得其和为 $0$。
也就是说,要我们找出一个子数组 $A$,使得其平均值等于数组 $\textit{nums}$ 的平均值。我们考虑将数组 $\textit{nums}$ 每个元素都减去数组 $\textit{nums}$ 的平均值,这样问题就转化为了在数组 $\textit{nums}$ 中找出一个子数组,使得其和为 $0$。

但是,数组 `nums` 的平均值可能不是整数,浮点数计算可能存在精度问题,我们不妨将数组 `nums` 中的每个元素都乘以 $n$,即 $nums[i] \leftarrow nums[i] \times n$,上述式子就变成:
但是,数组 $\textit{nums}$ 的平均值可能不是整数,浮点数计算可能存在精度问题,我们不妨将数组 $\textit{nums}$ 中的每个元素都乘以 $n$,即 $nums[i] \leftarrow nums[i] \times n$,上述式子就变成:

$$
\frac{s_1\times n}{k} = s
$$

此时我们将数组 `nums` 中每个元素都减去整数 $s$,题目就转化为:在数组 $nums$ 中找出一个子数组 $A$,使得其和为 $0$。
此时我们将数组 $\textit{nums}$ 中每个元素都减去整数 $s$,题目就转化为:在数组 $nums$ 中找出一个子数组 $A$,使得其和为 $0$。

数组 `nums` 的长度范围为 $[1, 30]$,如果我们使用暴力枚举子数组的方法,时间复杂度为 $O(2^n)$,会超时。我们可以使用折半查找的方法,将时间复杂度降低到 $O(2^{n/2})$。
数组 $\textit{nums}$ 的长度范围为 $[1, 30]$,如果我们使用暴力枚举子数组的方法,时间复杂度为 $O(2^n)$,会超时。我们可以使用折半查找的方法,将时间复杂度降低到 $O(2^{n/2})$。

我们将数组 `nums` 分成左右两部分,那么子数组 $A$ 可能存在三种情况:
我们将数组 $\textit{nums}$ 分成左右两部分,那么子数组 $A$ 可能存在三种情况:

1. 子数组 $A$ 完全在数组 `nums` 的左半部分;
2. 子数组 $A$ 完全在数组 `nums` 的右半部分;
3. 子数组 $A$ 一部分在数组 `nums` 的左半部分,一部分在数组 `nums` 的右半部分。
1. 子数组 $A$ 完全在数组 $\textit{nums}$ 的左半部分;
2. 子数组 $A$ 完全在数组 $\textit{nums}$ 的右半部分;
3. 子数组 $A$ 一部分在数组 $\textit{nums}$ 的左半部分,一部分在数组 $\textit{nums}$ 的右半部分。

我们可以使用二进制枚举的方法,先枚举左半部分所有子数组的和,如果存在一个子数组和为 $0$,直接返回 `true`,否则我们将其存入哈希表 `vis` 中;然后枚举右半部分所有子数组的和,如果存在一个子数组和为 $0$,直接返回 `true`,否则我们判断此时哈希表 `vis` 中是否存在该和的相反数,如果存在,直接返回 `true`。
我们可以使用二进制枚举的方法,先枚举左半部分所有子数组的和,如果存在一个子数组和为 $0$,直接返回 `true`,否则我们将其存入哈希表 $\textit{vis}$ 中;然后枚举右半部分所有子数组的和,如果存在一个子数组和为 $0$,直接返回 `true`,否则我们判断此时哈希表 $\textit{vis}$ 中是否存在该和的相反数,如果存在,直接返回 `true`。

需要注意的是,我们不能同时全选左半部分和右半部分,因为这样会导致子数组 $B$ 为空,这是不符合题意的。在实现上,我们只需要考虑数组的 $n-1$ 个数。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,51 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Binary Search + Binary Enumeration

According to the problem requirements, we need to determine if the array $\textit{nums}$ can be divided into two subarrays $A$ and $B$ such that the average values of the two subarrays are equal.

Let the sum of the array $\textit{nums}$ be $s$, and the number of elements be $n$. The sum and number of elements of subarray $A$ are $s_1$ and $k$, respectively. Then the sum of subarray $B$ is $s_2 = s - s_1$, and the number of elements is $n - k$. Thus:

$$
\frac{s_1}{k} = \frac{s_2}{n - k} = \frac{s-s_1}{n-k}
$$

Rearranging, we get:

$$
s_1 \times (n-k) = (s-s_1) \times k
$$

Simplifying, we get:

$$
\frac{s_1}{k} = \frac{s}{n}
$$

This means we need to find a subarray $A$ such that its average value equals the average value of the array $\textit{nums}$. We consider subtracting the average value of the array $\textit{nums}$ from each element of the array $\textit{nums}$, transforming the problem into finding a subarray in the array $\textit{nums}$ whose sum is $0$.

However, the average value of the array $\textit{nums}$ may not be an integer, and floating-point calculations may have precision issues. We can multiply each element of the array $\textit{nums}$ by $n$, i.e., $nums[i] \leftarrow nums[i] \times n$. The above equation becomes:

$$
\frac{s_1\times n}{k} = s
$$

Now, we subtract the integer $s$ from each element of the array $\textit{nums}$, transforming the problem into finding a subarray $A$ in the array $nums$ whose sum is $0$.

The length of the array $\textit{nums}$ ranges from $[1, 30]$. If we use brute force to enumerate subarrays, the time complexity is $O(2^n)$, which will time out. We can use binary search to reduce the time complexity to $O(2^{n/2})$.

We divide the array $\textit{nums}$ into left and right parts. The subarray $A$ can exist in three cases:

1. Subarray $A$ is entirely in the left part of the array $\textit{nums}$;
2. Subarray $A$ is entirely in the right part of the array $\textit{nums}$;
3. Subarray $A$ is partially in the left part and partially in the right part of the array $\textit{nums}$.

We can use binary enumeration to first enumerate the sums of all subarrays in the left part. If there is a subarray with a sum of $0$, we return `true` immediately. Otherwise, we store the sums in a hash table $\textit{vis}$. Then we enumerate the sums of all subarrays in the right part. If there is a subarray with a sum of $0$, we return `true` immediately. Otherwise, we check if the hash table $\textit{vis}$ contains the opposite of the current sum. If it does, we return `true`.

Note that we cannot select all elements from both the left and right parts simultaneously, as this would leave subarray $B$ empty, which does not meet the problem requirements. In implementation, we only need to consider $n-1$ elements of the array.

The time complexity is $O(n \times 2^{\frac{n}{2}})$, and the space complexity is $O(2^{\frac{n}{2}})$.

<!-- tabs:start -->

Expand Down
27 changes: 26 additions & 1 deletion solution/0800-0899/0808.Soup Servings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,32 @@ So the total probability of A becoming empty first plus half the probability tha

<!-- solution:start -->

### Solution 1
### Solution 1: Memoization Search

In this problem, since each operation is a multiple of $25$, we can consider every $25ml$ of soup as one unit. This reduces the data scale to $\left \lceil \frac{n}{25} \right \rceil$.

We design a function $dfs(i, j)$, which represents the probability result when there are $i$ units of soup $A$ and $j$ units of soup $B$ remaining.

When $i \leq 0$ and $j \leq 0$, it means both soups are finished, and we should return $0.5$. When $i \leq 0$, it means soup $A$ is finished first, and we should return $1$. When $j \leq 0$, it means soup $B$ is finished first, and we should return $0$.

Next, for each operation, we have four choices:

- Take $4$ units from soup $A$ and $0$ units from soup $B$;
- Take $3$ units from soup $A$ and $1$ unit from soup $B$;
- Take $2$ units from soup $A$ and $2$ units from soup $B$;
- Take $1$ unit from soup $A$ and $3$ units from soup $B$.

Each choice has a probability of $0.25$, so we can derive:

$$
dfs(i, j) = 0.25 \times (dfs(i - 4, j) + dfs(i - 3, j - 1) + dfs(i - 2, j - 2) + dfs(i - 1, j - 3))
$$

We use memoization to store the results of the function.

Additionally, we find that when $n=4800$, the result is $0.999994994426$, and the required precision is $10^{-5}$. As $n$ increases, the result gets closer to $1$. Therefore, when $n \gt 4800$, we can directly return $1$.

The time complexity is $O(C^2)$, and the space complexity is $O(C^2)$. In this problem, $C=200$.

<!-- tabs:start -->

Expand Down
14 changes: 7 additions & 7 deletions solution/0800-0899/0809.Expressive Words/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<p><strong>示例:</strong></p>

<pre>
<strong>输入:</strong>
<strong>输入:</strong>
s = "heeellooo"
words = ["hello", "hi", "helo"]
<strong>输出:</strong>1
Expand All @@ -58,17 +58,17 @@ words = ["hello", "hi", "helo"]

### 方法一:遍历计数 + 双指针

我们可以遍历数组 `words`,对于数组中的每个单词 $t$,判断 $t$ 是否可以通过扩张得到 $s$,如果可以,那么答案加一。
我们可以遍历数组 $\textit{words}$,对于数组中的每个单词 $t$,判断 $t$ 是否可以通过扩张得到 $s$,如果可以,那么答案加一。

因此,问题的关键在于判断单词 $t$ 是否可以通过扩张得到 $s$。这里我们通过一个 $check(s, t)$ 函数来判断。函数的具体实现逻辑如下:
因此,问题的关键在于判断单词 $t$ 是否可以通过扩张得到 $s$。这里我们通过一个 $\textit{check}(s, t)$ 函数来判断。函数的具体实现逻辑如下:

首先判断 $s$ 和 $t$ 的长度关系,如果 $t$ 的长度大于 $s$,直接返回 `false`;否则,我们用双指针 $i$ 和 $j$ 分别指向 $s$ 和 $t$,初始时 $i$ 和 $j$ 的值均为 $0$。
首先判断 $s$ 和 $t$ 的长度关系,如果 $t$ 的长度大于 $s$,直接返回 $\textit{false}$;否则,我们用双指针 $i$ 和 $j$ 分别指向 $s$ 和 $t$,初始时 $i$ 和 $j$ 的值均为 $0$。

如果 $i$ 和 $j$ 指向的字符不同,那么 $t$ 无法通过扩张得到 $s$,直接返回 `false`;否则,我们需要判断 $i$ 指向的字符的连续出现次数 $c_1$ 和 $j$ 指向的字符的连续出现次数 $c_2$ 的关系。如果 $c_1 \lt c_2$ 或者 $c_1 \lt 3$ 并且 $c_1 \neq c_2$,那么 $t$ 无法通过扩张得到 $s$,直接返回 `false`;否则,将 $i$ 和 $j$ 分别右移 $c_1$ 和 $c_2$ 次。继续判断。
如果 $i$ 和 $j$ 指向的字符不同,那么 $t$ 无法通过扩张得到 $s$,直接返回 $\textit{false}$;否则,我们需要判断 $i$ 指向的字符的连续出现次数 $c_1$ 和 $j$ 指向的字符的连续出现次数 $c_2$ 的关系。如果 $c_1 \lt c_2$ 或者 $c_1 \lt 3$ 并且 $c_1 \neq c_2$,那么 $t$ 无法通过扩张得到 $s$,直接返回 $\textit{false}$;否则,将 $i$ 和 $j$ 分别右移 $c_1$ 和 $c_2$ 次。继续判断。

如果 $i$ 和 $j$ 都到达了字符串的末尾,那么 $t$ 可以通过扩张得到 $s$,返回 `true`,否则返回 `false`
如果 $i$ 和 $j$ 都到达了字符串的末尾,那么 $t$ 可以通过扩张得到 $s$,返回 $\textit{true}$,否则返回 $\textit{false}$

时间复杂度 $O(n \times m + \sum_{i=0}^{m-1} w_i)$,其中 $n$ 和 $m$ 分别为字符串 $s$ 和数组 $words$ 的长度,而 $w_i$ 为数组 $words$ 中第 $i$ 个单词的长度。
时间复杂度 $O(n \times m + \sum_{i=0}^{m-1} w_i)$,其中 $n$ 和 $m$ 分别为字符串 $s$ 和数组 $\textit{words}$ 的长度,而 $w_i$ 为数组 $\textit{words}$ 中第 $i$ 个单词的长度。

<!-- tabs:start -->

Expand Down
16 changes: 14 additions & 2 deletions solution/0800-0899/0809.Expressive Words/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<pre>
<strong>Input:</strong> s = &quot;heeellooo&quot;, words = [&quot;hello&quot;, &quot;hi&quot;, &quot;helo&quot;]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
<strong>Explanation:</strong>
We can extend &quot;e&quot; and &quot;o&quot; in the word &quot;hello&quot; to get &quot;heeellooo&quot;.
We can&#39;t extend &quot;helo&quot; to get &quot;heeellooo&quot; because the group &quot;ll&quot; is not size 3 or more.
</pre>
Expand All @@ -68,7 +68,19 @@ We can&#39;t extend &quot;helo&quot; to get &quot;heeellooo&quot; because the gr

<!-- solution:start -->

### Solution 1
### Solution 1: Traversal Counting + Two Pointers

We can traverse the array $\textit{words}$, and for each word $t$ in the array, check if $t$ can be expanded to obtain $s$. If it can, increment the answer by one.

Therefore, the key problem is to determine if the word $t$ can be expanded to obtain $s$. We use a function $\textit{check}(s, t)$ to determine this. The implementation logic of the function is as follows:

First, check the length relationship between $s$ and $t$. If the length of $t$ is greater than $s$, return $\textit{false}$ directly. Otherwise, use two pointers $i$ and $j$ to point to $s$ and $t$, respectively, both initially set to $0$.

If the characters pointed to by $i$ and $j$ are different, then $t$ cannot be expanded to obtain $s$, and we return $\textit{false}$. Otherwise, we need to check the relationship between the consecutive occurrence counts of the characters pointed to by $i$ and $j$, denoted as $c_1$ and $c_2$. If $c_1 \lt c_2$ or $c_1 \lt 3$ and $c_1 \neq c_2$, then $t$ cannot be expanded to obtain $s$, and we return $\textit{false}$. Otherwise, move $i$ and $j$ to the right by $c_1$ and $c_2$ times, respectively, and continue checking.

If both $i$ and $j$ reach the end of the strings, then $t$ can be expanded to obtain $s$, and we return $\textit{true}$. Otherwise, we return $\textit{false}$.

The time complexity is $O(n \times m + \sum_{i=0}^{m-1} w_i)$, where $n$ and $m$ are the lengths of the string $s$ and the array $\textit{words}$, respectively, and $w_i$ is the length of the $i$-th word in the array $\textit{words}$.

<!-- tabs:start -->

Expand Down
30 changes: 15 additions & 15 deletions solution/0800-0899/0810.Chalkboard XOR Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ tags:
<pre>
<strong>输入:</strong> nums = [1,1,2]
<strong>输出:</strong> false
<strong>解释:</strong>
<strong>解释:</strong>
Alice 有两个选择: 擦掉数字 1 或 2。
如果擦掉 1, 数组变成 [1, 2]。剩余数字按位异或得到 1 XOR 2 = 3。那么 Bob 可以擦掉任意数字,因为 Alice 会成为擦掉最后一个数字的人,她总是会输。
如果 Alice 擦掉 2,那么数组变成[1, 1]。剩余数字按位异或得到 1 XOR 1 = 0。Alice 仍然会输掉游戏。
Expand Down Expand Up @@ -72,28 +72,28 @@ Alice 有两个选择: 擦掉数字 1 或 2。

### 方法一:位运算

根据游戏规则,轮到某个玩家时,如果当前黑板上所有数字按位异或运算结果为 $0$,这个玩家获胜。由于 Alice 先手,因此当 `nums` 中所有数字的异或结果为 $0$ 时,Alice 可以获胜。
根据游戏规则,轮到某个玩家时,如果当前黑板上所有数字按位异或运算结果为 $0$,这个玩家获胜。由于 Alice 先手,因此当 $\textit{nums}$ 中所有数字的异或结果为 $0$ 时,Alice 可以获胜。

`nums` 中所有数字的异或结果不为 $0$ 时,我们不妨考虑从数组 `nums` 的长度奇偶性来分析 Alice 的获胜情况。
$\textit{nums}$ 中所有数字的异或结果不为 $0$ 时,我们不妨考虑从数组 $\textit{nums}$ 的长度奇偶性来分析 Alice 的获胜情况。

`nums` 的长度为偶数时,如果 Alice 必败,那么只有一种情况,就是 Alice 无论擦掉哪个数字,剩余所有数字的异或结果都等于 $0$。我们来分析一下是否存在这种情况。
$\textit{nums}$ 的长度为偶数时,如果 Alice 必败,那么只有一种情况,就是 Alice 无论擦掉哪个数字,剩余所有数字的异或结果都等于 $0$。我们来分析一下是否存在这种情况。

假设数组 `nums` 长度为 $n$,并且 $n$ 是偶数,记所有数字异或结尾为 $S$,则有:
假设数组 $\textit{nums}$ 长度为 $n$,并且 $n$ 是偶数,记所有数字异或结尾为 $S$,则有:

$$
S = nums[0] \oplus nums[1] \oplus \cdots \oplus nums[n-1] \neq 0
S = \textit{nums}[0] \oplus \textit{nums}[1] \oplus \cdots \oplus \textit{nums}[n-1] \neq 0
$$

我们记 $S_i$ 为数组 `nums` 擦掉第 $i$ 个数字后的异或结果,那么有:
我们记 $S_i$ 为数组 $\textit{nums}$ 擦掉第 $i$ 个数字后的异或结果,那么有:

$$
S_i \oplus nums[i] = S
S_i \oplus \textit{nums}[i] = S
$$

等式两边同时异或 $nums[i]$,得到:
等式两边同时异或 $\textit{nums}[i]$,得到:

$$
S_i = S \oplus nums[i]
S_i = S \oplus \textit{nums}[i]
$$

如果无论 Alice 擦掉哪个数字,剩余所有数字的异或结果都等于 $0$,那么对所有 $i$,都有 $S_i = 0$,即:
Expand All @@ -102,19 +102,19 @@ $$
S_0 \oplus S_1 \oplus \cdots \oplus S_{n-1} = 0
$$

我们将 $S_i = S \oplus nums[i]$ 代入上式,得到:
我们将 $S_i = S \oplus \textit{nums}[i]$ 代入上式,得到:

$$
S \oplus nums[0] \oplus S \oplus nums[1] \oplus \cdots \oplus S \oplus nums[n-1] = 0
S \oplus \textit{nums}[0] \oplus S \oplus \textit{nums}[1] \oplus \cdots \oplus S \oplus \textit{nums}[n-1] = 0
$$

上式共有 $n$(偶数)个 $S$,而 $nums[0] \oplus nums[1] \oplus \cdots \oplus nums[n-1]$ 也等于 $S$,因此上式等价于 $0 \oplus S = 0$。这与 $S \neq 0$ 矛盾,因此不存在这种情况。因此当 `nums` 的长度为偶数时,Alice 必胜。
上式共有 $n$(偶数)个 $S$,而 $\textit{nums}[0] \oplus \textit{nums}[1] \oplus \cdots \oplus \textit{nums}[n-1]$ 也等于 $S$,因此上式等价于 $0 \oplus S = 0$。这与 $S \neq 0$ 矛盾,因此不存在这种情况。因此当 $\textit{nums}$ 的长度为偶数时,Alice 必胜。

如果长度为奇数,那么 Alice 擦掉一个数字后,剩余数字个数为偶数,也就是将偶数长度的情况留给 Bob,那么 Bob 必胜,也即 Alice 必败。

综上,当 `nums` 的长度为偶数,或者 `nums` 中所有数字的异或结果为 $0$ 时,Alice 可以获胜。
综上,当 $\textit{nums}$ 的长度为偶数,或者 $\textit{nums}$ 中所有数字的异或结果为 $0$ 时,Alice 可以获胜。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand Down
Loading
Loading