Movie Rating — LeetCode 1341 Python Solution
MediumDatabase
- Problem
- #1341
- Reading time
- 5 min
- Source
- leetcode.com
Table schema
SQL
Table: Movies +---------------+---------+ | Column Name | Type | +---------------+---------+ | movie_id | int | | title | varchar | +---------------+---------+ movie_id is the primary key (column with unique values) for this table. title is the name of the movie.Example
SQL
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| title | varchar |
+---------------+---------+
movie_id is the primary key (column with unique values) for this table.
title is the name of the movie.
Each movie has a unique title.Python solution
Python
import duckdb
import pandas as pd
def solution(movies: pd.DataFrame, users: pd.DataFrame, movie_rating: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Movies", movies)
con.register("Users", users)
con.register("MovieRating", movie_rating)
return con.execute("""(
SELECT name AS results
FROM
Users
JOIN MovieRating USING (user_id)
GROUP BY user_id
ORDER BY COUNT(1) DESC, name
LIMIT 1
)
UNION ALL
(
SELECT title
FROM
MovieRating
JOIN Movies USING (movie_id)
WHERE DATE_FORMAT(created_at, '%Y-%m') = '2020-02'
GROUP BY movie_id
ORDER BY AVG(rating) DESC, title
LIMIT 1
);""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1341. Movie Rating?
- LeetCode 1341. Movie Rating is rated Medium on LeetCode.
- What topics does LeetCode 1341. Movie Rating cover?
- LeetCode 1341. Movie Rating is tagged Database on LeetCode.