首页 > Java > java教程 > 正文

两个指针和滑动窗口模式

PHPz
发布: 2024-09-10 12:30:02
转载
325人浏览过

两个指针和滑动窗口模式

双指针和滑动窗口模式

模式 1:常量窗口(如 window = 4 或某个整数值)

例如,给定一个 (-ve 和 +ve) 整数数组,找到大小为 k.

的连续窗口的最大总和

模式 2:(可变窗口大小)具有 <condition> 的最大子数组/子字符串示例:sum <=k.

  • 方法:
    • 蛮力: 生成所有可能的子数组并选择最大长度的子数组 sum <=k (时间复杂度为 $o(n^2)$。
  • 最佳/最佳: 利用两个指针和滑动窗口将时间复杂度降低到o(n)

模式 3:没有子数组/子字符串,其中 <condition> 就像 sum=k。
这个问题很难解决,因为何时扩展(右++)或何时收缩(左++)变得很困难。

这个问题可以用模式2
解决 用于解决诸如查找 sum =k 的子串数量之类的问题。

  • 这可以分解为

    协和·太初
    协和·太初

    国内首个针对罕见病领域的AI大模型

    协和·太初 38
    查看详情 协和·太初
    • 查找 sum<=k ----x
    • 的子数组
    • 查找子数组 sum<=k-1----y 那么结果将是 x-y = 答案

模式 4:找到最短/最小窗口 <condition>

模式 2 的不同方法
示例:最大子数组 sum<=k

public class sample{
    public static void main(string args[]){
        n = 10;
        int arr[] = new int[n];

        //brute force approach for finding the longest subarray with sum <=k
        //tc : o(n^2)
        int maxlen=0;
        for(int i =0;i<arr.length;i++){
            int sum =0;
            for(int j = i+1;j<arr.length;j++){
                sum+=arr[j];
                if(sum<=k){
                    maxlen = integer.max(maxlen, j-i+1);
                }
                else if(sum > k) break; /// optimization if the sum is greater than the k, what is the point in going forward? 
            }
        }
登录后复制

使用两个指针和滑动窗口的更好方法

        //o(n+n) in the worst case r will move from 0 to n and in the worst case left will move from 0 0 n as well so 2n
        int left = 0;
        int right =0;
        int sum = 0;
        int maxlen = 0;
        while(right<arr.length){
            sum+=arr[right];
            while(sum > k){
                sum = sum-arr[left];
                left++;
            }
            if(sum <=k){
                maxlen = integer.max(maxlen, right-left+1); //if asked to print max subarray length,we print maxlen else asked for printing the subarray keep track of
                // left and right in a different variable 
            }
            right++;
        }
登录后复制

最佳方法
我们知道,如果找到子数组,我们将其长度存储在 maxlen 中,但是在添加 arr[right] 时,如果总和大于 k,那么当前我们通过执行 sum = sum-arr[left] 和 left++ 来向左收缩。
我们知道当前的最大长度是 maxlen,如果我们继续缩小左索引,我们可能会得到另一个满足条件的子数组(<=k),但长度可能小于当前的 maxlen 那么我们不会更新 maxlen,直到我们找到另一个满足条件且具有 len > maxlen 的子数组,则仅更新 maxlen。

当子数组不满足条件 (<=k)int left = 0 时,最佳方法是仅在子数组长度大于 maxlen 时收缩左侧。

        int right =0;
        int sum = 0;
        int maxLen = 0;
        while(right<arr.length){
            sum+=arr[right];
            if(sum > k){// this will ensure that the left is incremented one by one (not till the sum<=k because this might reduce the length i.e right-left+1  which will not be taken into consideration)
                sum = sum-arr[left];
                left++;
            }
            if(sum <=k){
                maxLen = Integer.max(maxLen, right-left+1); //if asked to print max subarray length,we print maxLen else asked for printing the subarray keep track of
                // left and right in a different variable 
            }
            right++;
        }

    }
}

登录后复制

以上就是两个指针和滑动窗口模式的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号