forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f527c8a
Create 2472.Maximum-Number-of-Non-overlapping-Palindrome-Substrings.cpp
wisdompeak 8d0a0b3
Update Readme.md
wisdompeak 0d397d4
Create Readme.md
wisdompeak 9cc1f8c
Create 2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome.cpp
wisdompeak 15cfb93
Update Readme.md
wisdompeak 39f226e
Create Readme.md
wisdompeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
...ng-Palindrome-Substrings/2472.Maximum-Number-of-Non-overlapping-Palindrome-Substrings.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
...gramming/2472.Maximum-Number-of-Non-overlapping-Palindrome-Substrings/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]是否是一个回文串。这个技巧已经出现过很多次了。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
...to-Turn-Array-Into-a-Palindrome/2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
}; |
7 changes: 7 additions & 0 deletions
Two_Pointers/2422.Merge-Operations-to-Turn-Array-Into-a-Palindrome/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`。这意味着什么呢?说明只有一个方案,即将整个数组都归并在一起,成为回文串的中心。 | ||
|
||
总结:所以本题就是一个双指针,不停地调整左右指针,试图使得前缀和等于后缀和。如此一轮一轮地确定外层的每一对。如果最终指针相遇,意味着该轮的所有元素必须都归并在一起。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.