![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LC滑动窗口
Slider Window
你别教我打游戏
直面困难,重视过程,追求结果,淡忘过去。
展开
-
LeetCode 1838. 最高频元素的频数 (排序 + 二分 | 滑窗)
1838. 最高频元素的频数排序+二分class Solution {public: int maxFrequency(vector<int>& nums, int k) { sort(nums.begin(),nums.end()); int ans = 1; int n = nums.size(); vector<long long> s(n); s[0] = nums[0]原创 2021-07-19 18:31:14 · 83 阅读 · 0 评论 -
LeetCode 395. 至少有K个重复字符的最长子串( 前缀和+分治 、枚举+滑动窗口)
至少有K个重复字符的最长子串class Solution {public: vector<int> pre[26]; int len, k; string s; int longestSubstring(string s, int k) { len = s.size(); this->k = k; this->s = s; for(int i=0;i<26;i++) pre[i原创 2021-02-27 03:08:44 · 168 阅读 · 0 评论 -
LeetCode 1004. 最大连续1的个数 III (滑动窗口经典入门题)
class Solution {public: int longestOnes(vector<int>& a, int k) { int ans = 0 , n = a.size(); int l = 0, r = 0; while(l<n){ while(r<n && (a[r] || k)){ if(a[r]==0){原创 2021-02-19 00:27:04 · 61 阅读 · 0 评论 -
LeetCode 567. 字符串的排列 (巧妙的滑动窗口)
567. 字符串的排列自己的麻瓜做法。。。class Solution { struct Freq{ int cnt[26] = {0}; bool ok(){ for(int i=0;i<26;i++){ if(cnt[i]!=0) return false; } return true; } void add(char原创 2021-02-10 17:57:20 · 88 阅读 · 0 评论 -
LeetCode 992. K 个不同整数的子数组 (思维、计数、双指针、哈希表)
992. K 个不同整数的子数组class Solution { public int subarraysWithKDistinct(int[] A, int K) { return helper(A,K)-helper(A,K-1); } int helper(int[] a,int k){ int n = a.length, l = 0, r = 0; int[] cnt = new int[n+1]; int原创 2021-02-10 17:44:26 · 125 阅读 · 0 评论 -
LeetCode 340. 至多包含 K 个不同字符的最长子串 (滑动窗口)
340. 至多包含 K 个不同字符的最长子串class Solution { public int lengthOfLongestSubstringKDistinct(String ss, int k) { char[] s = ss.toCharArray(); Map<Character,Integer> map = new HashMap<>(); int n = s.length, l = 0, r = 0;原创 2021-02-09 11:56:05 · 152 阅读 · 1 评论 -
LeetCode 424. 替换后的最长重复字符 (滑动窗口)
424. 替换后的最长重复字符做这道题最好先尝试做一道题LeetCode 1208. 尽可能使字符串相等。代码中的maxn指的是:比如有mmm个时刻,每个时刻窗口的为[lt1,rt1][l_{t_1},r_{t_1}][lt1,rt1]……[ltm,rtm][l_{t_m},r_{t_m}][ltm,rtm]记每个窗口中的出现次数最多的字母分别为:ct1,ct2,……,ctmc_{t1},c_{t_2},……,c_{t_m}ct1,ct2,……,ctm出现次数依次原创 2021-02-03 02:42:51 · 88 阅读 · 0 评论 -
LeetCode 1438. 绝对差不超过限制的最长连续子数组 (multiset维护滑动窗口、单调队列)
绝对差不超过限制的最长连续子数组class Solution {public: int longestSubarray(vector<int>& nums, int limit) { int ans = 0 , n = nums.size(); int l = 0 ,r = 0; multiset<int> vis; vis.insert(nums[0]); while(l<n)原创 2020-08-08 11:47:38 · 150 阅读 · 0 评论 -
LeetCode 632. 最小区间 (哈希表+滑动窗口)
最小区间如果用滑动窗口,要先会做这道题,最小覆盖子串这种求满足条件的最长区间或者最短区间问题,很多时候都可以用滑动窗口解决。class Solution {public: map<int,vector<int>> mp; vector<int> smallestRange(vector<vector<int>>& nums) { vector<int> ans = {-100000,100原创 2020-08-01 11:05:31 · 115 阅读 · 0 评论 -
LeetCode 209. 长度最小的子数组 (滑动窗口 || 前缀和+二分查找)
长度最小的子数组class Solution {public: int minSubArrayLen(int s, vector<int>& a) { int ans = 1e9,n = a.size(); int l = 0 ,r = 0 ,cnt = 0; while(l<n){ while(r<n && cnt<s){ cnt +=原创 2020-07-29 10:09:21 · 140 阅读 · 0 评论 -
LeetCode 76. 最小覆盖子串(哈希表+滑动窗口)
最小覆盖子串子串类的题目很多都是滑动窗口,此题关键是如何维护这个滑动窗口,用map<char,int>维护我们关心的字符出现的次数,用cnt变量维护还有几个字符没有被消掉。class Solution {public: string minWindow(string s, string t) { int res = s.size()+1,n = s.size(); string ans = ""; int l = 0 ,r = 0原创 2020-07-16 03:13:05 · 102 阅读 · 0 评论 -
LeetCode 1208. 尽可能使字符串相等 (滑动窗口模板题)
尽可能使字符串相等class Solution {public: int equalSubstring(string s, string t, int maxCost) { int ans = 0 , n = s.size() , cnt = 0; int l = 0, r = 0; while(l<n){ while(r<n && cnt+abs(s[r]-t[r])<=maxCost){原创 2020-07-14 22:41:20 · 118 阅读 · 0 评论 -
LeetCode 1052. 爱生气的书店老板 (最简单的滑动窗口)
爱生气的书店老板当窗口移动时,只需关心移进窗口的和移出窗口的两个值即可。class Solution {public: int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) { int ans = 0,n = customers.size(),cnt = 0,res = 0; for(int i=0;i<n;i++){原创 2020-07-12 01:29:53 · 138 阅读 · 0 评论 -
LeetCode 239. 滑动窗口最大值 (堆、单调队列)
滑动窗口最大值标题即题意,滑动窗口经典例题。暴力法时间复杂度: O((n−k)∗k)O((n-k)*k)O((n−k)∗k)主要耗时的是要在窗口的k个元素里通过遍历挑出最大值。用堆去优化,multiset<int>实现,时间复杂度:O((n−k)∗log(k))O((n-k)*log(k))O((n−k)∗log(k))class Solution {public: vector<int> maxSlidingWindow(vector<int>&原创 2020-07-05 18:52:34 · 149 阅读 · 0 评论 -
LeetCode 3. 无重复字符的最长子串 (从暴力到滑动窗口——滑动窗口经典入门题)
无重复字符的最长子串 滑动窗口做法就不说了。主要讲一讲为什么可以用这种做法,或者说暴力方法的优化点在哪。考虑一种最暴力的方法:对一个字符串的每个子串(共有(n+1)∗n/2(n+1)*n/2(n+1)∗n/2个),调用一个检查其合法性的函数(判断是否有重复字符),更新子串的最大值。时间复杂度:O(n3)O(n^3)O(n3)class Solution {public: int lengthOfLongestSubstring(string s) { int ans = 0原创 2020-07-04 19:46:50 · 134 阅读 · 0 评论