LeetCode 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: 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.

给定一个数组作为一群人的年龄分布,判断这群人中一共可以发送最多多少好友申请。

思考

首先题目中的B > A时A不会发送说明了一个人只能向年龄小于等于自己的人发送,由此可以先对数组进行排序,然后进行查找。

还有一个可以借鉴的就是同龄人发送的个数是一样的,可以记录在数组或者HashMap中,以便重用。

代码实现

import java.util.Arrays;
import java.util.HashMap;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<Integer, Integer> 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 ans;}
}

感想

HashMap可以用来重用已经计算了的数据。

LeetCode 825 Friends Of Appropriate Ages相关推荐

  1. leetcode 825. Friends Of Appropriate Ages

    leetcode 825. Friends Of Appropriate Ages 原题地址:https://leetcode.com/problems/friends-of-appropriate- ...

  2. 825. Friends Of Appropriate Ages**

    825. Friends Of Appropriate Ages** https://leetcode.com/problems/friends-of-appropriate-ages/ 题目描述 S ...

  3. LeetCode 825 friends-of-appropriate-ages

    LeetCode 825 仔细分析一下条件: age[B] <= 0.5 * age[A] + 7 age[B] > age[A] age[B] > 100 && a ...

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

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

  5. 【Leetcode】825. Friends Of Appropriate Ages

    题目地址: https://leetcode.com/problems/friends-of-appropriate-ages/ 给定nnn个人的年龄,当满足以下要求的时候aaa不会给bbb发消息: ...

  6. LeetCode 825. 适龄的朋友(计数排序+前缀和)

    文章目录 1. 题目 2. 解题 1. 题目 人们会互相发送好友请求,现在给定一个包含有他们年龄的数组,ages[i] 表示第 i 个人的年龄. 当满足以下任一条件时,A 不能给 B(A.B不为同一人 ...

  7. java人员安排表_Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)

    732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  9. leetcode每日一题825. 适龄的朋友 快排+双指针

    本文目录 leetcode每日一题825. 适龄的朋友 快排+双指针 写在前面 题目 示例 提示 思路 代码实现 执行结果 写在后面 leetcode每日一题825. 适龄的朋友 快排+双指针 写在前 ...

最新文章

  1. Excel向数据库插入数据和数据库向Excel导出数据
  2. 铁大Facebook——十天冲刺(5)
  3. 10012 - How Big Is It?
  4. ant编译java工程总结
  5. autocad软件提供的哪些功能可以提高设计效率?_节省80%制图时间:AutoCAD如何有效“手脑”结合,释放创作潜能?...
  6. C# 制作Com组件:java调用.net DLL的方法
  7. jquery ajax post请求连续多个问号特殊数据异常问题
  8. Fortinet再获Gartner 2021广域网边缘基础设施魔力象限领导者
  9. 什么是Windows内核编程
  10. Ipsec phase1 and phase2
  11. 路飞学城Python-Day2
  12. 栈“后进先出”和队列中“先进先出”的含义
  13. 一个毕业设计 儿童趣味数学 app
  14. 4.Oracle PDB官方解读- Lone PDB对比 non-CDB
  15. Mac升级文件不见了怎么恢复?
  16. GB50016计算机房设计规定,建筑设计防火规范GB50016-2014
  17. 网络协议-TCP协议详解
  18. 使用WebSocket实现网页聊天室
  19. vue姓名筛选模糊搜索
  20. 我们的时间都去哪了?

热门文章

  1. C++ - 多继承方式会产生多个虚函数表
  2. ACM-ICPC 2018 沈阳赛区网络预赛(E F G J K)
  3. Python破解WIFI密码完整源代码,实测可成功破解
  4. Siammask源码demo运行配置
  5. ios 弹起软键盘position: fixed;失效
  6. BJDCTF 2nd 刷题记录
  7. python红楼梦人物词频统计_用R进行文本分析初探——以《红楼梦》为例
  8. 在线求助ing~ 急!!
  9. 分享2019年陆陆续续读过的书-附书单
  10. FFplay文档解读-39-视频过滤器十四