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: 谁有最多的朋友?的更多相关文章

  1. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  2. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  3. [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 ...

  4. 7-unittest和requests重构、封装处理get/post请求

    1.概念说明 ① unittest:python中自带的单元测试框架,封装了一些校验返回的结果方法和一些用例执行前的初始化操作 ② TestCase:测试用例 ③ TestSuite:测试套,即多个测 ...

  5. 使用requests库提交multipart/form-data 格式的请求

    前言: Requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求 ...

  6. requests模块发送带headers的Get请求和带参数的请求

    1.在PyCharm开发工具中新建try_params.py文件: 2.try_params.py文件中编写代码: import requests#设置请求Headers头部header = {&qu ...

  7. 剑指 Offer 56 - II. 数组中数字出现的次数 II + 位运算

    剑指 Offer 56 - II. 数组中数字出现的次数 II Offer_56_2 题目详情 解题思路 java代码 package com.walegarrett.offer; /** * @Au ...

  8. 剑指 Offer 32 - II. 从上到下打印二叉树 II + 层次遍历二叉树 + 按层存储

    剑指 Offer 32 - II. 从上到下打印二叉树 II Offer_32 题目描述: 题解分析: 这道题我一开始想到的解决方法较粗暴,就是使用两个变量来记录当前层的节点数和下一层的结点数. 以上 ...

  9. 剑指 Offer 32 - II. 从上到下打印二叉树 II

    剑指 Offer 32 - II. 从上到下打印二叉树 II 从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行. 例如: 给定二叉树: [3,9,20,null,null,1 ...

随机推荐

  1. C# 验证控件组

    C# 验证控件允许使用ValidationGroup给验证控件分组,分组后的两组验证控件可以独立使用,互不相干.比如一个页面有登录和注册两个部分,假如使用验证控件组,提交的时候会对所有的验证控件进行验 ...

  2. Hadoop跨集群迁移数据(整理版)

    1. 什么是DistCp DistCp(分布式拷贝)是用于大规模集群内部和集群之间拷贝的工具.它使用Map/Reduce实现文件分发,错误处理和恢复,以及报告生成.它把文件和目录的列表作为map任务的 ...

  3. windows——快速得到某一目录下所有文件的名称

    前言 其实用的是windows上dir命令,能快速得到某一目录下的所有文件名称,天天那么忙都没时间写博客(┬_┬) 步骤 打开cmd并cd到某目录下 C:\Users\Administrator.KI ...

  4. flume的sink写入hive表

    flume的配置文件如下: a1.sources=r1 a1.channels=c1 a1.sinks=s1 a1.sources.r1.type=netcat a1.sources.r1.bind= ...

  5. Access数据库连接封装类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. [Flutter] Style a message chat style-ish bubble

    const kOtherBubblePointer = BorderRadius.only( topRight: Radius.circular(30), bottomLeft: Radius.cir ...

  7. Tips on Probability Theory

    1.独立与不相关 随机变量X和Y相互独立,有:E(XY) = E(X)E(Y). 独立一定不相关,不相关不一定独立(高斯过程里二者等价) .对于均值为零的高斯随机变量,“独立”和“不相关”等价的. 独 ...

  8. LeetCode 1059. All Paths from Source Lead to Destination

    原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/ 题目: Given the edges ...

  9. vault 使用 中间ca 进行证书管理

    使用vault 进行pki 管理是很方便的,以前测试的都是由根证书进行证书签发,这次使用中间ca 进行签发 所以会有一个证书连 测试使用docker-compose 运行 环境准备 docker-co ...

  10. Centos 7 更换为 阿里云 yum 源

    地址: https://opsx.alibaba.com/ 操作步骤: 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentO ...