[LeetCode] Friends Of Appropriate Ages

825. Friends Of Appropriate Ages

There are n persons on a social media website. You are given an integer array ages where ages[i] is the age of the ith person.

A Person x will not send a friend request to a person y (x != y) if any of the following conditions is true:

  • age[y] <= 0.5 * age[x] + 7
  • age[y] > age[x]
  • age[y] > 100 && age[x] < 100

Otherwise, x will send a friend request to y.

Note that if x sends a request to y, y will not necessarily send a request to x. Also, a person will not send a friend request to themself.

Return the total number of friend requests made.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int numFriendRequests(vector<int>& A) {
int res = 0;
int freq[121] = {};
for(auto& a : A) freq[a]++;
for(int i = 15; i <= 120; i++) {
for(int j = i * 0.5 + 8; j <= i; j++)
res += freq[j] * (freq[i] - (i==j));
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/08/PS/LeetCode/friends-of-appropriate-ages/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.