题解 | #相差不超过k的最多数#
相差不超过k的最多数
https://www.nowcoder.com/practice/562630ca90ac40ce89443c91060574c6
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
int main() {
int n, k;
while (cin >> n >> k) {
vector<int> vec(n,0);
for(int i=0;i<n;i++){
cin>>vec[i];
}
int slow=0;
int res=0;
int fast=0;
sort(vec.begin(),vec.end());
while(slow<n){
while(abs(vec[fast]-vec[slow]) <= k && fast<n){
fast++;
}
res=max(res,fast-slow);
slow++;
}
cout<<res<<endl;
}
}
// 64 位输出请用 printf("%lld")
华为HUAWEI工作强度 1383人发布
查看2道真题和解析