Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solution description to lc problems: No.2533~2541 #1938

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
Nov 7, 2023
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
15 changes: 15 additions & 0 deletions solution/2500-2599/2533.Number of Good Binary Strings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ It can be proven that there is only 1 good string satisfying all conditions.

## Solutions

**Solution 1: Dynamic Programming**

We define $f[i]$ as the number of strings of length $i$ that meet the condition. The state transition equation is:

$$
f[i] = \begin{cases}
1 & i = 0 \\
f[i - oneGroup] + f[i - zeroGroup] & i \geq 1
\end{cases}
$$

The final answer is $f[minLength] + f[minLength + 1] + \cdots + f[maxLength]$.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n=maxLength$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ nums 的数字和是 1 + 2 + 3 + 4 = 10 。

**方法一:模拟**

遍历数组 `nums`,计算元素和 $a$ 与数字和 $b$,最后返回 $|a - b|$ 即可。
我们遍历数组 $nums$,计算元素和 $a$ 与数字和 $b$,最后返回 $|a - b|$ 即可。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ The absolute difference between the element sum and digit sum is |10 - 10| = 0.

## Solutions

**Solution 1: Simulation**

We traverse the array $nums$, calculate the sum of elements $a$ and the sum of digits $b$, and finally return $|a - b|$.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@

如果一个子数组中包含 $k$ 对相同的元素,那么包含这个子数组的数组一定至少包含 $k$ 对相同的元素。

我们用一个哈希表 `cnt` 统计窗口内数组元素出现的次数,用 `cur` 统计窗口内相同元素的对数,用 $i$ 维护窗口的左端点。
我们用一个哈希表 $cnt$ 统计窗口内数组元素出现的次数,用 $cur$ 统计窗口内相同元素的对数,用 $i$ 维护窗口的左端点。

遍历数组 `nums`,我们将当前元素 $x$ 作为右端点,那么窗口内相同元素的对数将增加 $cnt[x]$,同时将 $x$ 的出现次数加一,即 $cnt[x] \leftarrow cnt[x] + 1$。接下来,我们循环判断移出左端点后窗口内相同元素的对数是否大于等于 $k$,如果大于等于 $k$,那么我们将左端点元素的出现次数减一,即 $cnt[nums[i]] \leftarrow cnt[nums[i]] - 1$,同时将窗口内相同元素的对数减去 $cnt[nums[i]]$,即 $cur \leftarrow cur - cnt[nums[i]]$,同时将左端点右移,即 $i \leftarrow i + 1$。此时窗口左端点以及左侧的所有元素都可以作为当前右端点的左端点,因此答案加上 $i + 1$。
遍历数组 $nums$,我们将当前元素 $x$ 作为右端点,那么窗口内相同元素的对数将增加 $cnt[x]$,同时将 $x$ 的出现次数加一,即 $cnt[x] \leftarrow cnt[x] + 1$。接下来,我们循环判断移出左端点后窗口内相同元素的对数是否大于等于 $k$,如果大于等于 $k$,那么我们将左端点元素的出现次数减一,即 $cnt[nums[i]] \leftarrow cnt[nums[i]] - 1$,同时将窗口内相同元素的对数减去 $cnt[nums[i]]$,即 $cur \leftarrow cur - cnt[nums[i]]$,同时将左端点右移,即 $i \leftarrow i + 1$。此时窗口左端点以及左侧的所有元素都可以作为当前右端点的左端点,因此答案加上 $i + 1$。

最后,我们返回答案即可。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@

## Solutions

**Solution 1: Hash Table + Two Pointers**

If a subarray contains $k$ pairs of identical elements, then an array that contains this subarray must contain at least $k$ pairs of identical elements.

We use a hash table $cnt$ to count the number of occurrences of each element in the window, use $cur$ to count the number of pairs of identical elements in the window, and use $i$ to maintain the left endpoint of the window.

We traverse the array $nums$, take the current element $x$ as the right endpoint, then the number of pairs of identical elements in the window will increase by $cnt[x]$, and the occurrence times of $x$ will be increased by one, i.e., $cnt[x] \leftarrow cnt[x] + 1$. Next, we loop to judge whether the number of pairs of identical elements in the window is greater than or equal to $k$ after removing the left endpoint. If it is greater than or equal to $k$, then we decrease the occurrence times of the left endpoint element by one, i.e., $cnt[nums[i]] \leftarrow cnt[nums[i]] - 1$, and decrease the number of pairs of identical elements in the window by $cnt[nums[i]]$, i.e., $cur \leftarrow cur - cnt[nums[i]]$, and move the left endpoint to the right, i.e., $i \leftarrow i + 1$. At this time, all elements to the left of the window left endpoint and the left endpoint itself can be used as the left endpoint of the current right endpoint, so the answer is increased by $i + 1$.

