|
| 1 | +# **602. Friend Requests II: Who Has the Most Friends** |
| 2 | + |
| 3 | +## **Problem Statement** |
| 4 | +You are given a table `RequestAccepted` that records friend request acceptances between users. |
| 5 | + |
| 6 | +### **RequestAccepted Table** |
| 7 | +``` |
| 8 | ++--------------+-------------+-------------+ |
| 9 | +| Column Name | Type | |
| 10 | ++--------------+-------------+ |
| 11 | +| requester_id | int | |
| 12 | +| accepter_id | int | |
| 13 | +| accept_date | date | |
| 14 | ++--------------+-------------+ |
| 15 | +``` |
| 16 | +- **(requester_id, accepter_id)** is the **primary key**. |
| 17 | +- Each row indicates the user who sent a friend request (`requester_id`), the user who accepted it (`accepter_id`), and the date when the request was accepted. |
| 18 | + |
| 19 | +### **Task:** |
| 20 | +Find the person who has the **most friends** along with the number of friends they have. |
| 21 | +*Note:* The test cases are generated so that only one person has the most friends. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## **Example 1:** |
| 26 | + |
| 27 | +### **Input:** |
| 28 | +#### **RequestAccepted Table** |
| 29 | +``` |
| 30 | ++--------------+-------------+-------------+ |
| 31 | +| requester_id | accepter_id | accept_date | |
| 32 | ++--------------+-------------+-------------+ |
| 33 | +| 1 | 2 | 2016/06/03 | |
| 34 | +| 1 | 3 | 2016/06/08 | |
| 35 | +| 2 | 3 | 2016/06/08 | |
| 36 | +| 3 | 4 | 2016/06/09 | |
| 37 | ++--------------+-------------+-------------+ |
| 38 | +``` |
| 39 | + |
| 40 | +### **Output:** |
| 41 | +``` |
| 42 | ++----+-----+ |
| 43 | +| id | num | |
| 44 | ++----+-----+ |
| 45 | +| 3 | 3 | |
| 46 | ++----+-----+ |
| 47 | +``` |
| 48 | + |
| 49 | +### **Explanation:** |
| 50 | +- User with `id = 3` is friends with users 1, 2, and 4, making a total of **3 friends**—the highest among all users. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## **Solution Approaches** |
| 55 | + |
| 56 | +### **SQL Solution (Using UNION ALL)** |
| 57 | +```sql |
| 58 | +WITH T AS ( |
| 59 | + SELECT requester_id, accepter_id FROM RequestAccepted |
| 60 | + UNION ALL |
| 61 | + SELECT accepter_id, requester_id FROM RequestAccepted |
| 62 | +) |
| 63 | +SELECT requester_id AS id, COUNT(*) AS num |
| 64 | +FROM T |
| 65 | +GROUP BY requester_id |
| 66 | +ORDER BY num DESC |
| 67 | +LIMIT 1; |
| 68 | +``` |
| 69 | +**Explanation:** |
| 70 | +- The CTE `T` creates a complete friendship list by combining both directions of the friend relationship. |
| 71 | +- The outer query groups by `requester_id` (which now represents a user) and counts the number of occurrences (i.e., friends). |
| 72 | +- The result is ordered by the friend count in descending order and limited to one row, returning the user with the most friends. |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### **Pandas Solution** |
| 77 | +```python |
| 78 | +import pandas as pd |
| 79 | + |
| 80 | +def most_friends(requests: pd.DataFrame) -> pd.DataFrame: |
| 81 | + # Create a DataFrame that contains all friend relationships in both directions |
| 82 | + friend_df = pd.concat([ |
| 83 | + requests[['requester_id', 'accepter_id']].rename(columns={'requester_id': 'id', 'accepter_id': 'friend'}), |
| 84 | + requests[['accepter_id', 'requester_id']].rename(columns={'accepter_id': 'id', 'requester_id': 'friend'}) |
| 85 | + ]) |
| 86 | + |
| 87 | + # Count number of friends for each user |
| 88 | + friend_counts = friend_df.groupby('id').size().reset_index(name='num') |
| 89 | + |
| 90 | + # Get the user with the most friends |
| 91 | + max_friends = friend_counts.loc[friend_counts['num'].idxmax()] |
| 92 | + |
| 93 | + return pd.DataFrame({'id': [max_friends['id']], 'num': [max_friends['num']]}) |
| 94 | + |
| 95 | +# Example usage: |
| 96 | +# requests_df = pd.read_csv('request_accepted.csv') |
| 97 | +# print(most_friends(requests_df)) |
| 98 | +``` |
| 99 | +**Explanation:** |
| 100 | +- The solution concatenates two DataFrames to consider friend relationships in both directions. |
| 101 | +- It then groups by user `id` and counts the number of friends. |
| 102 | +- The user with the maximum friend count is selected and returned. |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +## **File Structure** |
| 107 | +``` |
| 108 | +LeetCode602/ |
| 109 | +├── problem_statement.md # Contains the problem description and constraints. |
| 110 | +├── sql_solution.sql # Contains the SQL solution using UNION ALL. |
| 111 | +├── pandas_solution.py # Contains the Pandas solution for Python users. |
| 112 | +├── README.md # Overview of the problem and available solutions. |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## **Useful Links** |
| 118 | +- [LeetCode Problem 602](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/) |
| 119 | +- [SQL UNION ALL Documentation](https://www.w3schools.com/sql/sql_union.asp) |
| 120 | +- [Pandas concat Documentation](https://pandas.pydata.org/docs/reference/api/pandas.concat.html) |
| 121 | +- [Pandas groupby Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html) |
0 commit comments