列表

详情


602. 好友申请 II :谁有最多的好友

在 Facebook 或者 Twitter 这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请。

RequestAccepted 表:

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| requester_id   | int     |
| accepter_id    | int     |
| accept_date    | date    |
+----------------+---------+
(requester_id, accepter_id) 是这张表的主键。
这张表包含发送好友请求的人的 ID ,接收好友请求的人的 ID ,以及好友请求通过的日期。

 

写一个查询语句,找出拥有最多的好友的人和他拥有的好友数目。

生成的测试用例保证拥有最多好友数目的只有 1 个人。

查询结果格式如下例所示。

 

示例:

输入:
RequestAccepted 表:
+--------------+-------------+-------------+
| 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  |
+--------------+-------------+-------------+
输出:
+----+-----+
| id | num |
+----+-----+
| 3  | 3   |
+----+-----+
解释:
编号为 3 的人是编号为 1 ,2 和 4 的人的好友,所以他总共有 3 个好友,比其他人都多。

 

进阶:在真实世界里,可能会有多个人拥有好友数相同且最多,你能找到所有这些人吗?

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
# Write your MySQL query statement below

pythondata 解法, 执行用时: 364 ms, 内存消耗: 60.4 MB, 提交时间: 2023-09-17 10:48:22

import pandas as pd

def most_friends(request_accepted: pd.DataFrame) -> pd.DataFrame:
    request = request_accepted.groupby("requester_id").size().reset_index(name="request").rename(columns={"requester_id": "id"})
    accept = request_accepted.groupby("accepter_id").size().reset_index(name="accept").rename(columns={"accepter_id": "id"})
    df = pd.merge(request, accept, on="id", how="outer").fillna(0)
    df["num"] = df["request"]+df["accept"]
    df = df[df["num"]==df["num"].max()]
    return df[["id","num"]]

def most_friends2(request_accepted: pd.DataFrame) -> pd.DataFrame:
    res = pd.concat((request_accepted[['requester_id']], request_accepted[['accepter_id']].rename(columns={'accepter_id': 'requester_id'})))
    res = res.groupby('requester_id').size().reset_index(name='num')
    return res.loc[[res['num'].idxmax()], :].rename(columns={'requester_id': 'id'})

mysql 解法, 执行用时: 163 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:20:21

# Write your MySQL query statement below
select id, count(*) as Num
from 
    (select requester_id id from RequestAccepted
    union all
    select accepter_id id from RequestAccepted) as t
group by id
order by count(*) desc
limit 1

mysql 解法, 执行用时: 163 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:19:46

# Write your MySQL query statement below
select rid as `id`,count(aid) as `num`
from
(
    select R1.requester_id as rid,R1.accepter_id as aid
    from RequestAccepted as R1
    UNION all
    select R2.accepter_id as rid,R2.requester_id as aid
    from RequestAccepted as R2
) as A
group by rid
order by num desc
limit 0,1

mysql 解法, 执行用时: 182 ms, 内存消耗: 0 B, 提交时间: 2023-04-02 12:18:51

# Write your MySQL query statement below
select ids as id, cnt as num
from
(
select ids, count(*) as cnt
   from
   (
        select requester_id as ids from RequestAccepted
        union all
        select accepter_id from RequestAccepted
    ) as tbl1
   group by ids
   ) as tbl2
order by cnt desc
limit 1
;

上一题