原题链接在这里:https://leetcode.com/problems/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:

  1. Input: [16,16]
  2. Output: 2
  3. Explanation: 2 people friend request each other.

Example 2:

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

Example 3:

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

Notes:

  • 1 <= ages.length <= 20000.
  • 1 <= ages[i] <= 120.

题解:

Accumlate the frequency of different ages.

If age a and age b could send request, and a != b, then res += a freq * b freq.

If a == b, since no one could send friend request to themselves, the request is a freq * (a freq - 1). Send friend request to other people with same age.

Time Complexity: O(n^2). n = ages.length.

Space: O(n).

AC Java:

  1. class Solution {
  2. public int numFriendRequests(int[] ages) {
  3. if(ages == null || ages.length == 0){
  4. return 0;
  5. }
  6.  
  7. HashMap<Integer, Integer> hm = new HashMap<>();
  8. for(int age : ages){
  9. hm.put(age, hm.getOrDefault(age, 0) + 1);
  10. }
  11.  
  12. int res = 0;
  13. for(int a : hm.keySet()){
  14. for(int b : hm.keySet()){
  15. if(couldSendRequest(a, b)){
  16. res += hm.get(a) * (hm.get(b) - (a == b ? 1 : 0));
  17. }
  18. }
  19. }
  20.  
  21. return res;
  22. }
  23.  
  24. private boolean couldSendRequest(int a, int b){
  25. return !(b <= a*0.5 + 7 || b > a || (b > 100 && a < 100));
  26. }
  27. }

With 3 conditions, we only care the count of B in range (a/2+7, a].

Get the sum count of b and * a count - a count since people can't sent friend request to themselves.

Since A > B >= 0.5*A+7, A > 0.5*A+7. Then A>14. Thus i is started from 15.

Time Complexity: O(n).

Space: O(1).

AC Java:

  1. class Solution {
  2. public int numFriendRequests(int[] ages) {
  3. if(ages == null || ages.length == 0){
  4. return 0;
  5. }
  6.  
  7. int [] count = new int[121];
  8. for(int age : ages){
  9. count[age]++;
  10. }
  11.  
  12. int [] sum = new int[121];
  13. for(int i = 1; i<121; i++){
  14. sum[i] = sum[i-1] + count[i];
  15. }
  16.  
  17. int res = 0;
  18. for(int i = 15; i<121; i++){
  19. if(count[i] == 0){
  20. continue;
  21. }
  22.  
  23. int bCount = sum[i] - sum[i/2+7];
  24. res += bCount * count[i] - count[i];
  25. }
  26.  
  27. return res;
  28. }
  29. }

LeetCode 825. Friends Of Appropriate Ages的更多相关文章

  1. 【LeetCode】825. Friends Of Appropriate Ages 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/friends-o ...

  2. Java实现 LeetCode 825 适龄的朋友(暴力)

    825. 适龄的朋友 人们会互相发送好友请求,现在给定一个包含有他们年龄的数组,ages[i] 表示第 i 个人的年龄. 当满足以下条件时,A 不能给 B(A.B不为同一人)发送好友请求: age[B ...

  3. 825. Friends Of Appropriate Ages有效的好友请求的数量

    [抄题]: Some people will make friend requests. The list of their ages is given and ages[i] is the age ...

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. [LeetCode] 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 ...

  6. Leetcode题解 - 部分中等难度算法题解(56、957、825、781、1324、816)

    957. N 天后的牢房 思路: 模拟变换,当N天结合后返回 => 当N非常大的时候,超时 => 一般N很大的时候,这种题目必然存在循环,所以记录找过的状态,一旦出现已经访问过的状态可立即 ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. [Swift]LeetCode825. 适龄的朋友 | 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 ...

  9. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

随机推荐

  1. 2019年上-C语言程序设计-第1次blog作业

    准备工作(20分) 1.在博客园申请个人博客,以真实姓名和学号加入班级博客(链接地址) 2.关注邹欣老师博客(并寻找答案:邹欣老师是谁?是做什么的?).关注任课老师博客 3.注册登录中国大学MOOC网 ...

  2. Docker下构建centos7容器无法使用systemctl命令的解决办法

    最近在使用docker 构建centos7 容器时,发现无法使用systemctl 命令.后来万能的百度解决了问题,随记之以备后用. 解决办法: docker run --privileged -it ...

  3. Mybatis设置主键自增

    <insert id="insertArea" useGeneratedKeys="true" keyProperty="areaId" ...

  4. 初识Go语言--(1)环境安装

    1.安装包下载:https://golang.google.cn/dl/ 2. 一直点击下一步进行安装 3.安装后查看版本及帮助信息

  5. CountdownLatch例子

    CountdownLatch 一个线程或者多个线程等待其他线程完成了再接着往下执行 public class CountDownLatchTest { ); private static Random ...

  6. 【04】Kubernets:资源清单(pod)

    写在前面的话 前面我们提到过,纯手敲 K8S 名称管理 K8S 服务只是作为我们了解 K8S 的一种方案,而我们最终管理 K8S 的方法还是通过接下来的资源清单的方式进行管理. 所以从本章节开始,将会 ...

  7. MySQL如何定位慢sql

    MySQL如何定位慢sql MySQL"慢SQL"定位 数据库调优我个人觉得必须要明白两件事 1.定位问题(你得知道问题出在哪里,要不然从哪里调优呢) 2.解决问题(这个没有基本的 ...

  8. 新一代ActiveMQ —— Apache ActiveMQ Artemis

    资料: .net demo : https://github.com/apache/activemq-artemis/tree/master/examples/protocols/amqp/dotne ...

  9. ADO.NET中使用事务

    using (SqlConnection conn = new SqlConnection(k2ConnStr)) { SqlCommand cmd = new SqlCommand(sql, con ...

  10. Windows Server - 用tomcat部署finereport

    原博地址:https://blog.csdn.net/qq_39019865/article/details/80969728