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] + 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?
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩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 ...
随机推荐
- Spring Cloud微服务框架介绍
Spring Cloud为开发人员提供了一整套的快速构建分布式应用的工具,入服务注册.服务发现.熔断.负载均衡.路由等,提供了开箱即用的各种依赖以及良好的可扩展机制. 目前在Spring Cloud的 ...
- 学习 FPGA之前的基础知识
在学习一门技术之前往往应该从它的编程语言入手,比如学习单片机时,往往从汇编或者C语言入门.所以不少开始接触FPGA的开发人员,往往是从VHDL或者Verilog开始入手学习的.但小编认为,若能先结合& ...
- (转)python virtual_env 的使用 + 将原来的虚拟环境部署到新环境
原文链接: https://blog.csdn.net/poxiaonie/article/details/78820015
- oracle 索引使用小结
1. 普通索引 create index my_index on test (col_1); 可创建合并两列或多列的索引,最多可将32列合并在一个索引中(位图索引最多可合并30列) create in ...
- poj 3518 Prime Gap
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7392 Accepted: 4291 Descrip ...
- FTP服务器的搭建与配置
主要来源:http://www.cnblogs.com/helonghl/articles/5533857.html 1.安装FTP服务器: yum install vsftpd -y 2.启动FTP ...
- Web(Easy UI)
HTML菜鸟教程 http://www.runoob.com/html/html-tables.html EasyUI combobox下拉多选框的实现 https://www.cnblogs.com ...
- letter
while (cin.eof() != true) //cin.eof判断是否到达文件EOF,如果读取到EOF return true,读取到EOF则无法再次输入 while (cin.fail() ...
- Windbg基本命令应用总结
.cordll -ve -u -l //reload core dlls ------加载下载系统文件符号的URL---------- .sympath SRV*C:\Symbols*http://m ...
- C语言调用汇编
程序的入口是main,在main里调用汇编的函数. 首先要解决怎么定义函数的问题 在C语言中,要extern 一个函数声明即可,然后这个函数在汇编里面实现. 在汇编里面,用EXPORT 把C语言定义的 ...