Finally, we return the answer.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
- 前面不去掉端点的最大路径和加上当前节点去掉端点的最大路径和,即 $a + d$;
- 前面去掉端点的最大路径和加上当前节点不去掉端点的最大路径和,即 $b + c$。

我们更新答案的最大值,即 $ans = max(ans, a + d, b + c)$。
我们更新答案的最大值,即 $ans = \max(ans, a + d, b + c)$。

然后更新 $a$ 和 $b$,即 $a = max(a, price[i] + c)$, $b = max(b, price[i] + d)$,最后返回。
然后更新 $a$ 和 $b$,即 $a = \max(a, price[i] + c)$, $b = \max(b, price[i] + d)$,最后返回。

时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。其中 $n$ 为节点个数。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ The difference between the maximum and minimum price sum is 2. It can be proved

## Solutions

**Soution 1: Tree-shaped DP**

Since the value of each node is a positive integer, the smallest path with node $root$ as the root node is the $root$ node itself. Therefore, the difference between the path with the maximum value sum and the smallest path is equivalent to removing one endpoint of the path.

We design a function $dfs(i, fa)$, which represents the maximum path sum in the subtree with node $i$ as the root node, with and without removing the endpoint. Here, $fa$ represents the parent node of node $i$.

The implementation logic of the function $dfs(i, fa)$ is as follows:

Initialize $a = price[i]$, $b = 0$, indicating that initially there is only one node, the maximum path sum without removing the endpoint is $price[i]$, and the maximum path sum with removing the endpoint is $0$.

For each child node $j$ of node $i$, if $j \ne fa$, then recursively call the function $dfs(j, i)$. Here, it returns the maximum path sum in the subtree with node $j$ as the root node, with and without removing the endpoint, denoted as $c$ and $d$. At this time, there are two cases for the answer:

- The maximum path sum without removing the endpoint plus the maximum path sum of the current node with removing the endpoint, that is, $a + d$;
- The maximum path sum with removing the endpoint plus the maximum path sum of the current node without removing the endpoint, that is, $b + c$.

We update the maximum value of the answer, that is, $ans = \max(ans, a + d, b + c)$.

Then update $a$ and $b$, that is, $a = \max(a, price[i] + c)$, $b = \max(b, price[i] + d)$, and finally return.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

**方法一:枚举 + 组合计数**

由于题目说的是子序列中字母出现的次数,因此,我们可以先用一个数组 `cnt` 统计字符串 $s$ 中每个字母出现的次数,记最大的次数为 $mx$。
由于题目说的是子序列中字母出现的次数,因此,我们可以先用一个数组 $cnt$ 统计字符串 $s$ 中每个字母出现的次数,记最大的次数为 $mx$。

接下来,我们在 $[1,2..mx]$ 范围内枚举子序列中字母出现的次数 $i$,然后枚举所有出现过的字母,如果该字母 $c$ 的出现次数 $cnt[c]$ 大于等于 $i$,那么我们可以从这 $cnt[c]$ 个相同字母中选择其中 $i$ 个,也可以一个都不选,那么当前字母的可选方案数就是 $comb(cnt[c], i) + 1$,将所有可选方案数相乘,可以得到当前次数的所有子序列次数,将次数减去 $1$ 累加到答案中。

Expand Down
2 changes: 1 addition & 1 deletion solution/2500-2599/2540.Minimum Common Value/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

遍历两个数组,如果两个指针指向的元素相等,则返回该元素;如果两个指针指向的元素不相等,则将指向较小元素的指针右移一位,直到找到相等的元素或者遍历完数组。

时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是两个数组的长度。
时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别是两个数组的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand Down
6 changes: 6 additions & 0 deletions solution/2500-2599/2540.Minimum Common Value/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@

## Solutions

**Solution 1: Two Pointers**

Traverse the two arrays. If the elements pointed to by the two pointers are equal, return that element. If the elements pointed to by the two pointers are not equal, move the pointer pointing to the smaller element to the right by one bit until an equal element is found or the array is traversed.

The time complexity is $O(m + n)$, where $m$ and $n$ are the lengths of the two arrays respectively. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ One can prove that it is impossible to make arrays equal in fewer operations.</p

## Solutions

**Solution 1: Single Pass**

We use a variable $x$ to record the difference in the number of additions and subtractions, and a variable $ans$ to record the number of operations.

We traverse the array, and for each position $i$, if $k=0$ and $a_i \neq b_i$, then it is impossible to make the two arrays equal, so we return $-1$. Otherwise, if $k \neq 0$, then $a_i - b_i$ must be a multiple of $k$, otherwise it is impossible to make the two arrays equal, so we return $-1$. Next, we update $x$ and $ans$.

Finally, if $x \neq 0$, then it is impossible to make the two arrays equal, so we return $-1$. Otherwise, we return $\frac{ans}{2}$.

The time complexity is $O(n)$, and the space complexity is $O(1)$, where $n$ is the length of the array.

<!-- tabs:start -->

### **Python3**
Expand Down