From ff8d6117b295225ae6e6bbb444ce827e8a65b29d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 1 Apr 2025 13:05:02 +0000 Subject: [PATCH 1/2] chore: update lc problems --- .../README.md | 2 +- .../README.md | 3 + .../README_EN.md | 3 + .../3498.Reverse Degree of a String/README.md | 3 + .../README_EN.md | 3 + .../README.md | 3 + .../README_EN.md | 3 + .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 5 + .../README_EN.md | 5 + .../README.md | 2 + .../README_EN.md | 2 + .../README.md | 5 + .../README_EN.md | 5 + .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 7 + .../README_EN.md | 7 + .../README.md | 121 ++++++++++++++++++ .../README_EN.md | 119 +++++++++++++++++ solution/DATABASE_README.md | 2 +- solution/DATABASE_README_EN.md | 2 +- solution/README.md | 21 +-- solution/README_EN.md | 21 +-- 25 files changed, 337 insertions(+), 23 deletions(-) create mode 100644 solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md create mode 100644 solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md diff --git a/solution/3400-3499/3482.Analyze Organization Hierarchy/README.md b/solution/3400-3499/3482.Analyze Organization Hierarchy/README.md index 88409e56b6bbd..502a994e53d75 100644 --- a/solution/3400-3499/3482.Analyze Organization Hierarchy/README.md +++ b/solution/3400-3499/3482.Analyze Organization Hierarchy/README.md @@ -135,7 +135,7 @@ employee_id 是这张表的唯一主键。 diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md index 2d28374ba4b3b..05ae8fff402b3 100644 --- a/solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README.md @@ -2,6 +2,9 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README.md +tags: + - 贪心 + - 数组 --- diff --git a/solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md index 16f79e3644d4e..605330e67ffca 100644 --- a/solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md +++ b/solution/3400-3499/3496.Maximize Score After Pair Deletions/README_EN.md @@ -2,6 +2,9 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README_EN.md +tags: + - Greedy + - Array --- diff --git a/solution/3400-3499/3498.Reverse Degree of a String/README.md b/solution/3400-3499/3498.Reverse Degree of a String/README.md index 8b95ad9ef2e3f..2ae4b76e55cc0 100644 --- a/solution/3400-3499/3498.Reverse Degree of a String/README.md +++ b/solution/3400-3499/3498.Reverse Degree of a String/README.md @@ -2,6 +2,9 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README.md +tags: + - 字符串 + - 模拟 --- diff --git a/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md b/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md index 0d2ba88add1b5..5cf48883daae3 100644 --- a/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md +++ b/solution/3400-3499/3498.Reverse Degree of a String/README_EN.md @@ -2,6 +2,9 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README_EN.md +tags: + - String + - Simulation --- diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md b/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md index 38d2e933099f5..386cabdb7b6ed 100644 --- a/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/README.md @@ -2,6 +2,9 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3499.Maximize%20Active%20Section%20with%20Trade%20I/README.md +tags: + - 字符串 + - 枚举 --- diff --git a/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md b/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md index 316b5c450e549..dd5927dddb75d 100644 --- a/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md +++ b/solution/3400-3499/3499.Maximize Active Section with Trade I/README_EN.md @@ -2,6 +2,9 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3499.Maximize%20Active%20Section%20with%20Trade%20I/README_EN.md +tags: + - String + - Enumeration --- diff --git a/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README.md b/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README.md index 9bf4c981ea035..846de3bc27c55 100644 --- a/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README.md +++ b/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3500.Minimum%20Cost%20to%20Divide%20Array%20Into%20Subarrays/README.md +tags: + - 数组 + - 动态规划 + - 前缀和 --- diff --git a/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README_EN.md b/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README_EN.md index ab403c073ad5a..002c51ac750f1 100644 --- a/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README_EN.md +++ b/solution/3500-3599/3500.Minimum Cost to Divide Array Into Subarrays/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3500.Minimum%20Cost%20to%20Divide%20Array%20Into%20Subarrays/README_EN.md +tags: + - Array + - Dynamic Programming + - Prefix Sum --- diff --git a/solution/3500-3599/3501.Maximize Active Section with Trade II/README.md b/solution/3500-3599/3501.Maximize Active Section with Trade II/README.md index 2eb5f21104e28..edb36a81a2472 100644 --- a/solution/3500-3599/3501.Maximize Active Section with Trade II/README.md +++ b/solution/3500-3599/3501.Maximize Active Section with Trade II/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3501.Maximize%20Active%20Section%20with%20Trade%20II/README.md +tags: + - 线段树 + - 数组 + - 字符串 + - 二分查找 --- diff --git a/solution/3500-3599/3501.Maximize Active Section with Trade II/README_EN.md b/solution/3500-3599/3501.Maximize Active Section with Trade II/README_EN.md index f9820eafe32ea..d04c52b36573a 100644 --- a/solution/3500-3599/3501.Maximize Active Section with Trade II/README_EN.md +++ b/solution/3500-3599/3501.Maximize Active Section with Trade II/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3501.Maximize%20Active%20Section%20with%20Trade%20II/README_EN.md +tags: + - Segment Tree + - Array + - String + - Binary Search --- diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md index 606f8926fa060..2a77898ba3016 100644 --- a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README.md +tags: + - 数组 --- diff --git a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md index 225ae995772ba..58d2e7a494feb 100644 --- a/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md +++ b/solution/3500-3599/3502.Minimum Cost to Reach Every Position/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README_EN.md +tags: + - Array --- diff --git a/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README.md b/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README.md index e97cc409608be..53cc43def7534 100644 --- a/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README.md +++ b/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README.md +tags: + - 双指针 + - 字符串 + - 动态规划 + - 枚举 --- diff --git a/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README_EN.md b/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README_EN.md index 24faceb1e6e89..e1e40281aaa1a 100644 --- a/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README_EN.md +++ b/solution/3500-3599/3503.Longest Palindrome After Substring Concatenation I/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README_EN.md +tags: + - Two Pointers + - String + - Dynamic Programming + - Enumeration --- diff --git a/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README.md b/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README.md index 269de847bbde6..7ee14a477e4d4 100644 --- a/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README.md +++ b/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README.md +tags: + - 双指针 + - 字符串 + - 动态规划 --- diff --git a/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README_EN.md b/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README_EN.md index 95221be84beba..598cda355159e 100644 --- a/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README_EN.md +++ b/solution/3500-3599/3504.Longest Palindrome After Substring Concatenation II/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README_EN.md +tags: + - Two Pointers + - String + - Dynamic Programming --- diff --git a/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README.md b/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README.md index 6e752aae79241..305837bdbe0bf 100644 --- a/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README.md +++ b/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README.md @@ -2,6 +2,13 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README.md +tags: + - 数组 + - 哈希表 + - 数学 + - 动态规划 + - 滑动窗口 + - 堆(优先队列) --- diff --git a/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README_EN.md b/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README_EN.md index c6bf631bd3eec..182675e675e99 100644 --- a/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README_EN.md +++ b/solution/3500-3599/3505.Minimum Operations to Make Elements Within K Subarrays Equal/README_EN.md @@ -2,6 +2,13 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README_EN.md +tags: + - Array + - Hash Table + - Math + - Dynamic Programming + - Sliding Window + - Heap (Priority Queue) --- diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md new file mode 100644 index 0000000000000..03e632d4a6c3f --- /dev/null +++ b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md @@ -0,0 +1,121 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md +--- + + + +# [3506. 查找消除细菌菌株所需时间 II 🔒](https://leetcode.cn/problems/find-time-required-to-eliminate-bacterial-strains) + +[English Version](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README_EN.md) + +## 题目描述 + + + +

给定一个整数数组 timeReq 和一个整数 splitTime

+ +

在人体微观世界中,免疫系统面临着一项非凡的挑战:对抗快速繁殖的细菌群落,这对身体的生存构成威胁。

+ +

最初,只部署一个 白细胞WBC)来消除细菌。然而,单独的白细胞很快意识到它无法跟上细菌的生长速度。

+ +

WBC制定了一种巧妙的策略来对抗细菌:

+ + + +

您必须确定消除所有细菌菌株所需的 最短 时间。

+ +

注意,细菌菌株可以按任何顺序消除。

+ +

 

+ +

示例 1:

+ +
+

输入:timeReq = [10,4,5], splitTime = 2

+ +

输出:12

+ +

解释:

+ +

消除过程如下:

+ + +
+ +

示例 2:

+ +
+

输入:timeReq = [10,4], splitTime = 5

+ +

输出:5

+ +

解释:

+ +

消除过程如下:

+ + +
+ +

 

+ +

提示:

+ + + + + +## 解法 + + + +### 方法一 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md new file mode 100644 index 0000000000000..6f3cfbf295cad --- /dev/null +++ b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md @@ -0,0 +1,119 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README_EN.md +--- + + + +# [3506. Find Time Required to Eliminate Bacterial Strains 🔒](https://leetcode.com/problems/find-time-required-to-eliminate-bacterial-strains) + +[中文文档](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md) + +## Description + + + +

You are given an integer array timeReq and an integer splitTime.

+ +

In the microscopic world of the human body, the immune system faces an extraordinary challenge: combatting a rapidly multiplying bacterial colony that threatens the body's survival.

+ +

Initially, only one white blood cell (WBC) is deployed to eliminate the bacteria. However, the lone WBC quickly realizes it cannot keep up with the bacterial growth rate.

+ +

The WBC devises a clever strategy to fight the bacteria:

+ + + +

You must determine the minimum time required to eliminate all the bacterial strains.

+ +

Note that the bacterial strains can be eliminated in any order.

+ +

 

+

Example 1:

+ +
+

Input: timeReq = [10,4,5], splitTime = 2

+ +

Output: 12

+ +

Explanation:

+ +

The elimination process goes as follows:

+ + +
+ +

Example 2:

+ +
+

Input: timeReq = [10,4], splitTime = 5

+ +

Output:15

+ +

Explanation:

+ +

The elimination process goes as follows:

