Leetcode Problem 825. Friends Of Appropriate Ages

825. Friends Of Appropriate Ages

Leetcode Solutions

Counting and Prefix Sum Approach

  1. Initialize a frequency array count of size 121 (since ages are from 1 to 120) to store the count of each age.
  2. Populate the count array with the frequency of each age from the input ages array.
  3. Create a prefix sum array prefix of the same size as count.
  4. Calculate the prefix sum such that prefix[i] contains the sum of counts up to age i.
  5. Initialize a variable requests to store the total number of friend requests.
  6. Iterate over each age i from 15 to 120 (since no one under 15 can send friend requests based on the conditions).
  7. If count[i] is zero, skip to the next age.
  8. Calculate the number of people who can receive a request from age i using the prefix sum array.
  9. Update requests by adding the number of new requests for age i.
  10. Return the total number of friend requests requests.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...