Leetcode 602. Friend Requests II: Who Has Most Friend?

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?
create table request_accepted
(
requester_id int,
accepter_id int,
accept_date date
)

insert into request_accepted values(1,2,'2016-06-03')
insert into request_accepted values(1,3,'2016-06-08')
insert into request_accepted values(2,3,'2016-06-08')
insert into request_accepted values(3,4,'2016-06-09')

select * from request_accepted

 

select  top 1 id1, count(*) as num 
from 
(
select requester_id as id1, accepter_id as id2 from request_accepted
union all
select accepter_id as id1, requester_id as id2 from request_accepted
) as tpl
group by id1
order by num desc

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值