LeetCode 825 Friends Of Appropriate Ages
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相关推荐
- leetcode 825. Friends Of Appropriate Ages
leetcode 825. Friends Of Appropriate Ages 原题地址:https://leetcode.com/problems/friends-of-appropriate- ...
- 825. Friends Of Appropriate Ages**
825. Friends Of Appropriate Ages** https://leetcode.com/problems/friends-of-appropriate-ages/ 题目描述 S ...
- LeetCode 825 friends-of-appropriate-ages
LeetCode 825 仔细分析一下条件: age[B] <= 0.5 * age[A] + 7 age[B] > age[A] age[B] > 100 && a ...
- 【LeetCode】825. Friends Of Appropriate Ages 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/friends-o ...
- 【Leetcode】825. Friends Of Appropriate Ages
题目地址: https://leetcode.com/problems/friends-of-appropriate-ages/ 给定nnn个人的年龄,当满足以下要求的时候aaa不会给bbb发消息: ...
- LeetCode 825. 适龄的朋友(计数排序+前缀和)
文章目录 1. 题目 2. 解题 1. 题目 人们会互相发送好友请求,现在给定一个包含有他们年龄的数组,ages[i] 表示第 i 个人的年龄. 当满足以下任一条件时,A 不能给 B(A.B不为同一人 ...
- java人员安排表_Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)
732. 我的日程安排表 III 实现一个 MyCalendar 类来存放你的日程安排,你可以一直添加新的日程安排. MyCalendar 有一个 book(int start, int end)方法 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- leetcode每日一题825. 适龄的朋友 快排+双指针
本文目录 leetcode每日一题825. 适龄的朋友 快排+双指针 写在前面 题目 示例 提示 思路 代码实现 执行结果 写在后面 leetcode每日一题825. 适龄的朋友 快排+双指针 写在前 ...
最新文章
- Excel向数据库插入数据和数据库向Excel导出数据
- 铁大Facebook——十天冲刺(5)
- 10012 - How Big Is It?
- ant编译java工程总结
- autocad软件提供的哪些功能可以提高设计效率?_节省80%制图时间:AutoCAD如何有效“手脑”结合,释放创作潜能?...
- C# 制作Com组件:java调用.net DLL的方法
- jquery ajax post请求连续多个问号特殊数据异常问题
- Fortinet再获Gartner 2021广域网边缘基础设施魔力象限领导者
- 什么是Windows内核编程
- Ipsec phase1 and phase2
- 路飞学城Python-Day2
- 栈“后进先出”和队列中“先进先出”的含义
- 一个毕业设计 儿童趣味数学 app
- 4.Oracle PDB官方解读- Lone PDB对比 non-CDB
- Mac升级文件不见了怎么恢复?
- GB50016计算机房设计规定,建筑设计防火规范GB50016-2014
- 网络协议-TCP协议详解
- 使用WebSocket实现网页聊天室
- vue姓名筛选模糊搜索
- 我们的时间都去哪了?