From 4610f293f0313c67374255b05dc4e692a0e72b4c Mon Sep 17 00:00:00 2001
From: rain84 表: 订阅服务想要分析用户行为模式。公司提供7天免费试用,试用结束后,用户可以选择订阅 付费计划 或 取消。编写解决方案: 返回结果表以 结果格式如下所示。 示例: 输入: UserActivity 表: 输出: 解释: 结果表仅包括从免费试用转为付费订阅的用户(用户 1,3 和 4),并且以 user_id 升序排序。 Table: A subscription service wants to analyze user behavior patterns. The company offers a Return the result table ordered by The result format is in the following example. Example: Input: UserActivity table: Output: Explanation: The result table only includes users who converted from free trial to paid subscription (users 1, 3, and 4), and is ordered by user_id in ascending order.
输入:grid = [[2,4],[6,8]], x = 2
输出:4
-解释:可以执行下述操作使所有元素都等于 4 :
+解释:可以执行下述操作使所有元素都等于 4 :
- 2 加 x 一次。
- 6 减 x 一次。
- 8 减 x 两次。
@@ -194,6 +194,44 @@ func abs(x int) int {
}
```
+#### TypeScript
+
+```ts
+function minOperations(grid: number[][], x: number): number {
+ const arr = grid.flat(2);
+ arr.sort((a, b) => a - b);
+ const median = arr[Math.floor(arr.length / 2)];
+
+ let res = 0;
+ for (const val of arr) {
+ const c = Math.abs(val - median) / x;
+ if (c !== (c | 0)) return -1;
+ res += c;
+ }
+
+ return res;
+}
+```
+
+#### JavaScript
+
+```js
+function minOperations(grid, x) {
+ const arr = grid.flat(2);
+ arr.sort((a, b) => a - b);
+ const median = arr[Math.floor(arr.length / 2)];
+
+ let res = 0;
+ for (const val of arr) {
+ const c = Math.abs(val - median) / x;
+ if (c !== (c | 0)) return -1;
+ res += c;
+ }
+
+ return res;
+}
+```
+
diff --git a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md
index 202e609bd8de3..69ac6cc530a98 100644
--- a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md
+++ b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md
@@ -33,7 +33,7 @@ tags:
Input: grid = [[2,4],[6,8]], x = 2
Output: 4
-Explanation: We can make every element equal to 4 by doing the following:
+Explanation: We can make every element equal to 4 by doing the following:
- Add x to 2 once.
- Subtract x from 6 once.
- Subtract x from 8 twice.
@@ -186,6 +186,44 @@ func abs(x int) int {
}
```
+#### TypeScript
+
+```ts
+function minOperations(grid: number[][], x: number): number {
+ const arr = grid.flat(2);
+ arr.sort((a, b) => a - b);
+ const median = arr[Math.floor(arr.length / 2)];
+
+ let res = 0;
+ for (const val of arr) {
+ const c = Math.abs(val - median) / x;
+ if (c !== (c | 0)) return -1;
+ res += c;
+ }
+
+ return res;
+}
+```
+
+#### JavaScript
+
+```js
+function minOperations(grid, x) {
+ const arr = grid.flat(2);
+ arr.sort((a, b) => a - b);
+ const median = arr[Math.floor(arr.length / 2)];
+
+ let res = 0;
+ for (const val of arr) {
+ const c = Math.abs(val - median) / x;
+ if (c !== (c | 0)) return -1;
+ res += c;
+ }
+
+ return res;
+}
+```
+
diff --git a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.js b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.js
new file mode 100644
index 0000000000000..43d9b37cd033c
--- /dev/null
+++ b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.js
@@ -0,0 +1,14 @@
+function minOperations(grid, x) {
+ const arr = grid.flat(2);
+ arr.sort((a, b) => a - b);
+ const median = arr[Math.floor(arr.length / 2)];
+
+ let res = 0;
+ for (const val of arr) {
+ const c = Math.abs(val - median) / x;
+ if (c !== (c | 0)) return -1;
+ res += c;
+ }
+
+ return res;
+}
diff --git a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.ts b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.ts
new file mode 100644
index 0000000000000..e470b9bcdb6c9
--- /dev/null
+++ b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.ts
@@ -0,0 +1,14 @@
+function minOperations(grid: number[][], x: number): number {
+ const arr = grid.flat(2);
+ arr.sort((a, b) => a - b);
+ const median = arr[Math.floor(arr.length / 2)];
+
+ let res = 0;
+ for (const val of arr) {
+ const c = Math.abs(val - median) / x;
+ if (c !== (c | 0)) return -1;
+ res += c;
+ }
+
+ return res;
+}
From fe7ee6f6063362871a35470ded8359c8c011317c Mon Sep 17 00:00:00 2001
From: Libin YANG
输入:grid = [[2,4],[6,8]], x = 2
输出:4
-解释:可以执行下述操作使所有元素都等于 4 :
+解释:可以执行下述操作使所有元素都等于 4 :
- 2 加 x 一次。
- 6 减 x 一次。
- 8 减 x 两次。
diff --git a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md
index 69ac6cc530a98..557513f032941 100644
--- a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md
+++ b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/README_EN.md
@@ -33,7 +33,7 @@ tags:
Input: grid = [[2,4],[6,8]], x = 2
Output: 4
-Explanation: We can make every element equal to 4 by doing the following:
+Explanation: We can make every element equal to 4 by doing the following:
- Add x to 2 once.
- Subtract x from 6 once.
- Subtract x from 8 twice.
diff --git a/solution/3400-3499/3497.Analyze Subscription Conversion/README.md b/solution/3400-3499/3497.Analyze Subscription Conversion/README.md
new file mode 100644
index 0000000000000..2318cffe94d0c
--- /dev/null
+++ b/solution/3400-3499/3497.Analyze Subscription Conversion/README.md
@@ -0,0 +1,220 @@
+---
+comments: true
+difficulty: 中等
+edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README.md
+tags:
+ - 数据库
+---
+
+
+
+# [3497. 分析订阅转化](https://leetcode.cn/problems/analyze-subscription-conversion)
+
+[English Version](/solution/3400-3499/3497.Analyze%20Subscription%20Conversion/README_EN.md)
+
+## 题目描述
+
+
+
+
UserActivity
++------------------+---------+
+| Column Name | Type |
++------------------+---------+
+| user_id | int |
+| activity_date | date |
+| activity_type | varchar |
+| activity_duration| int |
++------------------+---------+
+(user_id, activity_date, activity_type) 是这张表的唯一主键。
+activity_type 是('free_trial', 'paid', 'cancelled')中的一个。
+activity_duration 是用户当天在平台上花费的分钟数。
+每一行表示一个用户在特定日期的活动。
+
+
+
+
+
+2
位)2
位)user_id
升序 排序。
++---------+---------------+---------------+-------------------+
+| user_id | activity_date | activity_type | activity_duration |
++---------+---------------+---------------+-------------------+
+| 1 | 2023-01-01 | free_trial | 45 |
+| 1 | 2023-01-02 | free_trial | 30 |
+| 1 | 2023-01-05 | free_trial | 60 |
+| 1 | 2023-01-10 | paid | 75 |
+| 1 | 2023-01-12 | paid | 90 |
+| 1 | 2023-01-15 | paid | 65 |
+| 2 | 2023-02-01 | free_trial | 55 |
+| 2 | 2023-02-03 | free_trial | 25 |
+| 2 | 2023-02-07 | free_trial | 50 |
+| 2 | 2023-02-10 | cancelled | 0 |
+| 3 | 2023-03-05 | free_trial | 70 |
+| 3 | 2023-03-06 | free_trial | 60 |
+| 3 | 2023-03-08 | free_trial | 80 |
+| 3 | 2023-03-12 | paid | 50 |
+| 3 | 2023-03-15 | paid | 55 |
+| 3 | 2023-03-20 | paid | 85 |
+| 4 | 2023-04-01 | free_trial | 40 |
+| 4 | 2023-04-03 | free_trial | 35 |
+| 4 | 2023-04-05 | paid | 45 |
+| 4 | 2023-04-07 | cancelled | 0 |
++---------+---------------+---------------+-------------------+
+
+
+
++---------+--------------------+-------------------+
+| user_id | trial_avg_duration | paid_avg_duration |
++---------+--------------------+-------------------+
+| 1 | 45.00 | 76.67 |
+| 3 | 70.00 | 63.33 |
+| 4 | 37.50 | 45.00 |
++---------+--------------------+-------------------+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ UserActivity
++------------------+---------+
+| Column Name | Type |
++------------------+---------+
+| user_id | int |
+| activity_date | date |
+| activity_type | varchar |
+| activity_duration| int |
++------------------+---------+
+(user_id, activity_date, activity_type) is the unique key for this table.
+activity_type is one of ('free_trial', 'paid', 'cancelled').
+activity_duration is the number of minutes the user spent on the platform that day.
+Each row represents a user's activity on a specific date.
+
+
+7
-day free trial, after which users can subscribe to a paid plan or cancel. Write a solution to:
+
+
+2
decimal places)2
decimal places)user_id
in ascending order.
++---------+---------------+---------------+-------------------+
+| user_id | activity_date | activity_type | activity_duration |
++---------+---------------+---------------+-------------------+
+| 1 | 2023-01-01 | free_trial | 45 |
+| 1 | 2023-01-02 | free_trial | 30 |
+| 1 | 2023-01-05 | free_trial | 60 |
+| 1 | 2023-01-10 | paid | 75 |
+| 1 | 2023-01-12 | paid | 90 |
+| 1 | 2023-01-15 | paid | 65 |
+| 2 | 2023-02-01 | free_trial | 55 |
+| 2 | 2023-02-03 | free_trial | 25 |
+| 2 | 2023-02-07 | free_trial | 50 |
+| 2 | 2023-02-10 | cancelled | 0 |
+| 3 | 2023-03-05 | free_trial | 70 |
+| 3 | 2023-03-06 | free_trial | 60 |
+| 3 | 2023-03-08 | free_trial | 80 |
+| 3 | 2023-03-12 | paid | 50 |
+| 3 | 2023-03-15 | paid | 55 |
+| 3 | 2023-03-20 | paid | 85 |
+| 4 | 2023-04-01 | free_trial | 40 |
+| 4 | 2023-04-03 | free_trial | 35 |
+| 4 | 2023-04-05 | paid | 45 |
+| 4 | 2023-04-07 | cancelled | 0 |
++---------+---------------+---------------+-------------------+
+
+
+
++---------+--------------------+-------------------+
+| user_id | trial_avg_duration | paid_avg_duration |
++---------+--------------------+-------------------+
+| 1 | 45.00 | 76.67 |
+| 3 | 70.00 | 63.33 |
+| 4 | 37.50 | 45.00 |
++---------+--------------------+-------------------+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
给你一个字符串 s
,计算其 反转度。
反转度的计算方法如下:
+ +'a'
= 26, 'b'
= 25, ..., 'z'
= 1)与其在字符串中的位置(下标从1 开始)相乘。返回 反转度。
+ ++ +
示例 1:
+ +输入: s = "abc"
+ +输出: 148
+ +解释:
+ +字母 | +反转字母表中的位置 | +字符串中的位置 | +乘积 | +
---|---|---|---|
'a' |
+ 26 | +1 | +26 | +
'b' |
+ 25 | +2 | +50 | +
'c' |
+ 24 | +3 | +72 | +
反转度是 26 + 50 + 72 = 148
。
示例 2:
+ +输入: s = "zaza"
+ +输出: 160
+ +解释:
+ +字母 | +反转字母表中的位置 | +字符串中的位置 | +乘积 | +
---|---|---|---|
'z' |
+ 1 | +1 | +1 | +
'a' |
+ 26 | +2 | +52 | +
'z' |
+ 1 | +3 | +3 | +
'a' |
+ 26 | +4 | +104 | +
反转度是 1 + 52 + 3 + 104 = 160
。
+ +
提示:
+ +1 <= s.length <= 1000
s
仅包含小写字母。Given a string s
, calculate its reverse degree.
The reverse degree is calculated as follows:
+ +'a'
= 26, 'b'
= 25, ..., 'z'
= 1) with its position in the string (1-indexed).Return the reverse degree of s
.
+
Example 1:
+ +Input: s = "abc"
+ +Output: 148
+ +Explanation:
+ +Letter | +Index in Reversed Alphabet | +Index in String | +Product | +
---|---|---|---|
'a' |
+ 26 | +1 | +26 | +
'b' |
+ 25 | +2 | +50 | +
'c' |
+ 24 | +3 | +72 | +
The reversed degree is 26 + 50 + 72 = 148
.
Example 2:
+ +Input: s = "zaza"
+ +Output: 160
+ +Explanation:
+ +Letter | +Index in Reversed Alphabet | +Index in String | +Product | +
---|---|---|---|
'z' |
+ 1 | +1 | +1 | +
'a' |
+ 26 | +2 | +52 | +
'z' |
+ 1 | +3 | +3 | +
'a' |
+ 26 | +4 | +104 | +
The reverse degree is 1 + 52 + 3 + 104 = 160
.
+
Constraints:
+ +1 <= s.length <= 1000
s
contains only lowercase English letters.给你一个长度为 n
的二进制字符串 s
,其中:
'1'
表示一个 活跃 区段。'0'
表示一个 非活跃 区段。你可以执行 最多一次操作 来最大化 s
中的活跃区段数量。在一次操作中,你可以:
'0'
包围的连续 '1'
区块转换为全 '0'
。'1'
包围的连续 '0'
区块转换为全 '1'
。返回在执行最优操作后,s
中的 最大 活跃区段数。
注意:处理时需要在 s
的两侧加上 '1'
,即 t = '1' + s + '1'
。这些加上的 '1'
不会影响最终的计数。
+ +
示例 1:
+ +输入: s = "01"
+ +输出: 1
+ +解释:
+ +因为没有被 '0'
包围的 '1'
区块,因此无法进行有效操作。最大活跃区段数为 1。
示例 2:
+ +输入: s = "0100"
+ +输出: 4
+ +解释:
+ +"0100"
→ 两端加上 '1'
后得到 "101001"
。"0100"
,"101001"
→ "100001"
→ "111111"
。'1'
后为 "1111"
。最大活跃区段数为 4。示例 3:
+ +输入: s = "1000100"
+ +输出: 7
+ +解释:
+ +"1000100"
→ 两端加上 '1'
后得到 "110001001"
。"000100"
,"110001001"
→ "110000001"
→ "111111111"
。'1'
后为 "1111111"
。最大活跃区段数为 7。示例 4:
+ +输入: s = "01010"
+ +输出: 4
+ +解释:
+ +"01010"
→ 两端加上 '1'
后得到 "1010101"
。"010"
,"1010101"
→ "1000101"
→ "1111101"
。'1'
后为 "11110"
。最大活跃区段数为 4。+ +
提示:
+ +1 <= n == s.length <= 105
s[i]
仅包含 '0'
或 '1'
You are given a binary string s
of length n
, where:
'1'
represents an active section.'0'
represents an inactive section.You can perform at most one trade to maximize the number of active sections in s
. In a trade, you:
'1'
s that is surrounded by '0'
s to all '0'
s.'0'
s that is surrounded by '1'
s to all '1'
s.Return the maximum number of active sections in s
after making the optimal trade.
Note: Treat s
as if it is augmented with a '1'
at both ends, forming t = '1' + s + '1'
. The augmented '1'
s do not contribute to the final count.
+
Example 1:
+ +Input: s = "01"
+ +Output: 1
+ +Explanation:
+ +Because there is no block of '1'
s surrounded by '0'
s, no valid trade is possible. The maximum number of active sections is 1.
Example 2:
+ +Input: s = "0100"
+ +Output: 4
+ +Explanation:
+ +"0100"
→ Augmented to "101001"
."0100"
, convert "101001"
→ "100001"
→ "111111"
."1111"
. The maximum number of active sections is 4.Example 3:
+ +Input: s = "1000100"
+ +Output: 7
+ +Explanation:
+ +"1000100"
→ Augmented to "110001001"
."000100"
, convert "110001001"
→ "110000001"
→ "111111111"
."1111111"
. The maximum number of active sections is 7.Example 4:
+ +Input: s = "01010"
+ +Output: 4
+ +Explanation:
+ +"01010"
→ Augmented to "1010101"
."010"
, convert "1010101"
→ "1000101"
→ "1111101"
."11110"
. The maximum number of active sections is 4.+
Constraints:
+ +1 <= n == s.length <= 105
s[i]
is either '0'
or '1'
给你两个长度相等的整数数组 nums
和 cost
,和一个整数 k
。
你可以将 nums
分割成多个子数组。第 i
个子数组由元素 nums[l..r]
组成,其代价为:
(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])
。注意,i
表示子数组的顺序:第一个子数组为 1,第二个为 2,依此类推。
返回通过任何有效划分得到的 最小 总代价。
+ +子数组 是一个连续的 非空 元素序列。
+ ++ +
示例 1:
+ +输入: nums = [3,1,4], cost = [4,6,6], k = 1
+ +输出: 110
+ +解释:
+将nums
分割为子数组 [3, 1]
和 [4]
,得到最小总代价。
+
+[3,1]
的代价是 (3 + 1 + 1 * 1) * (4 + 6) = 50
。[4]
的代价是 (3 + 1 + 4 + 1 * 2) * 6 = 60
。示例 2:
+ +输入: nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7
+ +输出: 985
+ +解释:
+将nums
分割为子数组 [4, 8, 5, 1]
,[14, 2, 2]
和 [12, 1]
,得到最小总代价。
+
+[4, 8, 5, 1]
的代价是 (4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525
。[14, 2, 2]
的代价是 (4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250
。[12, 1]
的代价是 (4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210
。+ +
提示:
+ +1 <= nums.length <= 1000
cost.length == nums.length
1 <= nums[i], cost[i] <= 1000
1 <= k <= 1000
You are given two integer arrays, nums
and cost
, of the same size, and an integer k
.
You can divide nums
into subarrays. The cost of the ith
subarray consisting of elements nums[l..r]
is:
(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])
.Note that i
represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.
Return the minimum total cost possible from any valid division.
+ +A subarray is a contiguous non-empty sequence of elements within an array.
+ ++
Example 1:
+ +Input: nums = [3,1,4], cost = [4,6,6], k = 1
+ +Output: 110
+ +Explanation:
+The minimum total cost possible can be achieved by dividingnums
into subarrays [3, 1]
and [4]
.
+
+[3,1]
is (3 + 1 + 1 * 1) * (4 + 6) = 50
.[4]
is (3 + 1 + 4 + 1 * 2) * 6 = 60
.Example 2:
+ +Input: nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7
+ +Output: 985
+ +Explanation:
+The minimum total cost possible can be achieved by dividingnums
into subarrays [4, 8, 5, 1]
, [14, 2, 2]
, and [12, 1]
.
+
+[4, 8, 5, 1]
is (4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525
.[14, 2, 2]
is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250
.[12, 1]
is (4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210
.+
Constraints:
+ +1 <= nums.length <= 1000
cost.length == nums.length
1 <= nums[i], cost[i] <= 1000
1 <= k <= 1000
给你一个长度为 n
的二进制字符串 s
,其中:
'1'
表示一个 活跃 区域。'0'
表示一个 非活跃 区域。你最多可以进行一次 操作 来最大化 s
中活跃区间的数量。在一次操作中,你可以:
'0'
包围的连续 '1'
区域转换为全 '0'
。'1'
包围的连续 '0'
区域转换为全 '1'
。此外,你还有一个 二维数组 queries
,其中 queries[i] = [li, ri]
表示子字符串 s[li...ri]
。
对于每个查询,确定在对子字符串 s[li...ri]
进行最优交换后,字符串 s
中 可能的最大 活跃区间数。
返回一个数组 answer
,其中 answer[i]
是 queries[i]
的结果。
注意
+ +s[li...ri]
处理时,将其看作是在两端都加上一个 '1'
后的字符串,形成 t = '1' + s[li...ri] + '1'
。这些额外的 '1'
不会对最终的活跃区间数有贡献。+ +
示例 1:
+ +输入: s = "01", queries = [[0,1]]
+ +输出: [1]
+ +解释:
+ +因为没有被 '0'
包围的 '1'
区域,所以没有有效的操作可以进行。最大活跃区间数是 1。
示例 2:
+ +输入: s = "0100", queries = [[0,3],[0,2],[1,3],[2,3]]
+ +输出: [4,3,1,1]
+ +解释:
+ +查询 [0, 3]
→ 子字符串 "0100"
→ 变为 "101001"
+ 选择 "0100"
,"0100"
→ "0000"
→ "1111"
。
+ 最终字符串(去掉添加的 '1'
)为 "1111"
。最大活跃区间数为 4。
查询 [0, 2]
→ 子字符串 "010"
→ 变为 "10101"
+ 选择 "010"
,"010"
→ "000"
→ "111"
。
+ 最终字符串(去掉添加的 '1'
)为 "1110"
。最大活跃区间数为 3。
查询 [1, 3]
→ 子字符串 "100"
→ 变为 "11001"
+ 因为没有被 '0'
包围的 '1'
区域,所以没有有效的操作可以进行。最大活跃区间数为 1。
查询 [2, 3]
→ 子字符串 "00"
→ 变为 "1001"
+ 因为没有被 '0'
包围的 '1'
区域,所以没有有效的操作可以进行。最大活跃区间数为 1。
示例 3:
+ +输入: s = "1000100", queries = [[1,5],[0,6],[0,4]]
+ +输出: [6,7,2]
+ +解释:
+ +查询 [1, 5]
→ 子字符串 "00010"
→ 变为 "1000101"
+ 选择 "00010"
,"00010"
→ "00000"
→ "11111"
。
+ 最终字符串(去掉添加的 '1'
)为 "1111110"
。最大活跃区间数为 6。
查询 [0, 6]
→ 子字符串 "1000100"
→ 变为 "110001001"
+ 选择 "000100"
,"000100"
→ "000000"
→ "111111"
。
+ 最终字符串(去掉添加的 '1'
)为 "1111111"
。最大活跃区间数为 7。
查询 [0, 4]
→ 子字符串 "10001"
→ 变为 "1100011"
+ 因为没有被 '0'
包围的 '1'
区域,所以没有有效的操作可以进行。最大活跃区间数为 2。
示例 4:
+ +输入: s = "01010", queries = [[0,3],[1,4],[1,3]]
+ +输出: [4,4,2]
+ +解释:
+ +查询 [0, 3]
→ 子字符串 "0101"
→ 变为 "101011"
+ 选择 "010"
,"010"
→ "000"
→ "111"
。
+ 最终字符串(去掉添加的 '1'
)为 "11110"
。最大活跃区间数为 4。
查询 [1, 4]
→ 子字符串 "1010"
→ 变为 "110101"
+ 选择 "010"
,"010"
→ "000"
→ "111"
。
+ 最终字符串(去掉添加的 '1'
)为 "01111"
。最大活跃区间数为 4。
查询 [1, 3]
→ 子字符串 "101"
→ 变为 "11011"
+ 因为没有被 '0'
包围的 '1'
区域,所以没有有效的操作可以进行。最大活跃区间数为 2。
+ +
提示:
+ +1 <= n == s.length <= 105
1 <= queries.length <= 105
s[i]
只有 '0'
或 '1'
。queries[i] = [li, ri]
0 <= li <= ri < n
You are given a binary string s
of length n
, where:
'1'
represents an active section.'0'
represents an inactive section.You can perform at most one trade to maximize the number of active sections in s
. In a trade, you:
'1'
s that is surrounded by '0'
s to all '0'
s.'0'
s that is surrounded by '1'
s to all '1'
s.Additionally, you are given a 2D array queries
, where queries[i] = [li, ri]
represents a substring s[li...ri]
.
For each query, determine the maximum possible number of active sections in s
after making the optimal trade on the substring s[li...ri]
.
Return an array answer
, where answer[i]
is the result for queries[i]
.
A substring is a contiguous non-empty sequence of characters within a string.
+ +Note
+ +s[li...ri]
as if it is augmented with a '1'
at both ends, forming t = '1' + s[li...ri] + '1'
. The augmented '1'
s do not contribute to the final count.+
Example 1:
+ +Input: s = "01", queries = [[0,1]]
+ +Output: [1]
+ +Explanation:
+ +Because there is no block of '1'
s surrounded by '0'
s, no valid trade is possible. The maximum number of active sections is 1.
Example 2:
+ +Input: s = "0100", queries = [[0,3],[0,2],[1,3],[2,3]]
+ +Output: [4,3,1,1]
+ +Explanation:
+ +Query [0, 3]
→ Substring "0100"
→ Augmented to "101001"
+ Choose "0100"
, convert "0100"
→ "0000"
→ "1111"
.
+ The final string without augmentation is "1111"
. The maximum number of active sections is 4.
Query [0, 2]
→ Substring "010"
→ Augmented to "10101"
+ Choose "010"
, convert "010"
→ "000"
→ "111"
.
+ The final string without augmentation is "1110"
. The maximum number of active sections is 3.
Query [1, 3]
→ Substring "100"
→ Augmented to "11001"
+ Because there is no block of '1'
s surrounded by '0'
s, no valid trade is possible. The maximum number of active sections is 1.
Query [2, 3]
→ Substring "00"
→ Augmented to "1001"
+ Because there is no block of '1'
s surrounded by '0'
s, no valid trade is possible. The maximum number of active sections is 1.
Example 3:
+ +Input: s = "1000100", queries = [[1,5],[0,6],[0,4]]
+ +Output: [6,7,2]
+ +Explanation:
+ +Query [1, 5]
→ Substring "00010"
→ Augmented to "1000101"
+ Choose "00010"
, convert "00010"
→ "00000"
→ "11111"
.
+ The final string without augmentation is "1111110"
. The maximum number of active sections is 6.
Query [0, 6]
→ Substring "1000100"
→ Augmented to "110001001"
+ Choose "000100"
, convert "000100"
→ "000000"
→ "111111"
.
+ The final string without augmentation is "1111111"
. The maximum number of active sections is 7.
Query [0, 4]
→ Substring "10001"
→ Augmented to "1100011"
+ Because there is no block of '1'
s surrounded by '0'
s, no valid trade is possible. The maximum number of active sections is 2.
Example 4:
+ +Input: s = "01010", queries = [[0,3],[1,4],[1,3]]
+ +Output: [4,4,2]
+ +Explanation:
+ +Query [0, 3]
→ Substring "0101"
→ Augmented to "101011"
+ Choose "010"
, convert "010"
→ "000"
→ "111"
.
+ The final string without augmentation is "11110"
. The maximum number of active sections is 4.
Query [1, 4]
→ Substring "1010"
→ Augmented to "110101"
+ Choose "010"
, convert "010"
→ "000"
→ "111"
.
+ The final string without augmentation is "01111"
. The maximum number of active sections is 4.
Query [1, 3]
→ Substring "101"
→ Augmented to "11011"
+ Because there is no block of '1'
s surrounded by '0'
s, no valid trade is possible. The maximum number of active sections is 2.
+
Constraints:
+ +1 <= n == s.length <= 105
1 <= queries.length <= 105
s[i]
is either '0'
or '1'
.queries[i] = [li, ri]
0 <= li <= ri < n
给你一个长度为 n
的整数数组 cost
。当前你位于位置 n
(队伍的末尾),队伍中共有 n + 1
人,编号从 0 到 n
。
你希望在队伍中向前移动,但队伍中每个人都会收取一定的费用才能与你 交换位置。与编号 i
的人交换位置的费用为 cost[i]
。
你可以按照以下规则与他人交换位置:
+ +cost[i]
费用与他们交换位置。返回一个大小为 n
的数组 answer
,其中 answer[i]
表示到达队伍中每个位置 i
所需的 最小 总费用。
+ +
示例 1:
+ +输入: cost = [5,3,4,1,3,2]
+ +输出: [5,3,3,1,1,1]
+ +解释:
+ +我们可以通过以下方式到达每个位置:
+ +i = 0
。可以花费 5 费用与编号 0 的人交换位置。i = 1
。可以花费 3 费用与编号 1 的人交换位置。i = 2
。可以花费 3 费用与编号 1 的人交换位置,然后免费与编号 2 的人交换位置。i = 3
。可以花费 1 费用与编号 3 的人交换位置。i = 4
。可以花费 1 费用与编号 3 的人交换位置,然后免费与编号 4 的人交换位置。i = 5
。可以花费 1 费用与编号 3 的人交换位置,然后免费与编号 5 的人交换位置。示例 2:
+ +输入: cost = [1,2,4,6,7]
+ +输出: [1,1,1,1,1]
+ +解释:
+ +可以花费 1 费用与编号 0 的人交换位置,然后可以免费到达队伍中的任何位置 i
。
+ +
提示
+ +1 <= n == cost.length <= 100
1 <= cost[i] <= 100
You are given an integer array cost
of size n
. You are currently at position n
(at the end of the line) in a line of n + 1
people (numbered from 0 to n
).
You wish to move forward in the line, but each person in front of you charges a specific amount to swap places. The cost to swap with person i
is given by cost[i]
.
You are allowed to swap places with people as follows:
+ +cost[i]
to swap with them.Return an array answer
of size n
, where answer[i]
is the minimum total cost to reach each position i
in the line.
+
Example 1:
+ +Input: cost = [5,3,4,1,3,2]
+ +Output: [5,3,3,1,1,1]
+ +Explanation:
+ +We can get to each position in the following way:
+ +i = 0
. We can swap with person 0 for a cost of 5.i = 1
. We can swap with person 1 for a cost of 3.i = 2
. We can swap with person 1 for a cost of 3, then swap with person 2 for free.i = 3
. We can swap with person 3 for a cost of 1.i = 4
. We can swap with person 3 for a cost of 1, then swap with person 4 for free.i = 5
. We can swap with person 3 for a cost of 1, then swap with person 5 for free.Example 2:
+ +Input: cost = [1,2,4,6,7]
+ +Output: [1,1,1,1,1]
+ +Explanation:
+ +We can swap with person 0 for a cost of 1, then we will be able to reach any position i
for free.
+
Constraints:
+ +1 <= n == cost.length <= 100
1 <= cost[i] <= 100
给你两个字符串 s
和 t
。
你可以从 s
中选择一个子串(可以为空)以及从 t
中选择一个子串(可以为空),然后将它们 按顺序 连接,得到一个新的字符串。
返回可以由上述方法构造出的 最长 回文串的长度。
+ +回文串 是指正着读和反着读都相同的字符串。
+ +子字符串 是指字符串中的一个连续字符序列。
+ ++ +
示例 1:
+ +输入: s = "a", t = "a"
+ +输出: 2
+ +解释:
+ +从 s
中选择 "a"
,从 t
中选择 "a"
,拼接得到 "aa"
,这是一个长度为 2 的回文串。
示例 2:
+ +输入: s = "abc", t = "def"
+ +输出: 1
+ +解释:
+ +由于两个字符串的所有字符都不同,最长的回文串只能是任意一个单独的字符,因此答案是 1。
+示例 3:
+ +输入: s = "b", t = "aaaa"
+ +输出: 4
+ +解释:
+ +可以选择 "aaaa"
作为回文串,其长度为 4。
示例 4:
+ +输入: s = "abcde", t = "ecdba"
+ +输出: 5
+ +解释:
+ +从 s
中选择 "abc"
,从 t
中选择 "ba"
,拼接得到 "abcba"
,这是一个长度为 5 的回文串。
+ +
提示:
+ +1 <= s.length, t.length <= 30
s
和 t
仅由小写英文字母组成。You are given two strings, s
and t
.
You can create a new string by selecting a substring from s
(possibly empty) and a substring from t
(possibly empty), then concatenating them in order.
Return the length of the longest palindrome that can be formed this way.
+ +A substring is a contiguous sequence of characters within a string.
+ +A palindrome is a string that reads the same forward and backward.
+ ++
Example 1:
+ +Input: s = "a", t = "a"
+ +Output: 2
+ +Explanation:
+ +Concatenating "a"
from s
and "a"
from t
results in "aa"
, which is a palindrome of length 2.
Example 2:
+ +Input: s = "abc", t = "def"
+ +Output: 1
+ +Explanation:
+ +Since all characters are different, the longest palindrome is any single character, so the answer is 1.
+Example 3:
+ +Input: s = "b", t = "aaaa"
+ +Output: 4
+ +Explanation:
+ +Selecting "aaaa
" from t
is the longest palindrome, so the answer is 4.
Example 4:
+ +Input: s = "abcde", t = "ecdba"
+ +Output: 5
+ +Explanation:
+ +Concatenating "abc"
from s
and "ba"
from t
results in "abcba"
, which is a palindrome of length 5.
+
Constraints:
+ +1 <= s.length, t.length <= 30
s
and t
consist of lowercase English letters.给你两个字符串 s
和 t
。
你可以从 s
中选择一个子串(可以为空)以及从 t
中选择一个子串(可以为空),然后将它们 按顺序 连接,得到一个新的字符串。
返回可以由上述方法构造出的 最长 回文串的长度。
+ +回文串 是指正着读和反着读都相同的字符串。
+ +子字符串 是指字符串中的一个连续字符序列。
+ ++ +
示例 1:
+ +输入: s = "a", t = "a"
+ +输出: 2
+ +解释:
+ +从 s
中选择 "a"
,从 t
中选择 "a"
,拼接得到 "aa"
,这是一个长度为 2 的回文串。
示例 2:
+ +输入: s = "abc", t = "def"
+ +输出: 1
+ +解释:
+ +由于两个字符串的所有字符都不同,最长的回文串只能是任意一个单独的字符,因此答案是 1。
+示例 3:
+ +输入: s = "b", t = "aaaa"
+ +输出: 4
+ +解释:
+ +可以选择 "aaaa"
作为回文串,其长度为 4。
示例 4:
+ +输入: s = "abcde", t = "ecdba"
+ +输出: 5
+ +解释:
+ +从 s
中选择 "abc"
,从 t
中选择 "ba"
,拼接得到 "abcba"
,这是一个长度为 5 的回文串。
+ +
提示:
+ +1 <= s.length, t.length <= 1000
s
和 t
仅由小写英文字母组成。You are given two strings, s
and t
.
You can create a new string by selecting a substring from s
(possibly empty) and a substring from t
(possibly empty), then concatenating them in order.
Return the length of the longest palindrome that can be formed this way.
+ +A substring is a contiguous sequence of characters within a string.
+ +A palindrome is a string that reads the same forward and backward.
+ ++
Example 1:
+ +Input: s = "a", t = "a"
+ +Output: 2
+ +Explanation:
+ +Concatenating "a"
from s
and "a"
from t
results in "aa"
, which is a palindrome of length 2.
Example 2:
+ +Input: s = "abc", t = "def"
+ +Output: 1
+ +Explanation:
+ +Since all characters are different, the longest palindrome is any single character, so the answer is 1.
+Example 3:
+ +Input: s = "b", t = "aaaa"
+ +Output: 4
+ +Explanation:
+ +Selecting "aaaa
" from t
is the longest palindrome, so the answer is 4.
Example 4:
+ +Input: s = "abcde", t = "ecdba"
+ +Output: 5
+ +Explanation:
+ +Concatenating "abc"
from s
and "ba"
from t
results in "abcba"
, which is a palindrome of length 5.
+
Constraints:
+ +1 <= s.length, t.length <= 1000
s
and t
consist of lowercase English letters.给你一个整数数组 nums
和两个整数 x
和 k
。你可以执行以下操作任意次(包括零次):
nums
中的任意一个元素加 1 或减 1。返回为了使 nums
中 至少 包含 k 个长度 恰好 为 x
的不重叠子数组(每个子数组中的所有元素都相等)所需要的 最少 操作数。
子数组 是数组中连续、非空的一段元素。
+ ++ +
示例 1:
+ +输入: nums = [5,-2,1,3,7,3,6,4,-1], x = 3, k = 2
+ +输出: 8
+ +解释:
+ +nums[1]
加 3;进行 2 次操作,将 nums[3]
减 2。得到的数组为 [5, 1, 1, 1, 7, 3, 6, 4, -1]
。nums[5]
加 1;进行 2 次操作,将 nums[6]
减 2。得到的数组为 [5, 1, 1, 1, 7, 4, 4, 4, -1]
。[1, 1, 1]
(下标 1 到 3)和 [4, 4, 4]
(下标 5 到 7)中的所有元素都相等。总共进行了 8 次操作,因此输出为 8。示例 2:
+ +输入: nums = [9,-2,-2,-2,1,5], x = 2, k = 2
+ +输出: 3
+ +解释:
+ +nums[4]
减 3。得到的数组为 [9, -2, -2, -2, -2, 5]
。[-2, -2]
(下标 1 到 2)和 [-2, -2]
(下标 3 到 4)中的所有元素都相等。总共进行了 3 次操作,因此输出为 3。+ +
提示:
+ +2 <= nums.length <= 105
-106 <= nums[i] <= 106
2 <= x <= nums.length
1 <= k <= 15
2 <= k * x <= nums.length
You are given an integer array nums
and two integers, x
and k
. You can perform the following operation any number of times (including zero):
nums
by 1.Return the minimum number of operations needed to have at least k
non-overlapping subarrays of size exactly x
in nums
, where all elements within each subarray are equal.
+
Example 1:
+ +Input: nums = [5,-2,1,3,7,3,6,4,-1], x = 3, k = 2
+ +Output: 8
+ +Explanation:
+ +nums[1]
and use 2 operations to subtract 2 from nums[3]
. The resulting array is [5, 1, 1, 1, 7, 3, 6, 4, -1]
.nums[5]
and use 2 operations to subtract 2 from nums[6]
. The resulting array is [5, 1, 1, 1, 7, 4, 4, 4, -1]
.[1, 1, 1]
(from indices 1 to 3) and [4, 4, 4]
(from indices 5 to 7) are equal. Since 8 total operations were used, 8 is the output.Example 2:
+ +Input: nums = [9,-2,-2,-2,1,5], x = 2, k = 2
+ +Output: 3
+ +Explanation:
+ +nums[4]
. The resulting array is [9, -2, -2, -2, -2, 5]
.[-2, -2]
(from indices 1 to 2) and [-2, -2]
(from indices 3 to 4) are equal. Since 3 operations were used, 3 is the output.+
Constraints:
+ +2 <= nums.length <= 105
-106 <= nums[i] <= 106
2 <= x <= nums.length
1 <= k <= 15
2 <= k * x <= nums.length
You are given two integer arrays, nums
and cost
, of the same size, and an integer k
.
You can divide nums
into subarrays. The cost of the ith
subarray consisting of elements nums[l..r]
is:
You can divide nums
into subarrays. The cost of the ith
subarray consisting of elements nums[l..r]
is:
(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])
.Return the minimum total cost possible from any valid division.
-A subarray is a contiguous non-empty sequence of elements within an array.
-
Example 1:
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 527245c83a435..f9820eafe32ea 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 @@ -20,7 +20,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3501.Ma'1'
represents an active section.'0'
represents an inactive section.You can perform at most one trade to maximize the number of active sections in s
. In a trade, you:
'0'
s that is surrounded by '1'
s to all '1'
s.Additionally, you are given a 2D array queries
, where queries[i] = [li, ri]
represents a substring s[li...ri]
.
Additionally, you are given a 2D array queries
, where queries[i] = [li, ri]
represents a substring s[li...ri]
.
For each query, determine the maximum possible number of active sections in s
after making the optimal trade on the substring s[li...ri]
.
Return an array answer
, where answer[i]
is the result for queries[i]
.
A substring is a contiguous non-empty sequence of characters within a string.
-Note
You are given two strings, s
and t
.
You can create a new string by selecting a substring from s
(possibly empty) and a substring from t
(possibly empty), then concatenating them in order.
You can create a new string by selecting a substring from s
(possibly empty) and a substring from t
(possibly empty), then concatenating them in order.
Return the length of the longest palindrome that can be formed this way.
- -A substring is a contiguous sequence of characters within a string.
- -A palindrome is a string that reads the same forward and backward.
+Return the length of the longest palindrome that can be formed this way.
Example 1:
@@ -87,7 +83,26 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3503.Lo -### Solution 1 +### Solution 1: Enumerate Palindrome Centers + Dynamic Programming + +According to the problem description, the concatenated palindrome string can be composed entirely of string $s$, entirely of string $t$, or a combination of both strings $s$ and $t$. Additionally, there may be extra palindromic substrings in either string $s$ or $t$. + +Therefore, we first reverse string $t$ and preprocess arrays $\textit{g1}$ and $\textit{g2}$, where $\textit{g1}[i]$ represents the length of the longest palindromic substring starting at index $i$ in string $s$, and $\textit{g2}[i]$ represents the length of the longest palindromic substring starting at index $i$ in string $t$. + +We can initialize the answer $\textit{ans}$ as the maximum value in $\textit{g1}$ and $\textit{g2}$. + +Next, we define $\textit{f}[i][j]$ as the length of the palindromic substring ending at the $i$-th character of string $s$ and the $j$-th character of string $t$. + +For $\textit{f}[i][j]$, if $s[i - 1]$ equals $t[j - 1]$, then $\textit{f}[i][j] = \textit{f}[i - 1][j - 1] + 1$. We then update the answer: + +$$ +\textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } i \geq m \text{ else } \textit{g1}[i])) \\ +\textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } j \geq n \text{ else } \textit{g2}[j])) +$$ + +Finally, we return the answer $\textit{ans}$. + +The time complexity is $O(m \times (m + n))$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the lengths of strings $s$ and $t$, respectively. 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 17ee66466df32..269de847bbde6 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 @@ -90,7 +90,27 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3504.Lo -### 方法一 +### 方法一:枚举回文中点 + 动态规划 + +根据题目描述,连接后的回文串,可以只由字符串 $s$ 组成,也可以只由字符串 $t$ 组成,也可以由字符串 $s$ 和字符串 $t$ 组成,并且还可能在字符串 $s$ 或 $t$ 中多出一部分回文子串。 + +因此,我们先将字符串 $t$ 反转,然后预处理出数组 $\textit{g1}$ 和 $\textit{g2}$,其中 $\textit{g1}[i]$ 表示在字符串 $s$ 中以下标 $i$ 开始的最长回文子串长度,而 $\textit{g2}[i]$ 表示在字符串 $t$ 中以下标 $i$ 开始的最长回文子串长度。 + +那么我们可以初始化答案 $\textit{ans}$ 为 $\textit{g1}$ 和 $\textit{g2}$ 中的最大值。 + +接下来,我们定义 $\textit{f}[i][j]$ 表示以字符串 $s$ 的第 $i$ 个字符结尾,以字符串 $t$ 的第 $j$ 个字符结尾的回文子串的长度。 + +对于 $\textit{f}[i][j]$,如果 $s[i - 1]$ 等于 $t[j - 1]$,那么有 $\textit{f}[i][j] = \textit{f}[i - 1][j - 1] + 1$。然后,我们更新答案: + +$$ +\textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } i \geq m \text{ else } \textit{g1}[i])) \\ + +\textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } j \geq n \text{ else } \textit{g2}[j])) \ +$$ + +最后,我们返回答案 $\textit{ans}$ 即可。 + +时间复杂度 $O(m \times (m + n))$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是字符串 $s$ 和 $t$ 的长度。 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 3a2e00ef7e1cc..95221be84beba 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 @@ -15,15 +15,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3504.LoYou are given two strings, s
and t
.
You can create a new string by selecting a substring from s
(possibly empty) and a substring from t
(possibly empty), then concatenating them in order.
You can create a new string by selecting a substring from s
(possibly empty) and a substring from t
(possibly empty), then concatenating them in order.
Return the length of the longest palindrome that can be formed this way.
- -A substring is a contiguous sequence of characters within a string.
- -A palindrome is a string that reads the same forward and backward.
+Return the length of the longest palindrome that can be formed this way.
Example 1:
@@ -88,7 +83,26 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3504.Lo -### Solution 1 +### Solution 1: Enumerate Palindrome Centers + Dynamic Programming + +According to the problem description, the concatenated palindrome string can be composed entirely of string $s$, entirely of string $t$, or a combination of both strings $s$ and $t$. Additionally, there may be extra palindromic substrings in either string $s$ or $t$. + +Therefore, we first reverse string $t$ and preprocess arrays $\textit{g1}$ and $\textit{g2}$, where $\textit{g1}[i]$ represents the length of the longest palindromic substring starting at index $i$ in string $s$, and $\textit{g2}[i]$ represents the length of the longest palindromic substring starting at index $i$ in string $t$. + +We can initialize the answer $\textit{ans}$ as the maximum value in $\textit{g1}$ and $\textit{g2}$. + +Next, we define $\textit{f}[i][j]$ as the length of the palindromic substring ending at the $i$-th character of string $s$ and the $j$-th character of string $t$. + +For $\textit{f}[i][j]$, if $s[i - 1]$ equals $t[j - 1]$, then $\textit{f}[i][j] = \textit{f}[i - 1][j - 1] + 1$. We then update the answer: + +$$ +\textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } i \geq m \text{ else } \textit{g1}[i])) \\ +\textit{ans} = \max(\textit{ans}, \textit{f}[i][j] \times 2 + (0 \text{ if } j \geq n \text{ else } \textit{g2}[j])) +$$ + +Finally, we return the answer $\textit{ans}$. + +The time complexity is $O(m \times (m + n))$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the lengths of strings $s$ and $t$, respectively. 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 4ad8f5bb9a43a..c6bf631bd3eec 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 @@ -15,14 +15,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3505.MiYou are given an integer array nums
and two integers, x
and k
. You can perform the following operation any number of times (including zero):
nums
by 1.Return the minimum number of operations needed to have at least k
non-overlapping subarrays of size exactly x
in nums
, where all elements within each subarray are equal.
Return the minimum number of operations needed to have at least k
non-overlapping subarrays of size exactly x
in nums
, where all elements within each subarray are equal.
Example 1:
From 59e6d158fa05bb517a0ed74d335e24d017d5efc5 Mon Sep 17 00:00:00 2001 From: Libin YANGYou 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:
+ +ith
bacterial strain takes timeReq[i]
units of time to be eliminated.splitTime
units of time. Once split, the two WBCs can work in parallel on eliminating 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:
+ +t = 2 + 10 = 12.
The other WBC splits again, using 2 units of time.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:
+ +t = 5 + 10
and t = 5 + 4
.+
Constraints:
+ +2 <= timeReq.length <= 105
1 <= timeReq[i] <= 109
1 <= splitTime <= 109
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:
+ +ith
bacterial strain takes timeReq[i]
units of time to be eliminated.splitTime
units of time. Once split, the two WBCs can work in parallel on eliminating 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:
+ +t = 2 + 10 = 12.
The other WBC splits again, using 2 units of time.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:
+ +t = 5 + 10
and t = 5 + 4
.+
Constraints:
+ +2 <= timeReq.length <= 105
1 <= timeReq[i] <= 109
1 <= splitTime <= 109