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. Codeforces I. Barcelonian Distance(暴力)

    题目描述: In this problem we consider a very simplified model of Barcelona city. Barcelona can be repres ...

  2. 用IntelliJ IDEA学习Spring--创建一个简单的项目

    这段时间想学习一下Spring,其实之前学过Spring,只是有些忘记了.而且之前学的时候是适用eclipse学习的,现在好像对IntelliJ这个工具使用挺多的,现在就学习一下这个工具的用法,顺便复 ...

  3. drf框架总结复习(1)

    Serializers 序列化组件 为什么要用序列化组件 当我们做前后端分离的项目~~我们前后端交互一般都选择JSON数据格式,JSON是一个轻量级的数据交互格式. 那么我们给前端数据的时候都要转成j ...

  4. 《BUG创造队》第八次团队作业:Alpha冲刺

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 BUG创造队 作业学习目标 (1)掌握软件测试基础技术.(2)学习 ...

  5. 学习:类和对象——对象模型和this指针

    成员变量和成员函数分开存储: 在C++中,类内的成员变量和成员函数分开存储 第一点:空对象占用内存空间1个字节 第二点:只有非静态成员变量才属于类的对象上,非静态成员函数和静态成员函数和静态成员变量不 ...

  6. 内核用户模式调试支持(Dbgk)

    简介 将详细分析Windows调试的内核模式接口.希望读者对C和通用NT内核体系结构和语义有一些基本的了解.此外,这并不是介绍什么是调试或如何编写调试器.它可以作为经验丰富的调试器编写人员或好奇的安全 ...

  7. 树莓派基于scratch2控制GPIO

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/raspberry_scratch2_gpio_control.m ...

  8. kafka 创建消费者报错

    kafka-console-consumer.sh --zookeeper master:2181,slave1:2181,slave2:2181 --topic test --from-beginn ...

  9. python3 Paramiko模块学习

    简介 ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography). 有了Paramiko以后,我们就可以在Py ...

  10. Nginx服务器的安装

    #解压之前下载的nginx源码安装包 [root@redhat7 nginx-1.8.1]# tar xzvf nginx-1.8.1.tar.gz #进到新解压出来的nginx目录下 [root@r ...