Page Recommendations — LeetCode 1264 Python Solution
MediumLeetCode PremiumDatabase
- Problem
- #1264
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
SQL
Table: Friendship +---------------+---------+ | Column Name | Type | +---------------+---------+ | user1_id | int | | user2_id | int | +---------------+---------+ (user1_id, user2_id) is the primary key (combination of columns with unique values) for this table. Each row of this table indicates that there is a friendship relation between user1_id and user2_id.Example
SQL
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user1_id | int |
| user2_id | int |
+---------------+---------+
(user1_id, user2_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that there is a friendship relation between user1_id and user2_id.Python solution
Python
import duckdb
import pandas as pd
def solution(friendship: pd.DataFrame, likes: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Friendship", friendship)
con.register("Likes", likes)
return con.execute("""WITH
T AS (
SELECT user1_id AS user_id FROM Friendship WHERE user2_id = 1
UNION
SELECT user2_id AS user_id FROM Friendship WHERE user1_id = 1
)
SELECT DISTINCT page_id AS recommended_page
FROM
T
JOIN Likes USING (user_id)
WHERE page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = 1);""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1264. Page Recommendations?
- LeetCode 1264. Page Recommendations is rated Medium on LeetCode.
- What topics does LeetCode 1264. Page Recommendations cover?
- LeetCode 1264. Page Recommendations is tagged Database on LeetCode.
- Is LeetCode 1264. Page Recommendations a premium problem?
- Yes. LeetCode 1264. Page Recommendations is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.