Skip to contentKEEP THE CRAFTTHE SANCTUARY

Friends of Appropriate Ages

Count one-way friend requests by counting people per age and checking the rules once for every pair of ages.

The problem

Implement Solution.numFriendRequests(ages), where ages[i] is the age of person i. Person x sends a friend request to a different person y unless at least one of these is true: ages[y] <= 0.5 * ages[x] + 7, ages[y] > ages[x], or ages[y] > 100 and ages[x] < 100. Requests are one-way: x asking y does not mean y asks x. Nobody sends a request to themselves. Return the total number of requests. For example, [16, 16] returns 2, because each 16-year-old asks the other. 1 <= len(ages) <= 20000 and 1 <= ages[i] <= 120.

Starter code

class Solution:
    def numFriendRequests(self, ages):
        pass

Public examples

  1. ages = [16, 16]gives2
  2. ages = [120]gives0
  3. ages = [14, 14, 14]gives0

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.