|
| 1 | +--- |
| 2 | +id: friend-requests-ii-who-has-the-most-friends |
| 3 | +title: Friend Requests II Who Has the Most Friends |
| 4 | +sidebar_label: Friend Requests II |
| 5 | +tags: [sql, database, pandas] |
| 6 | +description: "This question solve important question of sql which gives us knowledge about writing of sql command." |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. Problem Description |
| 10 | +``` |
| 11 | ++----------------+---------+ |
| 12 | +| Column Name | Type | |
| 13 | ++----------------+---------+ |
| 14 | +| requester_id | int | |
| 15 | +| accepter_id | int | |
| 16 | +| accept_date | date | |
| 17 | ++----------------+---------+ |
| 18 | +(requester_id, accepter_id) is the primary key (combination of columns with unique values) for this table. |
| 19 | +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. |
| 20 | +
|
| 21 | +``` |
| 22 | +Write a solution to find the people who have the most friends and the most friends number. |
| 23 | + |
| 24 | +The test cases are generated so that only one person has the most friends. |
| 25 | + |
| 26 | +The result format is in the following example. |
| 27 | +## 2. Examples |
| 28 | + |
| 29 | +### Example 1: |
| 30 | +**Input:** |
| 31 | +``` |
| 32 | +RequestAccepted table: |
| 33 | ++--------------+-------------+-------------+ |
| 34 | +| requester_id | accepter_id | accept_date | |
| 35 | ++--------------+-------------+-------------+ |
| 36 | +| 1 | 2 | 2016/06/03 | |
| 37 | +| 1 | 3 | 2016/06/08 | |
| 38 | +| 2 | 3 | 2016/06/08 | |
| 39 | +| 3 | 4 | 2016/06/09 | |
| 40 | ++--------------+-------------+-------------+ |
| 41 | +
|
| 42 | +``` |
| 43 | +**Output:** |
| 44 | +``` |
| 45 | ++----+-----+ |
| 46 | +| id | num | |
| 47 | ++----+-----+ |
| 48 | +| 3 | 3 | |
| 49 | ++----+-----+ |
| 50 | +
|
| 51 | +``` |
| 52 | +**Explanation:** |
| 53 | +The person with id 3 is a friend of people 1, 2, and 4, so he has three friends in total, which is the most number than any others. |
| 54 | + |
| 55 | +### Idea |
| 56 | +- Combine two columns into one list to find all people who either sent or received friend requests. |
| 57 | +``` |
| 58 | + SELECT → This command retrieves data from a database |
| 59 | +
|
| 60 | +AS → This command renames a column with an alias (temporary name). In most database languages, we can skip the AS keyword and get the same result |
| 61 | +
|
| 62 | +UNION ALL → This operator combines the result-set of two or more SELECT statements (every SELECT statement must have the same number/data type/order of columns). It returns all rows from the query and it does not remove duplicate rows between the various SELECT statements |
| 63 | +``` |
| 64 | +- Calculate the number of friends each person has. |
| 65 | +``` |
| 66 | +COUNT() → This function returns the number of rows |
| 67 | +
|
| 68 | +Asterisk (*) → This symbol specifies that the query should return all columns of the queried tables |
| 69 | +``` |
| 70 | +- Group the results to show all records related to the same person. |
| 71 | +``` |
| 72 | +GROUP BY → This command groups rows that have the same values into summary rows, typically to perform aggregate functions on them |
| 73 | +``` |
| 74 | +- Arrange the results by the number of friends in descending order. |
| 75 | +``` |
| 76 | +ORDER BY → This command sorts the result-set in ascending (smallest to largest) order by default |
| 77 | +
|
| 78 | +DESC → This keyword sorts the records in descending (largest to smallest) order |
| 79 | +``` |
| 80 | +- Show the person with the most friends. |
| 81 | +``` |
| 82 | +LIMIT → This clause specifies the number of records to return |
| 83 | +``` |
| 84 | + |
| 85 | +## 5. Implementation |
| 86 | + |
| 87 | +<Tabs> |
| 88 | + <TabItem value="Pandas" label="Pandas" default> |
| 89 | + ```Pandas |
| 90 | + import pandas as pd |
| 91 | +
|
| 92 | +def most_friends(request_accepted: pd.DataFrame) -> pd.DataFrame: |
| 93 | +
|
| 94 | + ctr = Counter(chain(request_accepted.requester_id.to_list(), |
| 95 | + request_accepted. accepter_id.to_list())) |
| 96 | + |
| 97 | + mx= max(ctr, key = lambda x: ctr[x]) |
| 98 | + |
| 99 | + return pd.DataFrame({'id':[mx], 'num':[ctr[mx]]}) |
| 100 | +
|
| 101 | + ``` |
| 102 | + </TabItem> |
| 103 | + |
| 104 | + <TabItem value="SQL" label="SQL"> |
| 105 | + ```SQL |
| 106 | + SELECT id, COUNT(*) AS num |
| 107 | +FROM ( |
| 108 | + SELECT requester_id AS id FROM RequestAccepted |
| 109 | + UNION ALL |
| 110 | + SELECT accepter_id FROM RequestAccepted |
| 111 | +) AS friends_count |
| 112 | +GROUP BY id |
| 113 | +ORDER BY num DESC |
| 114 | +LIMIT 1; |
| 115 | + ``` |
| 116 | + </TabItem> |
| 117 | + |
| 118 | +</Tabs> |
| 119 | + |
| 120 | +### Complexity Analysis |
| 121 | +**Time Complexity:** $O(n)$ |
| 122 | + |
| 123 | + |
| 124 | +**Space Complexity:** $O(u)$ where `N ~ len(request_accepted)` and `U ~ the count of unique ids`. |
| 125 | + |
| 126 | +## 10. References |
| 127 | + |
| 128 | +- [LeetCode - Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/solutions/3803211/union-all-is-what-you-need-don-t-overcomplicate/) |
| 129 | + |
| 130 | + |
0 commit comments