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?
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
用sliding window写不出
至少可以返回来用暴力做法啊
[一句话思路]:
有duplicate的数组完全可以用hashmap来存数,特别是duplicate特别多的时候
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 一开始没有先想好,hashmap存错了。先想好再写
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
index 还是 nums[index]下次可以检查下
[总结]:
有duplicate的数组完全可以用hashmap来存数
[复杂度]:Time complexity: O(n^2) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
有duplicate的数组完全可以用hashmap来存数,特别是duplicate特别多的时候
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
两个变量的双重循环:
- for (int a : map.keySet()) for (int b : map.keySet())
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
- class Solution {
- public int numFriendRequests(int[] ages) {
- //cc
- if (ages == null || ages.length == 0) return 0;
- //ini: hashmap: age,count
- Map<Integer, Integer> map = new HashMap<>();
- for (int i = 0; i < ages.length; i++) map.put(ages[i], map.getOrDefault(ages[i], 0) + 1);
- int res = 0;
- //for loop: return a* b or a * (a - 1)
- for (int a : map.keySet()) for (int b : map.keySet()) {
- if (valid(a, b))
- res += map.get(a) * (map.get(b) - (a == b ? 1 : 0));
- }
- return res;
- }
- public boolean valid(int a, int b) {
- return !(b <= 0.5 * a + 7 || b > a || (b > 100 && a < 100));
- }
- }
825. Friends Of Appropriate Ages有效的好友请求的数量的更多相关文章
- 李洪强iOS开发本人集成环信的经验总结_09_处理好友请求
李洪强iOS开发本人集成环信的经验总结_09_处理好友请求 实现这种效果: 01 - 遵守处理好友请求的代理协议 02 - 设置代理 03 - 实现代理方法 04 - 实现代理中用到的方法
- 李洪强iOS开发本人集成环信的经验总结_07_监听好友请求
李洪强iOS开发本人集成环信的经验总结_07_监听好友请求 来到Appdalegate中: 遵守代理协议 设置代理 实现监听好友请求的回调的方法
- 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求
李洪强iOS开发本人集成环信的经验总结_06_发送好友请求 同步好友请求 异步好友请求
- 【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/ 题目: Some people will make friend ...
- PC结束 Spark 二次开发 收到自己主动,并允许好友请求
本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...
- Java实现 LeetCode 825 适龄的朋友(暴力)
825. 适龄的朋友 人们会互相发送好友请求,现在给定一个包含有他们年龄的数组,ages[i] 表示第 i 个人的年龄. 当满足以下条件时,A 不能给 B(A.B不为同一人)发送好友请求: age[B ...
- [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] 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 ...
随机推荐
- 11.Python使用Scrapy爬虫小Demo(新手入门)
1.前提:已安装好scrapy,且已新建好项目,编写小Demo去获取美剧天堂的电影标题名 2.在项目中创建一个python文件 3.代码如下所示: import scrapy class movies ...
- 关于python urlopen 一个类似radio流的timeout方法
终极解决方法来啦!看代码感受: import requests import eventlet import time eventlet.monkey_patch() try: with eventl ...
- android 文件上传,中文utf-8编码
要上传文件到后台的php服务器,服务器能收到中文,手机发送过去,却只能收到一堆转了UTF-8的编码(就是要decode后才是中文的编码).android这边上传文件通常是用stream方式上传的,用M ...
- 杂项-数学软件:Maple
ylbtech-杂项-数学软件:Maple Maple是目前世界上最为通用的数学和工程计算软件之一,在数学和科学领域享有盛誉,有“数学家的软件”之称.Maple 在全球拥有数百万用户,被广泛地应用于科 ...
- NFV网络功能虚拟化 基本概念
NFV基本概念 NFV则由运营商联盟提出,为了加速部署新的网络服务,运营商倾向于放弃笨重昂贵的专用网络设备,转而使用标准的IT虚拟化技术来拆分网络功能模块,如DNS.NAT.Firewall等.于是一 ...
- Git操作行
基础层:-----------------#初始化一个版本仓库git init #复制远程版本库git clone url #添加远程版本库origingit remote add origin ur ...
- Java测试用例简介
最近需要向组内其他成员普及一下关于Java测试用例的相关知识,特在此进行一下简单的学习和总结. JUnit简介 JUnit是一个开源的Java单元测试框架,JUnit4对原有的JUnit框架进行了大幅 ...
- Linux系统命令与脚本开发
系统命令 # cat EFO cat >> file << EOF neirong EOF # 清空 >file 清空文件 [root@Poppy conf]# sed ...
- vptr, vtable, virtual base class table
#include <iostream> using namespace std; class X { int x, y, z; }; class Y: public virtual X { ...
- python学习(二十二) String(上)
str1 = "This is a 'test'" print(str1) str1 = 'This is a "test"' print(str1) str1 ...