原题链接在这里: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:

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

Example 2:

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

题解:

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:

 class Solution {
public int numFriendRequests(int[] ages) {
if(ages == null || ages.length == 0){
return 0;
} HashMap<Integer, Integer> hm = new HashMap<>();
for(int age : ages){
hm.put(age, hm.getOrDefault(age, 0) + 1);
} int res = 0;
for(int a : hm.keySet()){
for(int b : hm.keySet()){
if(couldSendRequest(a, b)){
res += hm.get(a) * (hm.get(b) - (a == b ? 1 : 0));
}
}
} return res;
} private boolean couldSendRequest(int a, int b){
return !(b <= a*0.5 + 7 || b > a || (b > 100 && a < 100));
}
}

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:

 class Solution {
public int numFriendRequests(int[] ages) {
if(ages == null || ages.length == 0){
return 0;
} int [] count = new int[121];
for(int age : ages){
count[age]++;
} int [] sum = new int[121];
for(int i = 1; i<121; i++){
sum[i] = sum[i-1] + count[i];
} int res = 0;
for(int i = 15; i<121; i++){
if(count[i] == 0){
continue;
} int bCount = sum[i] - sum[i/2+7];
res += bCount * count[i] - count[i];
} return res;
}
}

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. 2 datax mysql 和 mysql之间相互导入

    插件文档: https://github.com/alibaba/DataX/blob/master/hdfswriter/doc/hdfswriter.md   1,参照第1篇日记,安装好datax ...

  2. PHP的小技巧

    PHP的小技巧fdd()[0]函数后面可以直接加数组索引 这样可以省内存占用啦 代码也更简洁

  3. HyperLogLog算法分析及其应用

    HyperLogLog 算法的原理讲解以及 Redis 是如何应用它的 探索HyperLogLog算法(含Java实现) 神奇的HyperLogLog算法 Sketch of the Day: Hyp ...

  4. RC4算法

    RC4算法简介:https://baike.baidu.com/item/RC4%E7%AE%97%E6%B3%95/9686396?fr=aladdin RC4算法java实现: /** * RC4 ...

  5. 发送邮件报错javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed

    关于使用javaMail发送邮件报错:javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multip ...

  6. 2019-11-29-C#-反射调用私有事件

    原文:2019-11-29-C#-反射调用私有事件 title author date CreateTime categories C# 反射调用私有事件 lindexi 2019-11-29 08: ...

  7. c#语法复习总结(2)-数据类型

    C#数据类型可以分值类型和引用类型.值类型,先说说一个概念 c#栈和堆. 一,栈和堆. 堆:在c里面叫堆,在c#里面其实叫托管堆.为什么叫托管堆,我们往下看. 栈:就是堆栈,因为和堆一起叫着别扭,就简 ...

  8. 解决plsql中文显示问号(???)问题

    最近新买的电脑,配置好数据库连接后,plsql查看数据与插入中文数据都显示问号(???),同事的都正常显示,查看了很多资料,有的说是数据库字符集的原因让修改数据库的字符集,但是我的数据库都是远程连接正 ...

  9. 1-Consul系列文章

    使用Consul做服务发现的若干姿势 Consul的反熵 [Consul]Consul架构-简介

  10. 关于final

    最近见的一道选择题 刚学习一直认为final修饰,为常量,必须声明时被初始化,现在又明白第二种情况可以通过创建对象之后由构造方法立即初始化. 1.final修饰类不能被继承 2.final修饰方法不能 ...