具体思路:
滑动窗口的一种船新题型;
逐个元素遍历,确定满足要求的左右子区间;
自己还是头一次见这种思路;
具体代码:
class Solution {
public:
int numFriendRequests(vector<int>& ages) {
int cnt=0;
sort(ages.begin(),ages.end());
int l=0,r=0;
for(auto ele:ages){
if(ele<15){
r++;
continue;
}
while(ages[l] <= 0.5 * ele + 7)
l++;
while(r+1<ages.size()&&ages[r+1]<=ele)
r++;
cnt+=r-l;
}
return cnt;
}
};