https://leetcode.com/problems/friends-of-appropriate-ages/description/
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/127341/10ms-concise-Java-solution-O(n)-time-and-O(1)-space
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/127303/Java-two-pointer-O(1)-space-solution
Some people will make friend requests. The list of their ages is given and
ages[i]
is the age of the ith person.
Person A will NOT friend request person B (B != A) if any of the following conditions are true:
age[B] <= 0.5 * age[A] + 7
age[B] > age[A]
age[B] > 100 && age[A] < 100
Otherwise, A will friend request B.
Note that if A requests B, B does not necessarily request A. Also, people will not friend request themselves.
How many total friend requests are made?
Example 1:
Input: [16,16] Output: 2 Explanation: 2 people friend request each other.
Example 2:
Input: [16,17,18] Output: 2 Explanation: Friend requests are made 17 -> 16, 18 -> 17.
Example 3:
Input: [20,30,100,110,120] Output: Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
Notes:
1 <= ages.length <= 20000
.1 <= ages[i] <= 120
.
Instead of processing all
20000
people, we can process pairs of (age, count)
representing how many people are that age. Since there are only 120 possible ages, this is a much faster loop.
Algorithm
For each pair
(ageA, countA)
, (ageB, countB)
, if the conditions are satisfied with respect to age, then countA * countB
pairs of people made friend requests.
If
ageA == ageB
, then we overcounted: we should have countA * (countA - 1)
pairs of people making friend requests instead, as you cannot friend request yourself.- Time Complexity: , where is the number of people, and is the number of ages.
- Space Complexity: , the space used to store
count
.
public int numFriendRequests(int[] ages) {
int[] count = new int[121];
for (int age: ages) count[age]++;
int ans = 0;
for (int ageA = 0; ageA <= 120; ageA++) {
int countA = count[ageA];
for (int ageB = 0; ageB <= 120; ageB++) {
int countB = count[ageB];
if (ageA * 0.5 + 7 >= ageB) continue;
if (ageA < ageB) continue;
if (ageA < 100 && 100 < ageB) continue;
ans += countA * countB;
if (ageA == ageB) ans -= countA;
}
}
return ans;
}
Three conditions could be merged to one:
The Person with age A can request person with age B if
The Person with age A can request person with age B if
- B is in range ( 0.5 * A + 7, A ]
public int numFriendRequests(int[] ages) {
int res = 0;
int[] numInAge = new int[121], sumInAge = new int[121];
for(int i : ages)
numInAge[i] ++;
for(int i = 1; i <= 120; ++i)
sumInAge[i] = numInAge[i] + sumInAge[i - 1];
for(int i = 15; i <= 120; ++i) {
if(numInAge[i] == 0) continue;
int count = sumInAge[i] - sumInAge[i / 2 + 7];
res += count * numInAge[i] - numInAge[i]; //people will not friend request themselves, so - numInAge[i]
}
return res;}
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/127029/C++JavaPython-Easy-and-Straight-Forward public int numFriendRequests(int[] ages) {
Map<Integer, Integer> count = new HashMap<>();
for (int age : ages) count.put(age, count.getOrDefault(age, 0) + 1);
int res = 0;
for (Integer a : count.keySet()) for (Integer b : count.keySet())
if (request(a, b)) res += count.get(a) * (count.get(b) - (a == b ? 1 : 0));
return res;
}
private boolean request(int a, int b) {
return !(b <= 0.5 * a + 7 || b > a || (b > 100 && a < 100));
}