返回介绍

solution / 0800-0899 / 0825.Friends Of Appropriate Ages / README_EN

发布于 2024-06-17 01:03:34 字数 4262 浏览 0 评论 0 收藏 0

825. Friends Of Appropriate Ages

中文文档

Description

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_.

 

Example 1:

Input: ages = [16,16]
Output: 2
Explanation: 2 people friend request each other.

Example 2:

Input: ages = [16,17,18]
Output: 2
Explanation: Friend requests are made 17 -> 16, 18 -> 17.

Example 3:

Input: ages = [20,30,100,110,120]
Output: 3
Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.

 

Constraints:

  • n == ages.length
  • 1 <= n <= 2 * 104
  • 1 <= ages[i] <= 120

Solutions

Solution 1

class Solution:
  def numFriendRequests(self, ages: List[int]) -> int:
    counter = Counter(ages)
    ans = 0
    for i in range(1, 121):
      n1 = counter[i]
      for j in range(1, 121):
        n2 = counter[j]
        if not (j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
          ans += n1 * n2
          if i == j:
            ans -= n2
    return ans
class Solution {
  public int numFriendRequests(int[] ages) {
    int[] counter = new int[121];
    for (int age : ages) {
      ++counter[age];
    }
    int ans = 0;
    for (int i = 1; i < 121; ++i) {
      int n1 = counter[i];
      for (int j = 1; j < 121; ++j) {
        int n2 = counter[j];
        if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
          ans += n1 * n2;
          if (i == j) {
            ans -= n2;
          }
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int numFriendRequests(vector<int>& ages) {
    vector<int> counter(121);
    for (int age : ages) ++counter[age];
    int ans = 0;
    for (int i = 1; i < 121; ++i) {
      int n1 = counter[i];
      for (int j = 1; j < 121; ++j) {
        int n2 = counter[j];
        if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
          ans += n1 * n2;
          if (i == j) ans -= n2;
        }
      }
    }
    return ans;
  }
};
func numFriendRequests(ages []int) int {
  counter := make([]int, 121)
  for _, age := range ages {
    counter[age]++
  }
  ans := 0
  for i := 1; i < 121; i++ {
    n1 := counter[i]
    for j := 1; j < 121; j++ {
      n2 := counter[j]
      if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
        ans += n1 * n2
        if i == j {
          ans -= n2
        }
      }
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文