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

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 5 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Aug 6, 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
1 change: 1 addition & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
[2537.Count-the-Number-of-Good-Subarrays](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/2537.Count-the-Number-of-Good-Subarrays) (M+)
[3298.Count-Substrings-That-Can-Be-Rearranged-to-Contain-a-String-II](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/3298.Count-Substrings-That-Can-Be-Rearranged-to-Contain-a-String-II) (M+)
[3306.Count-of-Substrings-Containing-Every-Vowel-and-K-Consonants-II](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/3306.Count-of-Substrings-Containing-Every-Vowel-and-K-Consonants-II) (H-)
[3641.Longest-Semi-Repeating-Subarray](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/3641.Longest-Semi-Repeating-Subarray) (H-)
* ``Two pointers for two sequences``
[986.Interval-List-Intersections](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/986.Interval-List-Intersections) (M)
[1229.Meeting-Scheduler](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1229.Meeting-Scheduler) (M+)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int longestSubarray(vector<int>& nums, int k) {
unordered_map<int,int>Map;
int count = 0;
int n = nums.size();
int j = -1;
int ret = 0;
for (int i=0; i<n; i++) {
while (j+1<n && count<=k) {
Map[nums[++j]]++;
if (Map[nums[j]]==2)
count++;
}
if (count>k)
ret = max(ret, j - i);
else
ret = max(ret, j - i + 1);
Map[nums[i]]--;
if (Map[nums[i]]==1) count--;
}
return ret;

}
};
7 changes: 7 additions & 0 deletions Two_Pointers/3641.Longest-Semi-Repeating-Subarray/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### 3641.Longest-Semi-Repeating-Subarray

非常明显的双指针,但是滑窗右边界j的控制需要格外小心。

对于任意的左边界i,当满足`while (j+1<n && count<=k)`时(count变量用于统计repeat元素的个数),说明可以将nums[++j]加入统计。如果循环退出,注意有两种不同情况:
1. 如果count>k,那么意味着j超出了合法的范围,合法subarray的长度最多是`j-i`.
2. 如果count<=k,那么意味着j已经到了最后一个元素(即n-1)但是仍未超出合法范围,故此时合法subarray的长度是`j-i+1`.

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