java:
每日一题快乐打卡 双指针yyds 不是很难 读懂题就可以 虽然我还是看了题解
再坚持几天就有12月小牌咯
class Solution {
public int numFriendRequests(int[] ages) {
int n = ages.length;
int count = 0;
int left = 0;
int right = 0;
Arrays.sort(ages);
for(int age : ages){
if(age <= 14){
continue;
}
while(ages[left] <= 0.5 * age + 7){
++left;
}
while(right < n - 1 && ages[right + 1] <= age){
++right;
}
count += right - left;
}
return count;
}
}
python3:
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
n = len(ages)
ages.sort()
left = right = count = 0
for age in ages:
if age <= 14:
continue
while ages[left] <= 0.5 * age + 7:
left += 1
while right + 1 < n and ages[right + 1] <= age:
right += 1
count += right - left
return count