-
-
Notifications
You must be signed in to change notification settings - Fork 155
add problem-602 #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add problem-602 #1435
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
777cb0f
add problem-602
agarwalhimanshugaya c27f5a9
change as per suggestion
agarwalhimanshugaya a0bce4a
Update 602-Friend-Requests-||.md
ajay-dhangar 423fd20
Update and rename 602-Friend-Requests-||.md to 602-friend-requests-2.md
ajay-dhangar ab72ab3
Update 602-friend-requests-2.md
ajay-dhangar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
dsa-solutions/lc-solutions/0600-0699/602-Friend-Requests-||.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
--- | ||
id: friend-requests-II-who-has-the-most-friends | ||
title: Friend-Requests-II-Who-Has-the-Most-Friends | ||
sidebar_label: Friend Requests II: Who Has the Most Friends | ||
tags: | ||
- Sql | ||
- Database | ||
- Pandas | ||
description: "This question solve important question of sql which gives us knowledge about writing of sql command." | ||
--- | ||
|
||
# Friend Requests II: Who Has the Most Friends | ||
|
||
## 1. Problem Description | ||
``` | ||
+----------------+---------+ | ||
| Column Name | Type | | ||
+----------------+---------+ | ||
| requester_id | int | | ||
| accepter_id | int | | ||
| accept_date | date | | ||
+----------------+---------+ | ||
(requester_id, accepter_id) is the primary key (combination of columns with unique values) for this table. | ||
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. | ||
|
||
``` | ||
Write a solution to find the people who have the most friends and the most friends number. | ||
|
||
The test cases are generated so that only one person has the most friends. | ||
|
||
The result format is in the following example. | ||
## 2. Examples | ||
|
||
### Example 1: | ||
**Input:** | ||
``` | ||
RequestAccepted table: | ||
+--------------+-------------+-------------+ | ||
| 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 | | ||
+--------------+-------------+-------------+ | ||
|
||
``` | ||
**Output:** | ||
``` | ||
+----+-----+ | ||
| id | num | | ||
+----+-----+ | ||
| 3 | 3 | | ||
+----+-----+ | ||
|
||
``` | ||
**Explanation:** | ||
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. | ||
|
||
### Idea | ||
- Combine two columns into one list to find all people who either sent or received friend requests. | ||
``` | ||
SELECT → This command retrieves data from a database | ||
|
||
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 | ||
|
||
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 | ||
``` | ||
- Calculate the number of friends each person has. | ||
``` | ||
COUNT() → This function returns the number of rows | ||
|
||
Asterisk (*) → This symbol specifies that the query should return all columns of the queried tables | ||
``` | ||
- Group the results to show all records related to the same person. | ||
``` | ||
GROUP BY → This command groups rows that have the same values into summary rows, typically to perform aggregate functions on them | ||
``` | ||
- Arrange the results by the number of friends in descending order. | ||
``` | ||
ORDER BY → This command sorts the result-set in ascending (smallest to largest) order by default | ||
|
||
DESC → This keyword sorts the records in descending (largest to smallest) order | ||
``` | ||
- Show the person with the most friends. | ||
``` | ||
LIMIT → This clause specifies the number of records to return | ||
``` | ||
|
||
## 5. Implementation | ||
|
||
<Tabs> | ||
<TabItem value="Pandas" label="Pandas" default> | ||
```Pandas | ||
import pandas as pd | ||
|
||
def most_friends(request_accepted: pd.DataFrame) -> pd.DataFrame: | ||
|
||
ctr = Counter(chain(request_accepted.requester_id.to_list(), | ||
request_accepted. accepter_id.to_list())) | ||
|
||
mx= max(ctr, key = lambda x: ctr[x]) | ||
|
||
return pd.DataFrame({'id':[mx], 'num':[ctr[mx]]}) | ||
|
||
``` | ||
</TabItem> | ||
|
||
<TabItem value="SQL" label="SQL"> | ||
```SQL | ||
SELECT id, COUNT(*) AS num | ||
FROM ( | ||
SELECT requester_id AS id FROM RequestAccepted | ||
UNION ALL | ||
SELECT accepter_id FROM RequestAccepted | ||
) AS friends_count | ||
GROUP BY id | ||
ORDER BY num DESC | ||
LIMIT 1; | ||
``` | ||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
### Complexity Analysis | ||
**Time Complexity:** $O(n)$ | ||
|
||
|
||
**Space Complexity:** $O(u)$ where `N ~ len(request_accepted)` and `U ~ the count of unique ids`. | ||
|
||
## 10. References | ||
|
||
- [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/) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.