Skip to content

Commit 488b49f

Browse files
committed
task: #602
1 parent 2202090 commit 488b49f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Have a good contributing!
5757
2. [Medium](./leetcode/medium/)
5858
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
5959
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
60+
- [602. Friend Requests II: Who Has the Most Friends](./leetcode/medium/602.%20Friend%20Requests%20II:%20Who%20Has%20the%20Most%20Friends.sql)
6061
- [1045. Customers Who Bought All Products](./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql)
6162
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)
6263
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Question 602. Friend Requests II: Who Has the Most Friends
3+
Link: https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/description/
4+
5+
Table: RequestAccepted
6+
7+
+----------------+---------+
8+
| Column Name | Type |
9+
+----------------+---------+
10+
| requester_id | int |
11+
| accepter_id | int |
12+
| accept_date | date |
13+
+----------------+---------+
14+
(requester_id, accepter_id) is the primary key (combination of columns with unique values) for this table.
15+
This table contains the ID of the user who sent the request, the ID of the user who received the request, and the date when the request was accepted.
16+
17+
18+
Write a solution to find the people who have the most friends and the most friends number.
19+
20+
The test cases are generated so that only one person has the most friends.
21+
*/
22+
23+
WITH id_table AS (
24+
SELECT requester_id AS id
25+
FROM RequestAccepted
26+
UNION ALL
27+
SELECT accepter_id AS id
28+
FROM RequestAccepted
29+
)
30+
31+
SELECT
32+
id,
33+
COUNT(id) AS num
34+
FROM id_table
35+
GROUP BY id
36+
ORDER BY COUNT(id) DESC
37+
LIMIT 1

0 commit comments

Comments
 (0)