Skip to content

feat: update lc problems #2208

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 3 commits into from
Jan 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ It can be shown that the minimum possible length of str<sub>2</sub> is 6.

## Solutions

**Solution 1: Case Discussion**
**Solution 1: Memoization Search**

We observe that the string 'AA' can only be followed by 'BB', and the string 'AB' can be placed at the beginning or end of the string. Therefore:
We notice that when concatenating strings, the first and last characters of the string will affect the length of the concatenated string. Therefore, we design a function $dfs(i, a, b)$, which represents the minimum length of the concatenated string starting from the $i$-th string, and the first character of the previously concatenated string is $a$, and the last character is $b$.

- If $x < y$, we can first alternately place 'BBAABBAA..BB', placing a total of $x$ 'AA' and $x+1$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x \times 2 + z + 1) \times 2$;
- If $x > y$, we can first alternately place 'AABBAABB..AA', placing a total of $y$ 'BB' and $y+1$ 'AA', then place the remaining $z$ 'AB', with a total length of $(y \times 2 + z + 1) \times 2$;
- If $x = y$, we only need to alternately place 'AABB', placing a total of $x$ 'AA' and $y$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x + y + z) \times 2$.
The execution process of the function $dfs(i, a, b)$ is as follows:

The time complexity is $O(1)$, and the space complexity is $O(1)$.
- If $i = n$, it means that all strings have been concatenated, return $0$;
- Otherwise, we consider concatenating the $i$-th string to the end or the beginning of the already concatenated string, and get the lengths $x$ and $y$ of the concatenated string, then $dfs(i, a, b) = \min(x, y) + |words[i]|$.

To avoid repeated calculations, we use the method of memoization search. Specifically, we use a three-dimensional array $f$ to store all the return values of $dfs(i, a, b)$. When we need to calculate $dfs(i, a, b)$, if $f[i][a][b]$ has been calculated, we directly return $f[i][a][b]$; otherwise, we calculate the value of $dfs(i, a, b)$ according to the above recurrence relation, and store it in $f[i][a][b]$.

In the main function, we directly return $|words[0]| + dfs(1, words[0][0], words[0][|words[0]| - 1])$.

The time complexity is $O(n \times C^2)$, and the space complexity is $O(n \times C^2)$. Where $C$ represents the maximum length of the string.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<p>Return <em>the size of such subtree with the <strong>maximum</strong> number of nodes possible.</em></p>

<p>&nbsp;</p>
<p><strong><img alt="" src="https://assets.leetcode.com/static_assets/others/20231216-134026.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 132px;" /></strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/images/20231216-134026.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 132px;" /></strong></p>

<p><strong class="example">Example 1:</strong></p>

Expand All @@ -33,7 +33,7 @@
<strong>Explanation:</strong> The whole tree has the same color, and the subtree rooted at node 0 has the most number of nodes which is 4. Hence, we return 4.
</pre>

<p><strong><img alt="" src="https://assets.leetcode.com/static_assets/others/20231216-134017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 221px;" /></strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/images/20231216-134017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 221px;" /></strong></p>

<p><strong class="example">Example 3:</strong></p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<p>Return <em>the size of such subtree with the <strong>maximum</strong> number of nodes possible.</em></p>

<p>&nbsp;</p>
<p><strong><img alt="" src="https://assets.leetcode.com/static_assets/others/20231216-134026.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 132px;" /></strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/images/20231216-134026.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 132px;" /></strong></p>

<p><strong class="example">Example 1:</strong></p>

Expand All @@ -31,7 +31,7 @@
<strong>Explanation:</strong> The whole tree has the same color, and the subtree rooted at node 0 has the most number of nodes which is 4. Hence, we return 4.
</pre>

<p><strong><img alt="" src="https://assets.leetcode.com/static_assets/others/20231216-134017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 221px;" /></strong></p>
<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/images/20231216-134017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 221px; height: 221px;" /></strong></p>

<p><strong class="example">Example 3:</strong></p>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@
| 2989 | [班级表现](/solution/2900-2999/2989.Class%20Performance/README.md) | `数据库` | 中等 | 🔒 |
| 2990 | [贷款类型](/solution/2900-2999/2990.Loan%20Types/README.md) | `数据库` | 简单 | 🔒 |
| 2991 | [最好的三家酒庄](/solution/2900-2999/2991.Top%20Three%20Wineries/README.md) | `数据库` | 困难 | 🔒 |
| 2993 | [Friday Purchases I](/solution/2900-2999/2993.Friday%20Purchases%20I/README.md) | `数据库` | 中等 | 🔒 |
| 2994 | [Friday Purchases II](/solution/2900-2999/2994.Friday%20Purchases%20II/README.md) | `数据库` | 困难 | 🔒 |
| 2993 | [发生在周五的交易 I](/solution/2900-2999/2993.Friday%20Purchases%20I/README.md) | `数据库` | 中等 | 🔒 |
| 2994 | [发生在周五的交易 II](/solution/2900-2999/2994.Friday%20Purchases%20II/README.md) | `数据库` | 困难 | 🔒 |
| 2995 | [观众变主播](/solution/2900-2999/2995.Viewers%20Turned%20Streamers/README.md) | `数据库` | 困难 | 🔒 |

