Skip to content

Commit 70f09ef

Browse files
committed
Update readme.md
1 parent 1f3f531 commit 70f09ef

File tree

1 file changed

+129
-0
lines changed
  • LeetCode SQL 50 Solution/602. Friend Requests II

1 file changed

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

0 commit comments

Comments
 (0)