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 75a4e988ff8c9..ab403c073ad5a 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 @@ -15,9 +15,8 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3500.Mi
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: