原题链接在这里: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. python 爬取媒体文件(无防火墙)

    #coding = utf-8 import requests import pandas as pd import os,time root_path = './根目录/' input_file = ...

  2. 《Interest Rate Risk Modeling》阅读笔记——第二章:债券价格、久期与凸性

    目录 第二章:债券价格.久期与凸性 思维导图 瞬时回报率-收益率的例子 第二章:债券价格.久期与凸性 思维导图 瞬时回报率-收益率的例子

  3. 【模板】KD-tree

    核心思想: 将空间内的点进行合理划分,以支持有关高维点的操作. 其实就是将线段树搬到了二维及更高维度上. 注意$KD-tree$虽然很像线段树,但其实是一棵二叉搜索树,空间复杂度是$O(n)$的. 查 ...

  4. HBase统计表行数(RowCount)的四种方法

    背景:对于其他数据存储系统来说,统计表的行数是再基本不过的操作了,一般实现都非常简单:但对于HBase这种key-value存储结构的列式数据库,统计 RowCount 的方法却有好几种不同的花样,并 ...

  5. .NET 导入Excel服务器报未在本地计算机上注册 Microsoft.Jet.Oledb.4.0提供程序

    代码已经在本地运行正常,发布到服务器后,一直报未在本地计算机上注册 Microsoft.Jet.Oledb.4.0提供程序, 解决办法:读取EXCEL文件时最好使用ACE方式,jet对于高版本系统的服 ...

  6. Python基础15

    P75. 闭包,需再理解. 装饰器,语法糖

  7. JDK相关目录介绍

    JDK安装后会在硬盘生成一个目录,这个目录被成为JDK安装目录 bin目录: 该目录里用于存放一些可执行文件 .例如:javac.exe(java编译器),java.exe(java运行工具),jar ...

  8. django--JWT认证

    目录 JWT认证 JWT简介 安装 djang-jwt开发 配置 手动签发jwt token 基于django_restframework-jwt的全局认证 全局启用 局部启用禁用:任何一个cbv类首 ...

  9. $.fn.extend 与 $.extend的区别

    今天看到别人写的jquery 代码都是这样的 $.fn.extend 所以查询了一下,因为自己不是前端开发,看到这样写的,感觉很牛逼.从百度上搜到的感觉解释的还是挺好的,作为记录,方便以后查找. 搜索 ...

  10. 同一个POD中默认共享哪些名称空间

    如果通过POD的形式来启动多个容器那么它们的名称空间会是共享的么,所以我这里讨论是在默认情况下同一个POD的不同容器的哪些名称空间是打通的.这里先说一下结论,共享的是UTS.IPC.NET.USER. ...