蠕动区间(尺取法、双游标)是一个经典的优化算法。
我们以毛毛虫🐛举例说明

具体的,我们看题目

这一题,我们用暴力法,复杂度O(N^2)
先给出暴力法代码
- int ans=n+1;
- for(int tail=0;tail
- int sum=0;
- for(int head=tail;head
- sum+=x[head];
- if(sum>=m){
- ans=min(ans,head+1-tail);
- break;
- }
- }
- }
- cout<
我们看一下蠕动区间怎么写

上面有注释,大家看不明白就私信哦
收集三原色
这道题更难一点……

数据结构
- string x[N];
- map
int> cnt; - set
rgb; - rgb.insert("red");
- rgb.insert("green");
- rgb.insert("blue");
主体部分
- int sum=0,ans=n+1;
- int tail=0,head=0;
- while(1){
- while(head
3){ - string color=x[head++];
- if(!rgb.count(color)) continue;
- ++cnt[color];
- if(cnt[color]==1) sum++;
- }
- if(sum<3) break;
- ans=min(ans,head-tail);
- string color=x[tail++];
- if(!rgb.count(color)) continue;
- --cnt[color];
- if(cnt[color]==0) sum--;
- }
- cout<
希望这些对大家有用,三联,必回
-
相关阅读:
ORB-SLAM2 ---- Tracking::Relocalization函数
【Dubbo框架、Dubbo中角色以及作用、各组件启动流程执行步骤、基于Dubbo实现的远程通信的案例】
socket数据读写
【WebService】C#搭建的标准WebService接口,在使ESB模版作为参数无法获取参数数据
Flutter Unable to ‘pub upgrade‘ flutter tool
论文阅读 (70):Exploring Self-attention for Image Recognition
java计算机毕业设计网上书城网站源码+系统+数据库+lw文档+mybatis+运行部署
计算机内存分配
计算机毕业论文Java项目源码下载基于SSM的旅游资讯网站含前台与后台
TensorFlow 的基本概念和使用场景
-
原文地址:https://blog.csdn.net/zhang040818/article/details/133882343