LeetCode 825. Friends Of Appropriate Ages
原题链接在这里: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] + 7age[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的更多相关文章
- 【LeetCode】825. Friends Of Appropriate Ages 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/friends-o ...
- Java实现 LeetCode 825 适龄的朋友(暴力)
825. 适龄的朋友 人们会互相发送好友请求,现在给定一个包含有他们年龄的数组,ages[i] 表示第 i 个人的年龄. 当满足以下条件时,A 不能给 B(A.B不为同一人)发送好友请求: age[B ...
- 825. Friends Of Appropriate Ages有效的好友请求的数量
[抄题]: Some people will make friend requests. The list of their ages is given and ages[i] is the age ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- [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 ...
- Leetcode题解 - 部分中等难度算法题解(56、957、825、781、1324、816)
957. N 天后的牢房 思路: 模拟变换,当N天结合后返回 => 当N非常大的时候,超时 => 一般N很大的时候,这种题目必然存在循环,所以记录找过的状态,一旦出现已经访问过的状态可立即 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [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 ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
随机推荐
- vue-v-xxx基于 Vue拓展的 v-xxx 库
君问归期未有期,巴山夜雨涨秋池. 何当共剪西窗烛,却话巴山夜雨时. 作为vue轻车熟路的老司机,经常会用到一些指令,vue官方提供的指令又太少,无法满足旺盛的欲望,而每次要写一遍,终日郁郁寡欢,从小就 ...
- C#根据流下载文件
C#从服务器下载文件可以使用下面4个方法:TransmitFile.WriteFile.WriteFile和流方式下载文件,并保存为相应类型,方法如下: .TransmitFile实现下载 prote ...
- PIE SDK创建金字塔算法
1.算法功能简介 为栅格影像建立了金字塔,这些影像便能快速进行显示.除了在屏幕上显示外,金字塔还包含了很多其他信息.如果没有金字塔,那么在显示时就要访问整理栅格数据集,然后进行大量计算来选择哪些栅格像 ...
- WebSocket实现Web端即时通信
前言 WebSocket 是HTML5开始提供的一种在浏览器和服务器间进行全双工通信的协议.目前很多没有使用WebSocket进行客户端服务端实时通信的web应用,大多使用设置规则时间的轮询,或者使用 ...
- 尝试在Mac/iOS上使用tcmalloc库
概述 TCMalloc 是 Google 开发的内存分配器,在不少项目中都有使用,例如在 Golang 中就使用了类似的算法进行内存分配.它具有现代化内存分配器的基本特征:对抗内存碎片. ...
- 英语AmbraGrisea龙涎香
龙涎香AmbraGrisea是抹香鲸科动物抹香鲸的肠内分泌物的干燥品.取自宰杀的抹香鲸肠内分泌物(即鲸鱼的粪便,它是抹香鲸吞食墨鱼后,胃肠道分泌出来的灰黑色的蜡状排泄物).其味甘.气腥.性涩,具有行气 ...
- 你的MES系统又失败了?正确的打开方式在这里
都知道MES实施艰难,真正成功的很少:有人戏称:10个MES,7个失败.1个不死不活.1个伪成功,最后一个仍需努力. 导致MES实施失败的原因有很多,所谓“成功的MES是一样的,失败的MES各有各的失 ...
- Go语言学习——三分钟看透iota
源起枚举 最近做需求时,有一个需要枚举的场景,大概有10+个枚举类型,不愿意像定义一个开关那样敷衍的写成 const ( SwitchOff = 0 SwitchOn = 1 ) 显得不够精致~ 于是 ...
- spoon数据转换中文乱码(kettle)
(1) 查看mysql数据库是否为utf8(status) (2) 设置spoon (3) 文本打开spoon.bat,找到set OPT=%OPT% %PENTAHO_DI_JAVA_OPTIONS ...
- mysql 连接数用完,root也无法登陆的处理方法
gdb -p $(pidof mysqld) -ex "set max_connections=1500" -batch 使用 gdb 临时调大 参数 max_connection ...