MediumNo. 1341
Movie Rating — LeetCode 1341 Python Solution
- Topics
- Database
- Reading time
- 5 min
- Source
- leetcode.com
Table schema
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
+---------------+---------+
| 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
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
- 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 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.