思路:
双指针加排序,排序之后中间的是一定可以,两边边界改变,注意的是如果两个人年纪一样,发出申请的数量也是一定一样,在这个条件之下,不考虑终点比起点小的情况。
class Solution {
public:
static bool cmp(int a, int b){
return a > b;
}
int numFriendRequests(vector<int>& ages) {
sort(ages.begin(), ages.end(), cmp);
int s = 0;
int e = 0;
int l = ages.size();
int an = 0;
int nowAn = 0;
while(s < l && e < l){
if(s != 0 && ages[s] == ages[s-1]){
an += nowAn;
s++;
continue;
}
if(e <= s){
e++;
continue;
}
if(ages[e] <= ages[s] * 0.5 + 7){
nowAn = e - s - 1;
an += e - s - 1;
s++;
continue;
}
e++;
if(e == l){
e = l - 1;
an += e - s;
nowAn = e - s;
s++;
}
}
return an;
}
};