825. Friends Of Appropriate Ages

网友投稿 554 2022-09-04

825. Friends Of Appropriate Ages

825. Friends Of Appropriate Ages

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: 2Explanation: 2 people friend request each other.

Example 2:

Input: [16,17,18]Output: 2Explanation: 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.

class Solution // 判断A是否会向B发送申请 boolean good(int A, int B) { if (B <= 0.5 * A + 7) { return false; } if (B > 100 && A < 100) { return false; } return true; } public int numFriendRequests(int[] ages) { if (ages == null || ages.length <= 1) { return 0; } // 一个map记录此年龄的人可以发送的请求个数 HashMap map = new HashMap<>(); int ans = 0; // 进行排序,在内部循环的时候会用到 Arrays.sort(ages); // 遍历 for (int i = 0; i < ages.length; i++) { // 重用以前数据 if (map.containsKey(ages[i])) { ans += map.get(ages[i]); continue; } // 计算此年龄的人可以发送的请求数 int sum = 0; for (int j = 0; j < ages.length && ages[j] <= ages[i]; j++) { if (i != j && good(ages[i], ages[j])) { ++sum; } } map.put(ages[i], sum); ans += sum; } return

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【基础】开发中经常用到的4个四舍五入函数(四舍五入函数是什么意思)
下一篇:38. Count and Say
相关文章

 发表评论

暂时没有评论,来抢沙发吧~