📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
查看: 2680| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] Sliding Window 题目的进一步归类

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Sliding Window: Generalized
Code Template
        int i = 0;
        for (int j = 0; j < n; j++) {
             // update state of the window after adding j into the window
             f[s.charAt(j)]++;  
             max = Math.max(max, f[s.charAt(j)]);

            // moving left pointer to make sure the window is always valid
             while (j - i + 1 - max > k) {
                 f[s.charAt(i)]--;
                 i++;
             }
             // update result
             res = Math.max(res, j-i+1);
         }
        return res;

Example
424. Longest Repeating Character Replacement
340 Longest Substring with At Most K Distinct Characters
3. Longest Substring Without Repeating Characters
1004. Max Consecutive Ones III
424. Longest Repeating Character Replacement
532. K-diff Pairs in an Array
923. 3Sum With Multiplicity
209. Minimum Size Subarray Sum
76. Minimum Window Substring
30. Substring with Concatenation of All Words
Number of subarrays with sum less than K
* subarray must contain only non-negative numbers, otherwise sliding window won’t work
1156. Swap For Longest Repeated Character Substring
* a window is defined as by having at most one swap, all chars in the window can be the same
* window become invalid when the window size is more than the number of most frequent char in the window + 1 (where 1 is the allowed swap) or the window size is more than the total freq of the most frequent char in the window
1234. Replace the Substring for Balanced String (slightly different than the traditional sliding window template)


Sliding Window: Fixed length
Pattern
right pointer - left pointer + 1 should be fixed size n
Example
438. Find All Anagrams in a String
567.Permutation In String
1151. Minimum Swaps to Group All 1's Together
220. Contains Duplicate III


Sliding Window: Stack
Pattern
The elements in the window are put into stack, stack can be viewed as the state of current window


Example:
1081. Smallest Subsequence of Distinct Characters








评分

参与人数 3大米 +4 收起 理由
100608430 + 2 很有用的信息!
lemoncorn1123 + 1 赞一个
besfield + 1 很有用的信息!

查看全部评分


上一篇:总结几个BFS 的pattern
下一篇:开始刷题,与同行人共勉。
🔗
maxmonlt 2020-2-9 23:23:23 | 只看该作者
全局:
多谢分享,辛苦了,先收藏了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表