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

 

解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:

1
2
3
4
5
6
7
8
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 题目汇总

posted @   轻风舞动  阅读(1349)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示