Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
int isPalin[2001][2001];
public:
int maxPalindromes(string s, int k)
{
int n = s.size();
for (int i=0; i<n; i++)
isPalin[i][i] = 1;
for (int i=0; i+1<n; i++)
isPalin[i][i+1] = (s[i]==s[i+1]);

for (int len=3; len<=n; len++)
for (int i=0; i+len-1<n; i++)
{
int j = i+len-1;
if (s[i]==s[j])
isPalin[i][j] = isPalin[i+1][j-1];
}

vector<int>dp(n);
for (int i=k-1; i<n; i++)
{
dp[i] = i==0?0:dp[i-1];
for (int j=0; j<=i-k+1; j++)
{
if (isPalin[j][i])
dp[i] = max(dp[i], (j==0?0:dp[j-1])+1);
}
}
return dp[n-1];
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### 2472.Maximum-Number-of-Non-overlapping-Palindrome-Substrings

令dp[i]表示s[0:i]里面的the maximum number of palindrome substrings. 显然,转移方程的突破口在于最后一个回文子串。如果最后一个回文子串不包括s[i],那么有`dp[i] = dp[i-1]`,如果最后一个回文子串包括s[i],那么我们就需要找这个回文子串的起始位置j,然后`dp[i] = dp[j-1] + 1`. 这样的j可能有多个,根据数据量,从[0:i]遍历一遍都是可行的。

另外,我们需要用o(N^2)的时间提前处理得到一个数组isPalin[i][j],来记录s[i:j]是否是一个回文串。这个技巧已经出现过很多次了。
2 changes: 2 additions & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
[1798.Maximum-Number-of-Consecutive-Values-You-Can-Make](https://github.com/wisdompeak/LeetCode/blob/master/Greedy/1798.Maximum-Number-of-Consecutive-Values-You-Can-Make) (H-)
[1989.Maximum-Number-of-People-That-Can-Be-Caught-in-Tag](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1989.Maximum-Number-of-People-That-Can-Be-Caught-in-Tag) (M+)
[2354.Number-of-Excellent-Pairs](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/2354.Number-of-Excellent-Pairs) (H-)
[2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome) (H-)
* ``Sliding window``
[532.K-diff-Pairs-in-an-Array](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/532.K-diff-Pairs-in-an-Array) (H-)
[611.Valid-Triangle-Number](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/611.Valid-Triangle-Number) (M+)
Expand Down Expand Up @@ -743,6 +744,7 @@
[1478.Allocate-Mailboxes](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1478.Allocate-Mailboxes) (H)
[1977.Number-of-Ways-to-Separate-Numbers](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1977.Number-of-Ways-to-Separate-Numbers) (H)
[2463.Minimum-Total-Distance-Traveled](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2463.Minimum-Total-Distance-Traveled) (M+)
[2472.Maximum-Number-of-Non-overlapping-Palindrome-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2472.Maximum-Number-of-Non-overlapping-Palindrome-Substrings) (M+)
* ``区间型 II``
[131.Palindrome-Partitioning](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/131.Palindrome-Partitioning) (M+)
[312.Burst-Balloons](https://github.com/wisdompeak/LeetCode/tree/master/DFS/312.Burst-Balloons) (H-)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using LL = long long;
class Solution {
public:
int minimumOperations(vector<int>& nums)
{
int i = 0, j = nums.size()-1;
LL left = nums[i], right = nums[j];
int count = 0;

while (i<j)
{
if (left==right)
{
i++;
j--;
left = nums[i];
right = nums[j];
}
else if (left < right)
{
i++;
left += nums[i];
count++;
}
else if (left > right)
{
j--;
right += nums[j];
count++;
}
}
return count;

}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### 2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome

我们可以设想,最终得到的回文串的最左边和最右边的元素是怎么得到的?一定是来自nums最左侧的若干个元素之和,与nums最右侧的若干个元素之和。考虑到所有的元素都是正数,我们可以用双指针的方法来得到two equal sum。也就是说,初始令`left=nums[0]`和`right=nums[n-1]`,如果发现`left<right`,必然只能将左指针右移才能试图让left与right相等;同理,如果发现`left>right`,必然只能将右指针左移才能试图让left与right相等。于是在得到`left==right`之前,两侧指针移动的总次数就是merge的次数。此时,说明我们找到了最终回文串的最外层的一对。剥离掉这最外层后,我们可以重复上述的过程。

此外,我们必须考虑到,有可能在左右指针相遇之前,都无法满足`left==right`。这意味着什么呢?说明只有一个方案,即将整个数组都归并在一起,成为回文串的中心。

总结:所以本题就是一个双指针,不停地调整左右指针,试图使得前缀和等于后缀和。如此一轮一轮地确定外层的每一对。如果最终指针相遇,意味着该轮的所有元素必须都归并在一起。

AltStyle によって変換されたページ (->オリジナル) /