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 #310

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 3 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Feb 18, 2025
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,60 @@
class Solution {
public:
bool maxSubstringLength(string s, int k)
{
int n = s.size();
vector<vector<int>>pos(26);
for (int i=0; i<n; i++)
pos[s[i]-'a'].push_back(i);

vector<pair<int, int>>intervals;
for (int letter=0; letter<26; letter++)
{
if (pos[letter].empty()) continue;
int start = pos[letter][0];
int i = start;
int far = pos[letter].back();

bool flag = true;
while (i<=far)
{
far = max(far, pos[s[i]-'a'].back());
if (pos[s[i]-'a'][0]<start)
{
flag = false;
break;
}
i++;
}
if (far-start+1==n) continue;
if (flag==false) continue;
intervals.push_back({start, far});
}

return helper(intervals)>=k;
}

bool contains(pair<int,int>a, pair<int,int>b)
{
return a.first<b.first && a.second>b.second;
}

int helper(vector<pair<int, int>> &intervals) {
int n = intervals.size();
vector<int>check(n, 1);
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
{
if (i==j) continue;
if (contains(intervals[i],intervals[j]))
check[i] = 0;
if (contains(intervals[j],intervals[i]))
check[j] = 0;
}
int ret = 0;
for (int i=0; i<n; i++) ret += check[i];
return ret;
}


};
5 changes: 4 additions & 1 deletion Greedy/3458.Select-K-Disjoint-Special-Substrings/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@

如果推进的过程持续至指针追上了end,那么意味着[start,end]区间内的所有字母都是自包含的。

我们由此可以得到最多26段自包含的区间。我们需要判断是否至少有k个区间是彼此不相交的。这就等同于`435.Non-overlapping-Intervals`. 一个常见的贪心的做法,就是将所有区间按照尾端点排序。贪心的原则就是:如果有若干个彼此相交的区间,根据规则我们最多只能取一个,那必然只会选择尾端点更靠前的,这样会留给后续更多的空间能选择不相交的区间。所以我们必然选择排序后的第一个区间,然后顺序检查后续的区间,如果与其相交的区间都舍弃;此时下一个区间就是选择的第二个区间:因为它与首区间不相交,且尾端点是剩下所有里面最靠前的。以此类推。
我们需要判断是否至少有k个区间是彼此不相交的。这就等同于`435.Non-overlapping-Intervals`. 一个常见的贪心的做法,就是将所有区间按照尾端点排序。贪心的原则就是:如果有若干个彼此相交的区间,根据规则我们最多只能取一个,那必然只会选择尾端点更靠前的,这样会留给后续更多的空间能选择不相交的区间。所以我们必然选择排序后的第一个区间,然后顺序检查后续的区间,如果与其相交的区间都舍弃;此时下一个区间就是选择的第二个区间:因为它与首区间不相交,且尾端点是剩下所有里面最靠前的。以此类推。

事实上,还有更简单的计算逻辑。我们得到的最多26段自包含的区间,要么互斥,要么互相嵌套,不可能出现部分相交的情况。分析见`1520. Maximum Number of Non-Overlapping Substrings`. 所以我们只需要两两比较,如果大的包含小的,去掉大的即可。

7 changes: 4 additions & 3 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,6 @@
826.Most-Profit-Assigning-Work (M)
[1268.Search-Suggestions-System](https://github.com/wisdompeak/LeetCode/tree/master/Trie/1268.Search-Suggestions-System) (H-)
[1402.Reducing-Dishes](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1402.Reducing-Dishes) (M)
[1520.Maximum-Number-of-Non-Overlapping-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1520.Maximum-Number-of-Non-Overlapping-Substrings) (H-)
[1564.Put-Boxes-Into-the-Warehouse-I](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1564.Put-Boxes-Into-the-Warehouse-I) (M+)
[1665.Minimum-Initial-Energy-to-Finish-Tasks](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1665.Minimum-Initial-Energy-to-Finish-Tasks) (H-)
[1686.Stone-Game-VI](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1686.Stone-Game-VI) (H-)
Expand Down Expand Up @@ -1478,8 +1477,10 @@
[2983.Palindrome-Rearrangement-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2983.Palindrome-Rearrangement-Queries) (H+)
[2781.Length-of-the-Longest-Valid-Substring](https://github.com/wisdompeak/LeetCode/tree/master/String/2781.Length-of-the-Longest-Valid-Substring) (H-)
[3394.Check-if-Grid-can-be-Cut-into-Sections](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3394.Check-if-Grid-can-be-Cut-into-Sections) (M)
[2271.Maximum-White-Tiles-Covered-by-a-Carpet](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2271.Maximum-White-Tiles-Covered-by-a-Carpet) (M+)
[3413.Maximum-Coins-From-K-Consecutive-Bags](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3413.Maximum-Coins-From-K-Consecutive-Bags) (H-)
[2271.Maximum-White-Tiles-Covered-by-a-Carpet](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2271.Maximum-White-Tiles-Covered-by-a-Carpet) (M+)
[3413.Maximum-Coins-From-K-Consecutive-Bags](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3413.Maximum-Coins-From-K-Consecutive-Bags) (H-)
3104.Find Longest Self-Contained Substring (TBD)
[1520.Maximum-Number-of-Non-Overlapping-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1520.Maximum-Number-of-Non-Overlapping-Substrings) (H-)
[3458.Select-K-Disjoint-Special-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3458.Select-K-Disjoint-Special-Substrings) (H-)
* ``Constructive Problems``
[324.Wiggle-Sort-II](https://github.com/wisdompeak/LeetCode/tree/master/Others/324.Wiggle-Sort-II) (H)
Expand Down

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