+ + +
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python + +``` + +#### Java + +```java + +``` + +#### C++ + +```cpp + +``` + +#### Go + +```go + +``` + + + + + + diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 6d84fcdfa02dc..17971ff7c3144 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -313,7 +313,7 @@ | 3465 | [查找具有有效序列号的产品](/solution/3400-3499/3465.Find%20Products%20with%20Valid%20Serial%20Numbers/README.md) | `数据库` | 简单 | | | 3475 | [DNA 模式识别](/solution/3400-3499/3475.DNA%20Pattern%20Recognition/README.md) | | 中等 | | | 3482 | [分析组织层级](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README.md) | `数据库` | 困难 | | -| 3497 | [分析订阅转化](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README.md) | | 中等 | | +| 3497 | [分析订阅转化](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README.md) | `数据库` | 中等 | | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index e2e8dff2e2d8e..cb0d4ea59039c 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -311,7 +311,7 @@ Press Control + F(or Command + F on | 3465 | [Find Products with Valid Serial Numbers](/solution/3400-3499/3465.Find%20Products%20with%20Valid%20Serial%20Numbers/README_EN.md) | `Database` | Easy | | | 3475 | [DNA Pattern Recognition](/solution/3400-3499/3475.DNA%20Pattern%20Recognition/README_EN.md) | | Medium | | | 3482 | [Analyze Organization Hierarchy](/solution/3400-3499/3482.Analyze%20Organization%20Hierarchy/README_EN.md) | `Database` | Hard | | -| 3497 | [Analyze Subscription Conversion](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README_EN.md) | | Medium | | +| 3497 | [Analyze Subscription Conversion](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README_EN.md) | `Database` | Medium | | ## Copyright diff --git a/solution/README.md b/solution/README.md index 7679fdb3c41b3..8699c3e8db3ab 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3506,17 +3506,18 @@ | 3493 | [属性图](/solution/3400-3499/3493.Properties%20Graph/README.md) | `深度优先搜索`,`广度优先搜索`,`并查集`,`图`,`数组`,`哈希表` | 中等 | 第 442 场周赛 | | 3494 | [酿造药水需要的最少总时间](/solution/3400-3499/3494.Find%20the%20Minimum%20Amount%20of%20Time%20to%20Brew%20Potions/README.md) | `数组`,`前缀和`,`模拟` | 中等 | 第 442 场周赛 | | 3495 | [使数组元素都变为零的最少操作次数](/solution/3400-3499/3495.Minimum%20Operations%20to%20Make%20Array%20Elements%20Zero/README.md) | `位运算`,`数组`,`数学` | 困难 | 第 442 场周赛 | -| 3496 | [最大化配对删除后的得分](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README.md) | | 中等 | 🔒 | -| 3497 | [分析订阅转化](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README.md) | | 中等 | | -| 3498 | [字符串的反转度](/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README.md) | | 简单 | 第 153 场双周赛 | -| 3499 | [操作后最大活跃区段数 I](/solution/3400-3499/3499.Maximize%20Active%20Section%20with%20Trade%20I/README.md) | | 中等 | 第 153 场双周赛 | -| 3500 | [将数组分割为子数组的最小代价](/solution/3500-3599/3500.Minimum%20Cost%20to%20Divide%20Array%20Into%20Subarrays/README.md) | | 困难 | 第 153 场双周赛 | -| 3501 | [操作后最大活跃区段数 II](/solution/3500-3599/3501.Maximize%20Active%20Section%20with%20Trade%20II/README.md) | | 困难 | 第 153 场双周赛 | -| 3502 | [到达每个位置的最小费用](/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README.md) | | 简单 | 第 443 场周赛 | -| 3503 | [子字符串连接后的最长回文串 I](/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README.md) | | 中等 | 第 443 场周赛 | -| 3504 | [子字符串连接后的最长回文串 II](/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README.md) | | 困难 | 第 443 场周赛 | -| 3505 | [使 K 个子数组内元素相等的最少操作数](/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README.md) | | 困难 | 第 443 场周赛 | +| 3496 | [最大化配对删除后的得分](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README.md) | `贪心`,`数组` | 中等 | 🔒 | +| 3497 | [分析订阅转化](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README.md) | `数据库` | 中等 | | +| 3498 | [字符串的反转度](/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README.md) | `字符串`,`模拟` | 简单 | 第 153 场双周赛 | +| 3499 | [操作后最大活跃区段数 I](/solution/3400-3499/3499.Maximize%20Active%20Section%20with%20Trade%20I/README.md) | `字符串`,`枚举` | 中等 | 第 153 场双周赛 | +| 3500 | [将数组分割为子数组的最小代价](/solution/3500-3599/3500.Minimum%20Cost%20to%20Divide%20Array%20Into%20Subarrays/README.md) | `数组`,`动态规划`,`前缀和` | 困难 | 第 153 场双周赛 | +| 3501 | [操作后最大活跃区段数 II](/solution/3500-3599/3501.Maximize%20Active%20Section%20with%20Trade%20II/README.md) | `线段树`,`数组`,`字符串`,`二分查找` | 困难 | 第 153 场双周赛 | +| 3502 | [到达每个位置的最小费用](/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README.md) | `数组` | 简单 | 第 443 场周赛 | +| 3503 | [子字符串连接后的最长回文串 I](/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README.md) | `双指针`,`字符串`,`动态规划`,`枚举` | 中等 | 第 443 场周赛 | +| 3504 | [子字符串连接后的最长回文串 II](/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README.md) | `双指针`,`字符串`,`动态规划` | 困难 | 第 443 场周赛 | +| 3505 | [使 K 个子数组内元素相等的最少操作数](/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README.md) | `数组`,`哈希表`,`数学`,`动态规划`,`滑动窗口`,`堆(优先队列)` | 困难 | 第 443 场周赛 | | 3506 | [Find Time Required to Eliminate Bacterial Strains II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README.md) | | 困难 | 🔒 | +| 3506 | [查找消除细菌菌株所需时间 II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md) | | 困难 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index def52d26ffde2..e53de1cdb4764 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3504,17 +3504,18 @@ Press Control + F(or Command + F on | 3493 | [Properties Graph](/solution/3400-3499/3493.Properties%20Graph/README_EN.md) | `Depth-First Search`,`Breadth-First Search`,`Union Find`,`Graph`,`Array`,`Hash Table` | Medium | Weekly Contest 442 | | 3494 | [Find the Minimum Amount of Time to Brew Potions](/solution/3400-3499/3494.Find%20the%20Minimum%20Amount%20of%20Time%20to%20Brew%20Potions/README_EN.md) | `Array`,`Prefix Sum`,`Simulation` | Medium | Weekly Contest 442 | | 3495 | [Minimum Operations to Make Array Elements Zero](/solution/3400-3499/3495.Minimum%20Operations%20to%20Make%20Array%20Elements%20Zero/README_EN.md) | `Bit Manipulation`,`Array`,`Math` | Hard | Weekly Contest 442 | -| 3496 | [Maximize Score After Pair Deletions](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README_EN.md) | | Medium | 🔒 | -| 3497 | [Analyze Subscription Conversion](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README_EN.md) | | Medium | | -| 3498 | [Reverse Degree of a String](/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README_EN.md) | | Easy | Biweekly Contest 153 | -| 3499 | [Maximize Active Section with Trade I](/solution/3400-3499/3499.Maximize%20Active%20Section%20with%20Trade%20I/README_EN.md) | | Medium | Biweekly Contest 153 | -| 3500 | [Minimum Cost to Divide Array Into Subarrays](/solution/3500-3599/3500.Minimum%20Cost%20to%20Divide%20Array%20Into%20Subarrays/README_EN.md) | | Hard | Biweekly Contest 153 | -| 3501 | [Maximize Active Section with Trade II](/solution/3500-3599/3501.Maximize%20Active%20Section%20with%20Trade%20II/README_EN.md) | | Hard | Biweekly Contest 153 | -| 3502 | [Minimum Cost to Reach Every Position](/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README_EN.md) | | Easy | Weekly Contest 443 | -| 3503 | [Longest Palindrome After Substring Concatenation I](/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README_EN.md) | | Medium | Weekly Contest 443 | -| 3504 | [Longest Palindrome After Substring Concatenation II](/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README_EN.md) | | Hard | Weekly Contest 443 | -| 3505 | [Minimum Operations to Make Elements Within K Subarrays Equal](/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README_EN.md) | | Hard | Weekly Contest 443 | +| 3496 | [Maximize Score After Pair Deletions](/solution/3400-3499/3496.Maximize%20Score%20After%20Pair%20Deletions/README_EN.md) | `Greedy`,`Array` | Medium | 🔒 | +| 3497 | [Analyze Subscription Conversion](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README_EN.md) | `Database` | Medium | | +| 3498 | [Reverse Degree of a String](/solution/3400-3499/3498.Reverse%20Degree%20of%20a%20String/README_EN.md) | `String`,`Simulation` | Easy | Biweekly Contest 153 | +| 3499 | [Maximize Active Section with Trade I](/solution/3400-3499/3499.Maximize%20Active%20Section%20with%20Trade%20I/README_EN.md) | `String`,`Enumeration` | Medium | Biweekly Contest 153 | +| 3500 | [Minimum Cost to Divide Array Into Subarrays](/solution/3500-3599/3500.Minimum%20Cost%20to%20Divide%20Array%20Into%20Subarrays/README_EN.md) | `Array`,`Dynamic Programming`,`Prefix Sum` | Hard | Biweekly Contest 153 | +| 3501 | [Maximize Active Section with Trade II](/solution/3500-3599/3501.Maximize%20Active%20Section%20with%20Trade%20II/README_EN.md) | `Segment Tree`,`Array`,`String`,`Binary Search` | Hard | Biweekly Contest 153 | +| 3502 | [Minimum Cost to Reach Every Position](/solution/3500-3599/3502.Minimum%20Cost%20to%20Reach%20Every%20Position/README_EN.md) | `Array` | Easy | Weekly Contest 443 | +| 3503 | [Longest Palindrome After Substring Concatenation I](/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README_EN.md) | `Two Pointers`,`String`,`Dynamic Programming`,`Enumeration` | Medium | Weekly Contest 443 | +| 3504 | [Longest Palindrome After Substring Concatenation II](/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README_EN.md) | `Two Pointers`,`String`,`Dynamic Programming` | Hard | Weekly Contest 443 | +| 3505 | [Minimum Operations to Make Elements Within K Subarrays Equal](/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README_EN.md) | `Array`,`Hash Table`,`Math`,`Dynamic Programming`,`Sliding Window`,`Heap (Priority Queue)` | Hard | Weekly Contest 443 | | 3506 | [Find Time Required to Eliminate Bacterial Strains II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README_EN.md) | | Hard | 🔒 | +| 3506 | [Find Time Required to Eliminate Bacterial Strains](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README_EN.md) | | Hard | 🔒 | ## Copyright From 52c731f84629f48fe0dcc08f9b5bdc70f2b74c60 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 1 Apr 2025 13:09:27 +0000 Subject: [PATCH 2/2] fix: problem --- .../README.md | 223 ------------------ .../README_EN.md | 223 ------------------ .../README.md | 112 ++++++++- .../README_EN.md | 112 ++++++++- .../Solution.cpp | 0 .../Solution.go | 0 .../Solution.java | 0 .../Solution.py | 0 .../Solution.rs | 0 .../Solution.ts | 0 solution/README.md | 1 - solution/README_EN.md | 1 - 12 files changed, 216 insertions(+), 456 deletions(-) delete mode 100644 solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README.md delete mode 100644 solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README_EN.md rename solution/3500-3599/{3506.Find Time Required to Eliminate Bacterial Strains II => 3506.Find Time Required to Eliminate Bacterial Strains}/Solution.cpp (100%) rename solution/3500-3599/{3506.Find Time Required to Eliminate Bacterial Strains II => 3506.Find Time Required to Eliminate Bacterial Strains}/Solution.go (100%) rename solution/3500-3599/{3506.Find Time Required to Eliminate Bacterial Strains II => 3506.Find Time Required to Eliminate Bacterial Strains}/Solution.java (100%) rename solution/3500-3599/{3506.Find Time Required to Eliminate Bacterial Strains II => 3506.Find Time Required to Eliminate Bacterial Strains}/Solution.py (100%) rename solution/3500-3599/{3506.Find Time Required to Eliminate Bacterial Strains II => 3506.Find Time Required to Eliminate Bacterial Strains}/Solution.rs (100%) rename solution/3500-3599/{3506.Find Time Required to Eliminate Bacterial Strains II => 3506.Find Time Required to Eliminate Bacterial Strains}/Solution.ts (100%) diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README.md deleted file mode 100644 index 56b96037f198f..0000000000000 --- a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README.md +++ /dev/null @@ -1,223 +0,0 @@ ---- -comments: true -difficulty: 困难 -edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README.md ---- - - - -# [3506. Find Time Required to Eliminate Bacterial Strains II 🔒](https://leetcode.cn/problems/find-time-required-to-eliminate-bacterial-strains-ii) - -[English Version](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README_EN.md) - -## 题目描述 - - - -

You are given an integer array timeReq and an integer splitTime.

- -

In the microscopic world of the human body, the immune system faces an extraordinary challenge: combatting a rapidly multiplying bacterial colony that threatens the body's survival.

- -

Initially, only one white blood cell (WBC) is deployed to eliminate the bacteria. However, the lone WBC quickly realizes it cannot keep up with the bacterial growth rate.

- -

The WBC devises a clever strategy to fight the bacteria:

- - - -

You must determine the minimum time required to eliminate all the bacterial strains.

- -

Note that the bacterial strains can be eliminated in any order.

- -

 

-

Example 1:

- -
-

Input: timeReq = [10,4,5], splitTime = 2

- -

Output: 12

- -

Explanation:

- -

The elimination process goes as follows:

- -
    -
  • Initially, there is a single WBC. The WBC splits into 2 WBCs after 2 units of time.
  • -
  • One of the WBCs eliminates strain 0 at a time t = 2 + 10 = 12. The other WBC splits again, using 2 units of time.
  • -
  • The 2 new WBCs eliminate the bacteria at times t = 2 + 2 + 4 and t = 2 + 2 + 5.
  • -
-
- -

Example 2:

- -
-

Input: timeReq = [10,4], splitTime = 5

- -

Output:15

- -

Explanation:

- -

The elimination process goes as follows:

- -
    -
  • Initially, there is a single WBC. The WBC splits into 2 WBCs after 5 units of time.
  • -
  • The 2 new WBCs eliminate the bacteria at times t = 5 + 10 and t = 5 + 4.
  • -
-
- -

 

-

Constraints:

- - - - - -## 解法 - - - -### 方法一:贪心 + 优先队列(小根堆) - -先考虑只有一种细菌的情况,此时不需要分裂白细胞,直接让他去消灭细菌,时间花费为 $\textit{timeSeq}[0]$。 - -如果有两种细菌,此时需要把白细胞分裂为两种,然后让它们分别去消灭细菌,时间花费为 $\textit{splitTime} + \max(\textit{timeSeq}[0], \textit{timeSeq}[1])$。 - -如果有超过两种细菌,此时每一步都需要考虑将几个白细胞进行分裂,正向思维不好处理。 - -我们不妨采用逆向思维,不分裂白细胞,而是将细菌进行合并。我们选取任意两种细菌 $i$, $j$ 进行合并,合并成一种新的细菌的时间为 $\textit{splitTime} + \max(\textit{timeSeq}[i], \textit{timeSeq}[j])$。 - -为了让耗时长的细菌尽可能少参与到合并中,我们可以每次贪心地选取耗时最小的两种细菌进行合并。因此,我们可以维护一个小根堆,每次取出最小的两种细菌进行合并,直到只剩下一种细菌。最后剩下的这个细菌的消灭时间就是答案。 - -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为细菌的数量。 - - - -#### Python3 - -```python -class Solution: - def minEliminationTime(self, timeReq: List[int], splitTime: int) -> int: - heapify(timeReq) - while len(timeReq) > 1: - heappop(timeReq) - heappush(timeReq, heappop(timeReq) + splitTime) - return timeReq[0] -``` - -#### Java - -```java -class Solution { - public long minEliminationTime(int[] timeReq, int splitTime) { - PriorityQueue q = new PriorityQueue<>(); - for (int x : timeReq) { - q.offer((long) x); - } - while (q.size() > 1) { - q.poll(); - q.offer(q.poll() + splitTime); - } - return q.poll(); - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - long long minEliminationTime(vector& timeReq, int splitTime) { - using ll = long long; - priority_queue, greater> pq; - for (int v : timeReq) { - pq.push(v); - } - while (pq.size() > 1) { - pq.pop(); - ll x = pq.top(); - pq.pop(); - pq.push(x + splitTime); - } - return pq.top(); - } -}; -``` - -#### Go - -```go -func minEliminationTime(timeReq []int, splitTime int) int64 { - pq := hp{} - for _, v := range timeReq { - heap.Push(&pq, v) - } - for pq.Len() > 1 { - heap.Pop(&pq) - heap.Push(&pq, heap.Pop(&pq).(int)+splitTime) - } - return int64(pq.IntSlice[0]) -} - -type hp struct{ sort.IntSlice } - -func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() any { - a := h.IntSlice - v := a[len(a)-1] - h.IntSlice = a[:len(a)-1] - return v -} -``` - -#### TypeScript - -```ts -function minEliminationTime(timeReq: number[], splitTime: number): number { - const pq = new MinPriorityQueue(); - for (const b of timeReq) { - pq.enqueue(b); - } - while (pq.size() > 1) { - pq.dequeue()!; - pq.enqueue(pq.dequeue()! + splitTime); - } - return pq.dequeue()!; -} -``` - -#### Rust - -```rust -use std::cmp::Reverse; -use std::collections::BinaryHeap; - -impl Solution { - pub fn min_elimination_time(time_req: Vec, split_time: i32) -> i64 { - let mut pq = BinaryHeap::new(); - for x in time_req { - pq.push(Reverse(x as i64)); - } - while pq.len() > 1 { - pq.pop(); - let merged = pq.pop().unwrap().0 + split_time as i64; - pq.push(Reverse(merged)); - } - pq.pop().unwrap().0 - } -} -``` - - - - - - diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README_EN.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README_EN.md deleted file mode 100644 index 159a59d166a8d..0000000000000 --- a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/README_EN.md +++ /dev/null @@ -1,223 +0,0 @@ ---- -comments: true -difficulty: Hard -edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README_EN.md ---- - - - -# [3506. Find Time Required to Eliminate Bacterial Strains II 🔒](https://leetcode.com/problems/find-time-required-to-eliminate-bacterial-strains-ii) - -[中文文档](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README.md) - -## Description - - - -

You are given an integer array timeReq and an integer splitTime.

- -

In the microscopic world of the human body, the immune system faces an extraordinary challenge: combatting a rapidly multiplying bacterial colony that threatens the body's survival.

- -

Initially, only one white blood cell (WBC) is deployed to eliminate the bacteria. However, the lone WBC quickly realizes it cannot keep up with the bacterial growth rate.

- -

The WBC devises a clever strategy to fight the bacteria:

- -
    -
  • The ith bacterial strain takes timeReq[i] units of time to be eliminated.
  • -
  • A single WBC can eliminate only one bacterial strain. Afterwards, the WBC is exhausted and cannot perform any other tasks.
  • -
  • A WBC can split itself into two WBCs, but this requires splitTime units of time. Once split, the two WBCs can work in parallel on eliminating the bacteria.
  • -
  • Only one WBC can work on a single bacterial strain. Multiple WBCs cannot attack one strain in parallel.
  • -
- -

You must determine the minimum time required to eliminate all the bacterial strains.

- -

Note that the bacterial strains can be eliminated in any order.

- -

 

-

Example 1:

- -
-

Input: timeReq = [10,4,5], splitTime = 2

- -

Output: 12

- -

Explanation:

- -

The elimination process goes as follows:

- -
    -
  • Initially, there is a single WBC. The WBC splits into 2 WBCs after 2 units of time.
  • -
  • One of the WBCs eliminates strain 0 at a time t = 2 + 10 = 12. The other WBC splits again, using 2 units of time.
  • -
  • The 2 new WBCs eliminate the bacteria at times t = 2 + 2 + 4 and t = 2 + 2 + 5.
  • -
-
- -

Example 2:

- -
-

Input: timeReq = [10,4], splitTime = 5

- -

Output:15

- -

Explanation:

- -

The elimination process goes as follows:

- -
    -
  • Initially, there is a single WBC. The WBC splits into 2 WBCs after 5 units of time.
  • -
  • The 2 new WBCs eliminate the bacteria at times t = 5 + 10 and t = 5 + 4.
  • -
-
- -

 

-

Constraints:

- -
    -
  • 2 <= timeReq.length <= 105
  • -
  • 1 <= timeReq[i] <= 109
  • -
  • 1 <= splitTime <= 109
  • -
- - - -## Solutions - - - -### Solution 1: Greedy + Priority Queue (Min-Heap) - -First, consider the case where there is only one type of bacteria. In this case, there is no need to split the white blood cell (WBC); it can directly eliminate the bacteria, and the time cost is $\textit{timeSeq}[0]$. - -If there are two types of bacteria, the WBC needs to split into two, and each WBC eliminates one type of bacteria. The time cost is $\textit{splitTime} + \max(\textit{timeSeq}[0], \textit{timeSeq}[1])$. - -If there are more than two types of bacteria, at each step, we need to consider splitting the WBCs into multiple cells, which is difficult to handle with a forward-thinking approach. - -Instead, we can adopt a reverse-thinking approach: instead of splitting the WBCs, we merge the bacteria. We select any two types of bacteria $i$ and $j$ to merge into a new type of bacteria. The time cost for this merge is $\textit{splitTime} + \max(\textit{timeSeq}[i], \textit{timeSeq}[j])$. - -To minimize the involvement of bacteria with long elimination times in the merging process, we can greedily select the two bacteria with the smallest elimination times for merging at each step. Therefore, we can maintain a min-heap, repeatedly extracting the two bacteria with the smallest elimination times and merging them until only one type of bacteria remains. The elimination time of this final bacteria is the answer. - -The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the number of bacteria. - - - -#### Python3 - -```python -class Solution: - def minEliminationTime(self, timeReq: List[int], splitTime: int) -> int: - heapify(timeReq) - while len(timeReq) > 1: - heappop(timeReq) - heappush(timeReq, heappop(timeReq) + splitTime) - return timeReq[0] -``` - -#### Java - -```java -class Solution { - public long minEliminationTime(int[] timeReq, int splitTime) { - PriorityQueue q = new PriorityQueue<>(); - for (int x : timeReq) { - q.offer((long) x); - } - while (q.size() > 1) { - q.poll(); - q.offer(q.poll() + splitTime); - } - return q.poll(); - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - long long minEliminationTime(vector& timeReq, int splitTime) { - using ll = long long; - priority_queue, greater> pq; - for (int v : timeReq) { - pq.push(v); - } - while (pq.size() > 1) { - pq.pop(); - ll x = pq.top(); - pq.pop(); - pq.push(x + splitTime); - } - return pq.top(); - } -}; -``` - -#### Go - -```go -func minEliminationTime(timeReq []int, splitTime int) int64 { - pq := hp{} - for _, v := range timeReq { - heap.Push(&pq, v) - } - for pq.Len() > 1 { - heap.Pop(&pq) - heap.Push(&pq, heap.Pop(&pq).(int)+splitTime) - } - return int64(pq.IntSlice[0]) -} - -type hp struct{ sort.IntSlice } - -func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() any { - a := h.IntSlice - v := a[len(a)-1] - h.IntSlice = a[:len(a)-1] - return v -} -``` - -#### TypeScript - -```ts -function minEliminationTime(timeReq: number[], splitTime: number): number { - const pq = new MinPriorityQueue(); - for (const b of timeReq) { - pq.enqueue(b); - } - while (pq.size() > 1) { - pq.dequeue()!; - pq.enqueue(pq.dequeue()! + splitTime); - } - return pq.dequeue()!; -} -``` - -#### Rust - -```rust -use std::cmp::Reverse; -use std::collections::BinaryHeap; - -impl Solution { - pub fn min_elimination_time(time_req: Vec, split_time: i32) -> i64 { - let mut pq = BinaryHeap::new(); - for x in time_req { - pq.push(Reverse(x as i64)); - } - while pq.len() > 1 { - pq.pop(); - let merged = pq.pop().unwrap().0 + split_time as i64; - pq.push(Reverse(merged)); - } - pq.pop().unwrap().0 - } -} -``` - - - - - - diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md index 03e632d4a6c3f..2e81320abb412 100644 --- a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md +++ b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README.md @@ -86,32 +86,136 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi -### 方法一 +### 方法一:贪心 + 优先队列(小根堆) + +先考虑只有一种细菌的情况,此时不需要分裂白细胞,直接让他去消灭细菌,时间花费为 $\textit{timeSeq}[0]$。 + +如果有两种细菌,此时需要把白细胞分裂为两种,然后让它们分别去消灭细菌,时间花费为 $\textit{splitTime} + \max(\textit{timeSeq}[0], \textit{timeSeq}[1])$。 + +如果有超过两种细菌,此时每一步都需要考虑将几个白细胞进行分裂,正向思维不好处理。 + +我们不妨采用逆向思维,不分裂白细胞,而是将细菌进行合并。我们选取任意两种细菌 $i$, $j$ 进行合并,合并成一种新的细菌的时间为 $\textit{splitTime} + \max(\textit{timeSeq}[i], \textit{timeSeq}[j])$。 + +为了让耗时长的细菌尽可能少参与到合并中,我们可以每次贪心地选取耗时最小的两种细菌进行合并。因此,我们可以维护一个小根堆,每次取出最小的两种细菌进行合并,直到只剩下一种细菌。最后剩下的这个细菌的消灭时间就是答案。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为细菌的数量。 #### Python3 ```python - +class Solution: + def minEliminationTime(self, timeReq: List[int], splitTime: int) -> int: + heapify(timeReq) + while len(timeReq) > 1: + heappop(timeReq) + heappush(timeReq, heappop(timeReq) + splitTime) + return timeReq[0] ``` #### Java ```java - +class Solution { + public long minEliminationTime(int[] timeReq, int splitTime) { + PriorityQueue q = new PriorityQueue<>(); + for (int x : timeReq) { + q.offer((long) x); + } + while (q.size() > 1) { + q.poll(); + q.offer(q.poll() + splitTime); + } + return q.poll(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long minEliminationTime(vector& timeReq, int splitTime) { + using ll = long long; + priority_queue, greater> pq; + for (int v : timeReq) { + pq.push(v); + } + while (pq.size() > 1) { + pq.pop(); + ll x = pq.top(); + pq.pop(); + pq.push(x + splitTime); + } + return pq.top(); + } +}; ``` #### Go ```go +func minEliminationTime(timeReq []int, splitTime int) int64 { + pq := hp{} + for _, v := range timeReq { + heap.Push(&pq, v) + } + for pq.Len() > 1 { + heap.Pop(&pq) + heap.Push(&pq, heap.Pop(&pq).(int)+splitTime) + } + return int64(pq.IntSlice[0]) +} + +type hp struct{ sort.IntSlice } + +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} +``` + +#### TypeScript + +```ts +function minEliminationTime(timeReq: number[], splitTime: number): number { + const pq = new MinPriorityQueue(); + for (const b of timeReq) { + pq.enqueue(b); + } + while (pq.size() > 1) { + pq.dequeue()!; + pq.enqueue(pq.dequeue()! + splitTime); + } + return pq.dequeue()!; +} +``` +#### Rust + +```rust +use std::cmp::Reverse; +use std::collections::BinaryHeap; + +impl Solution { + pub fn min_elimination_time(time_req: Vec, split_time: i32) -> i64 { + let mut pq = BinaryHeap::new(); + for x in time_req { + pq.push(Reverse(x as i64)); + } + while pq.len() > 1 { + pq.pop(); + let merged = pq.pop().unwrap().0 + split_time as i64; + pq.push(Reverse(merged)); + } + pq.pop().unwrap().0 + } +} ``` diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md index 6f3cfbf295cad..f15ef72f3875e 100644 --- a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md +++ b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/README_EN.md @@ -84,32 +84,136 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3506.Fi -### Solution 1 +### Solution 1: Greedy + Priority Queue (Min-Heap) + +First, consider the case where there is only one type of bacteria. In this case, there is no need to split the white blood cell (WBC); it can directly eliminate the bacteria, and the time cost is $\textit{timeSeq}[0]$. + +If there are two types of bacteria, the WBC needs to split into two, and each WBC eliminates one type of bacteria. The time cost is $\textit{splitTime} + \max(\textit{timeSeq}[0], \textit{timeSeq}[1])$. + +If there are more than two types of bacteria, at each step, we need to consider splitting the WBCs into multiple cells, which is difficult to handle with a forward-thinking approach. + +Instead, we can adopt a reverse-thinking approach: instead of splitting the WBCs, we merge the bacteria. We select any two types of bacteria $i$ and $j$ to merge into a new type of bacteria. The time cost for this merge is $\textit{splitTime} + \max(\textit{timeSeq}[i], \textit{timeSeq}[j])$. + +To minimize the involvement of bacteria with long elimination times in the merging process, we can greedily select the two bacteria with the smallest elimination times for merging at each step. Therefore, we can maintain a min-heap, repeatedly extracting the two bacteria with the smallest elimination times and merging them until only one type of bacteria remains. The elimination time of this final bacteria is the answer. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the number of bacteria. #### Python3 ```python - +class Solution: + def minEliminationTime(self, timeReq: List[int], splitTime: int) -> int: + heapify(timeReq) + while len(timeReq) > 1: + heappop(timeReq) + heappush(timeReq, heappop(timeReq) + splitTime) + return timeReq[0] ``` #### Java ```java - +class Solution { + public long minEliminationTime(int[] timeReq, int splitTime) { + PriorityQueue q = new PriorityQueue<>(); + for (int x : timeReq) { + q.offer((long) x); + } + while (q.size() > 1) { + q.poll(); + q.offer(q.poll() + splitTime); + } + return q.poll(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long minEliminationTime(vector& timeReq, int splitTime) { + using ll = long long; + priority_queue, greater> pq; + for (int v : timeReq) { + pq.push(v); + } + while (pq.size() > 1) { + pq.pop(); + ll x = pq.top(); + pq.pop(); + pq.push(x + splitTime); + } + return pq.top(); + } +}; ``` #### Go ```go +func minEliminationTime(timeReq []int, splitTime int) int64 { + pq := hp{} + for _, v := range timeReq { + heap.Push(&pq, v) + } + for pq.Len() > 1 { + heap.Pop(&pq) + heap.Push(&pq, heap.Pop(&pq).(int)+splitTime) + } + return int64(pq.IntSlice[0]) +} + +type hp struct{ sort.IntSlice } + +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} +``` + +#### TypeScript + +```ts +function minEliminationTime(timeReq: number[], splitTime: number): number { + const pq = new MinPriorityQueue(); + for (const b of timeReq) { + pq.enqueue(b); + } + while (pq.size() > 1) { + pq.dequeue()!; + pq.enqueue(pq.dequeue()! + splitTime); + } + return pq.dequeue()!; +} +``` +#### Rust + +```rust +use std::cmp::Reverse; +use std::collections::BinaryHeap; + +impl Solution { + pub fn min_elimination_time(time_req: Vec, split_time: i32) -> i64 { + let mut pq = BinaryHeap::new(); + for x in time_req { + pq.push(Reverse(x as i64)); + } + while pq.len() > 1 { + pq.pop(); + let merged = pq.pop().unwrap().0 + split_time as i64; + pq.push(Reverse(merged)); + } + pq.pop().unwrap().0 + } +} ``` diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.cpp b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.cpp similarity index 100% rename from solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.cpp rename to solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.cpp diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.go b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.go similarity index 100% rename from solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.go rename to solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.go diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.java b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.java similarity index 100% rename from solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.java rename to solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.java diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.py b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.py similarity index 100% rename from solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.py rename to solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.py diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.rs b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.rs similarity index 100% rename from solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.rs rename to solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.rs diff --git a/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.ts b/solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.ts similarity index 100% rename from solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains II/Solution.ts rename to solution/3500-3599/3506.Find Time Required to Eliminate Bacterial Strains/Solution.ts diff --git a/solution/README.md b/solution/README.md index 8699c3e8db3ab..b36d44794bc2f 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3516,7 +3516,6 @@ | 3503 | [子字符串连接后的最长回文串 I](/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README.md) | `双指针`,`字符串`,`动态规划`,`枚举` | 中等 | 第 443 场周赛 | | 3504 | [子字符串连接后的最长回文串 II](/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README.md) | `双指针`,`字符串`,`动态规划` | 困难 | 第 443 场周赛 | | 3505 | [使 K 个子数组内元素相等的最少操作数](/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README.md) | `数组`,`哈希表`,`数学`,`动态规划`,`滑动窗口`,`堆(优先队列)` | 困难 | 第 443 场周赛 | -| 3506 | [Find Time Required to Eliminate Bacterial Strains II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README.md) | | 困难 | 🔒 | | 3506 | [查找消除细菌菌株所需时间 II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README.md) | | 困难 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index e53de1cdb4764..7d464947014d6 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3514,7 +3514,6 @@ Press Control + F(or Command + F on | 3503 | [Longest Palindrome After Substring Concatenation I](/solution/3500-3599/3503.Longest%20Palindrome%20After%20Substring%20Concatenation%20I/README_EN.md) | `Two Pointers`,`String`,`Dynamic Programming`,`Enumeration` | Medium | Weekly Contest 443 | | 3504 | [Longest Palindrome After Substring Concatenation II](/solution/3500-3599/3504.Longest%20Palindrome%20After%20Substring%20Concatenation%20II/README_EN.md) | `Two Pointers`,`String`,`Dynamic Programming` | Hard | Weekly Contest 443 | | 3505 | [Minimum Operations to Make Elements Within K Subarrays Equal](/solution/3500-3599/3505.Minimum%20Operations%20to%20Make%20Elements%20Within%20K%20Subarrays%20Equal/README_EN.md) | `Array`,`Hash Table`,`Math`,`Dynamic Programming`,`Sliding Window`,`Heap (Priority Queue)` | Hard | Weekly Contest 443 | -| 3506 | [Find Time Required to Eliminate Bacterial Strains II](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains%20II/README_EN.md) | | Hard | 🔒 | | 3506 | [Find Time Required to Eliminate Bacterial Strains](/solution/3500-3599/3506.Find%20Time%20Required%20to%20Eliminate%20Bacterial%20Strains/README_EN.md) | | Hard | 🔒 | ## Copyright