## 版权
Expand Down
6 changes: 3 additions & 3 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3003,8 +3003,8 @@
| 2990 | [贷款类型](/solution/2900-2999/2990.Loan%20Types/README.md) | `数据库` | 简单 | 🔒 |
| 2991 | [最好的三家酒庄](/solution/2900-2999/2991.Top%20Three%20Wineries/README.md) | `数据库` | 困难 | 🔒 |
| 2992 | [自整除排列的数量](/solution/2900-2999/2992.Number%20of%20Self-Divisible%20Permutations/README.md) | `位运算`,`递归`,`数组`,`动态规划`,`状态压缩` | 中等 | 🔒 |
| 2993 | [Friday Purchases I](/solution/2900-2999/2993.Friday%20Purchases%20I/README.md) | `数据库` | 中等 | 🔒 |
| 2994 | [Friday Purchases II](/solution/2900-2999/2994.Friday%20Purchases%20II/README.md) | `数据库` | 困难 | 🔒 |
| 2993 | [发生在周五的交易 I](/solution/2900-2999/2993.Friday%20Purchases%20I/README.md) | `数据库` | 中等 | 🔒 |
| 2994 | [发生在周五的交易 II](/solution/2900-2999/2994.Friday%20Purchases%20II/README.md) | `数据库` | 困难 | 🔒 |
| 2995 | [观众变主播](/solution/2900-2999/2995.Viewers%20Turned%20Streamers/README.md) | `数据库` | 困难 | 🔒 |
| 2996 | [大于等于顺序前缀和的最小缺失整数](/solution/2900-2999/2996.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md) | `数组`,`哈希表`,`排序` | 简单 | 第 121 场双周赛 |
| 2997 | [使数组异或和等于 K 的最少操作次数](/solution/2900-2999/2997.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README.md) | `位运算`,`数组` | 中等 | 第 121 场双周赛 |
Expand All @@ -3014,7 +3014,7 @@
| 3001 | [捕获黑皇后需要的最少移动次数](/solution/3000-3099/3001.Minimum%20Moves%20to%20Capture%20The%20Queen/README.md) | `数组`,`枚举` | 中等 | 第 379 场周赛 |
| 3002 | [移除后集合的最多元素数](/solution/3000-3099/3002.Maximum%20Size%20of%20a%20Set%20After%20Removals/README.md) | `贪心`,`数组`,`哈希表` | 中等 | 第 379 场周赛 |
| 3003 | [执行操作后的最大分割数量](/solution/3000-3099/3003.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README.md) | `位运算`,`字符串`,`动态规划`,`状态压缩` | 困难 | 第 379 场周赛 |
| 3004 | [Maximum Subtree of the Same Color](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README.md) | | 中等 | 🔒 |
| 3004 | [相同颜色的最大子树](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README.md) | | 中等 | 🔒 |

## 版权

Expand Down
4 changes: 2 additions & 2 deletions solution/database-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,6 @@
- [2989.班级表现](/database-solution/2900-2999/2989.Class%20Performance/README.md)
- [2990.贷款类型](/database-solution/2900-2999/2990.Loan%20Types/README.md)
- [2991.最好的三家酒庄](/database-solution/2900-2999/2991.Top%20Three%20Wineries/README.md)
- [2993.Friday Purchases I](/database-solution/2900-2999/2993.Friday%20Purchases%20I/README.md)
- [2994.Friday Purchases II](/database-solution/2900-2999/2994.Friday%20Purchases%20II/README.md)
- [2993.发生在周五的交易 I](/database-solution/2900-2999/2993.Friday%20Purchases%20I/README.md)
- [2994.发生在周五的交易 II](/database-solution/2900-2999/2994.Friday%20Purchases%20II/README.md)
- [2995.观众变主播](/database-solution/2900-2999/2995.Viewers%20Turned%20Streamers/README.md)
6 changes: 3 additions & 3 deletions solution/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -3050,8 +3050,8 @@
- [2990.贷款类型](/solution/2900-2999/2990.Loan%20Types/README.md)
- [2991.最好的三家酒庄](/solution/2900-2999/2991.Top%20Three%20Wineries/README.md)
- [2992.自整除排列的数量](/solution/2900-2999/2992.Number%20of%20Self-Divisible%20Permutations/README.md)
- [2993.Friday Purchases I](/solution/2900-2999/2993.Friday%20Purchases%20I/README.md)
- [2994.Friday Purchases II](/solution/2900-2999/2994.Friday%20Purchases%20II/README.md)
- [2993.发生在周五的交易 I](/solution/2900-2999/2993.Friday%20Purchases%20I/README.md)
- [2994.发生在周五的交易 II](/solution/2900-2999/2994.Friday%20Purchases%20II/README.md)
- [2995.观众变主播](/solution/2900-2999/2995.Viewers%20Turned%20Streamers/README.md)
- [2996.大于等于顺序前缀和的最小缺失整数](/solution/2900-2999/2996.Smallest%20Missing%20Integer%20Greater%20Than%20Sequential%20Prefix%20Sum/README.md)
- [2997.使数组异或和等于 K 的最少操作次数](/solution/2900-2999/2997.Minimum%20Number%20of%20Operations%20to%20Make%20Array%20XOR%20Equal%20to%20K/README.md)
Expand All @@ -3063,4 +3063,4 @@
- [3001.捕获黑皇后需要的最少移动次数](/solution/3000-3099/3001.Minimum%20Moves%20to%20Capture%20The%20Queen/README.md)
- [3002.移除后集合的最多元素数](/solution/3000-3099/3002.Maximum%20Size%20of%20a%20Set%20After%20Removals/README.md)
- [3003.执行操作后的最大分割数量](/solution/3000-3099/3003.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README.md)
- [3004.Maximum Subtree of the Same Color](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README.md)
- [3004.相同颜色的最大子树](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README.md)