[LeetCode] 602. Friend Requests II: Who Has Most Friend? 朋友请求 II: 谁有最多的朋友?
In social network like Facebook or Twitter, people send friend requests and accept others' requests as well.
Table request_accepted
holds the data of friend acceptance, while requester_id and accepter_id both are the id of a person.
| requester_id | accepter_id | accept_date|
|--------------|-------------|------------|
| 1 | 2 | 2016_06-03 |
| 1 | 3 | 2016-06-08 |
| 2 | 3 | 2016-06-08 |
| 3 | 4 | 2016-06-09 |
Write a query to find the the people who has most friends and the most friends number. For the sample data above, the result is:
| id | num |
|----|-----|
| 3 | 3 |
Note:
- It is guaranteed there is only 1 people having the most friends.
- The friend request could only been accepted once, which mean there is no multiple records with the same requester_id and accepter_id value.
Explanation:
The person with id '3' is a friend of people '1', '2' and '4', so he has 3 friends in total, which is the most number than any others.Follow-up:
In the real world, multiple people could have the same most number of friends, can you find all these people in this case?
Algorithm
Being friends is bidirectional, so if one person accepts a request from another person, both of them will have one more friend.
Thus, we can union column requester_id and accepter_id, and then count the number of the occurrence of each person.
select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted;
Note: Here we should use union all instead of union because union all will keep all the records even the 'duplicated' one.
解法:
select ids as id, cnt as num
from
(
select ids, count(*) as cnt
from
(
select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted
) as tbl1
group by ids
) as tbl2
order by cnt desc
limit 1
;
解法2:
select a.id, count(*) as num
from
(select requester_id as id from request_accepted
union all
select accepter_id as id from request_accepted) a
group by id
order by num desc
limit 1
解法3:
select t2.Id as id, t2.num as num
from (
select t1.Id, sum(cnt) as num
from(
select accepter_id as Id, count(*) as cnt
from request_accepted
group by accepter_id union all select requester_id as Id, count(*) as cnt
from request_accepted
group by requester_id) t1 group by t1.Id ) t2 order by t2.num DESC
limit 1
All LeetCode Questions List 题目汇总
[LeetCode] 602. Friend Requests II: Who Has Most Friend? 朋友请求 II: 谁有最多的朋友?的更多相关文章
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 7-unittest和requests重构、封装处理get/post请求
1.概念说明 ① unittest:python中自带的单元测试框架,封装了一些校验返回的结果方法和一些用例执行前的初始化操作 ② TestCase:测试用例 ③ TestSuite:测试套,即多个测 ...
- 使用requests库提交multipart/form-data 格式的请求
前言: Requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求 ...
- requests模块发送带headers的Get请求和带参数的请求
1.在PyCharm开发工具中新建try_params.py文件: 2.try_params.py文件中编写代码: import requests#设置请求Headers头部header = {&qu ...
- 剑指 Offer 56 - II. 数组中数字出现的次数 II + 位运算
剑指 Offer 56 - II. 数组中数字出现的次数 II Offer_56_2 题目详情 解题思路 java代码 package com.walegarrett.offer; /** * @Au ...
- 剑指 Offer 32 - II. 从上到下打印二叉树 II + 层次遍历二叉树 + 按层存储
剑指 Offer 32 - II. 从上到下打印二叉树 II Offer_32 题目描述: 题解分析: 这道题我一开始想到的解决方法较粗暴,就是使用两个变量来记录当前层的节点数和下一层的结点数. 以上 ...
- 剑指 Offer 32 - II. 从上到下打印二叉树 II
剑指 Offer 32 - II. 从上到下打印二叉树 II 从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行. 例如: 给定二叉树: [3,9,20,null,null,1 ...
随机推荐
- SpringBoot项目下的mvnw与mvnw.cmd
Maven是一个常用的构建工具,但是Maven的版本和插件的配合并不是那么完美,有时候你不得不切换到一个稍微旧一些的版本,以保证所有东西正常工作. 而Gradle提供了一个Wrapper,可以很好解决 ...
- R笔记整理(持续更新中)
1. 安装R包 install.packages("ggplot2") #注意留意在包的名称外有引号!!! library(ggplot2) #在加载包的时候,则不需要在包的名称外 ...
- java项目部署到LIINUX
天领导给个任务,把java项目部署到liunx服务器上.现记录步骤,方便以后查看.项目部署服务器步骤:服务器信息:弹性IP地址:xx.xx.xxx.xx账号:root密码:cjw@100 数据库信息: ...
- java将图片输出base64位码显示
注意需要过滤:\r \n数据 jkd1.7的 import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; /** * 网络图片转换Base ...
- Win如何查看某个端口被谁占用并停掉
第一步在我们的电脑上按win+R键打开运行,输入cmd, 第二步进去命令提示符之后,输入“netstat -ano”,按回车键,查出所有端口,如下图所示: 第三步如果我们想找8089端口,输入nets ...
- [codewars] - int32 to IPv4 二进制十进制 ip地址转换
原题 https://www.codewars.com/kata/int32-to-ipv4/train/java Take the following IPv4 address: 128.32.10 ...
- SVN 常用 还原项目
1.先修改本来两个文件,然后再提交到SVN 2.在日志界面,查看提交的文件,找到对应的版本号 3.找到对应的版本号(这里的版本号是1995,我提交生成的版本号 的前一个版本 才是我未作出修改的版本), ...
- 解决js加减乘除精度问题
// 加法 const accAdd = (arg1, arg2) => { var r1, r2, m; try { r1 = arg1.toString(). ...
- 洛谷P2659 美丽的序列
题目 该题目可以用辅助数组l[i], r[i]来指向以data[i]为最小值的左端点和右端点.然后最后枚举每个data[i]寻找每个data[i]的美丽值的最大值. 然后辅助数组可以用单调栈求出. # ...
- 初识es
初识es es是什么? es是基于Apache Lucene的开源分布式(全文)搜索引擎,,提供简单的RESTful API来隐藏Lucene的复杂性. es除了全文搜索引擎之外,还可以这样描述它: ...