HardNo. 1919LeetCode Premium
Leetcodify Similar Friends — LeetCode 1919 Python Solution
- Topics
- Database
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
Table: Listens +-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | song_id | int | | day | date | +-------------+---------+ This table may contain duplicate rows. Each row of this table indicates that the user user_id listened to the song song_id on the day day.Example
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| song_id | int |
| day | date |
+-------------+---------+
This table may contain duplicate rows.
Each row of this table indicates that the user user_id listened to the song song_id on the day day.Python solution
import duckdb
import pandas as pd
def solution(listens: pd.DataFrame, friendship: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Listens", listens)
con.register("Friendship", friendship)
return con.execute("""SELECT DISTINCT user1_id, user2_id
FROM
Friendship AS f
LEFT JOIN Listens AS l1 ON user1_id = l1.user_id
LEFT JOIN Listens AS l2 ON user2_id = l2.user_id
WHERE l1.song_id = l2.song_id AND l1.day = l2.day
GROUP BY 1, 2, l1.day
HAVING COUNT(DISTINCT l1.song_id) >= 3;""").df()Complexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
Frequently asked questions
- How hard is LeetCode 1919. Leetcodify Similar Friends?
LeetCode 1919. Leetcodify Similar Friends is rated Hard on LeetCode.
- What topics does LeetCode 1919. Leetcodify Similar Friends cover?
LeetCode 1919. Leetcodify Similar Friends is tagged Database on LeetCode.
- Is LeetCode 1919. Leetcodify Similar Friends a premium problem?
Yes. LeetCode 1919. Leetcodify Similar Friends is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.