Skip to content

Commit b6ddb0f

Browse files
committed
Update readme.md
1 parent c64843c commit b6ddb0f

File tree

1 file changed

+208
-0
lines changed
  • LeetCode SQL 50 Solution/1341. Movie Rating

1 file changed

+208
-0
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
Here's a well-structured `README.md` for **LeetCode 1341 - Movie Rating**, formatted for a GitHub repository:
2+
3+
```md
4+
# 🎬 Movie Rating - LeetCode 1341
5+
6+
## 📌 Problem Statement
7+
You are given three tables: **Movies**, **Users**, and **MovieRating**.
8+
9+
Your task is to:
10+
1. Find the **user who has rated the greatest number of movies**.
11+
- In case of a tie, return the **lexicographically smaller** name.
12+
2. Find the **movie with the highest average rating** in **February 2020**.
13+
- In case of a tie, return the **lexicographically smaller** movie title.
14+
15+
---
16+
17+
## 📊 Table Structure
18+
19+
### **Movies Table**
20+
| Column Name | Type |
21+
| ----------- | ------- |
22+
| movie_id | int |
23+
| title | varchar |
24+
25+
- `movie_id` is the **primary key** (unique identifier).
26+
- `title` is the **name of the movie**.
27+
28+
---
29+
30+
### **Users Table**
31+
| Column Name | Type |
32+
| ----------- | ------- |
33+
| user_id | int |
34+
| name | varchar |
35+
36+
- `user_id` is the **primary key** (unique identifier).
37+
- `name` is **unique** for each user.
38+
39+
---
40+
41+
### **MovieRating Table**
42+
| Column Name | Type |
43+
| ----------- | ---- |
44+
| movie_id | int |
45+
| user_id | int |
46+
| rating | int |
47+
| created_at | date |
48+
49+
- `(movie_id, user_id)` is the **primary key** (ensuring unique user-movie ratings).
50+
- `created_at` represents the **review date**.
51+
52+
---
53+
54+
## 🔢 Goal:
55+
- Return a **single-column result** containing:
56+
1. **User name** with the most ratings.
57+
2. **Movie title** with the highest **average rating** in **February 2020**.
58+
59+
---
60+
61+
## 📊 Example 1:
62+
### **Input:**
63+
#### **Movies Table**
64+
| movie_id | title |
65+
| -------- | -------- |
66+
| 1 | Avengers |
67+
| 2 | Frozen 2 |
68+
| 3 | Joker |
69+
70+
#### **Users Table**
71+
| user_id | name |
72+
| ------- | ------ |
73+
| 1 | Daniel |
74+
| 2 | Monica |
75+
| 3 | Maria |
76+
| 4 | James |
77+
78+
#### **MovieRating Table**
79+
| movie_id | user_id | rating | created_at |
80+
| -------- | ------- | ------ | ---------- |
81+
| 1 | 1 | 3 | 2020-01-12 |
82+
| 1 | 2 | 4 | 2020-02-11 |
83+
| 1 | 3 | 2 | 2020-02-12 |
84+
| 1 | 4 | 1 | 2020-01-01 |
85+
| 2 | 1 | 5 | 2020-02-17 |
86+
| 2 | 2 | 2 | 2020-02-01 |
87+
| 2 | 3 | 2 | 2020-03-01 |
88+
| 3 | 1 | 3 | 2020-02-22 |
89+
| 3 | 2 | 4 | 2020-02-25 |
90+
91+
### **Output:**
92+
| results |
93+
| -------- |
94+
| Daniel |
95+
| Frozen 2 |
96+
97+
### **Explanation:**
98+
- **Most Active User:**
99+
- `Daniel` and `Monica` both rated **3 movies**.
100+
- Since `Daniel` is **lexicographically smaller**, he is chosen.
101+
102+
- **Highest Average Movie Rating in February 2020:**
103+
- **Frozen 2**: `(5 + 2) / 2 = 3.5`
104+
- **Joker**: `(3 + 4) / 2 = 3.5`
105+
- Since **Frozen 2** is **lexicographically smaller**, it is chosen.
106+
107+
---
108+
109+
## 🖥 SQL Solution
110+
111+
### ✅ **Using `JOIN` + `GROUP BY` + `HAVING`**
112+
#### **Explanation:**
113+
1. **Find the most active user:**
114+
- Count the number of ratings per user.
115+
- Use `ORDER BY COUNT(*) DESC, name` to get the **user with the most ratings**, breaking ties lexicographically.
116+
- Limit the result to **1 user**.
117+
118+
2. **Find the highest-rated movie in February 2020:**
119+
- Filter rows where `created_at` is **in February 2020**.
120+
- **Calculate the average rating per movie**.
121+
- Use `ORDER BY AVG(rating) DESC, title` to get the **highest-rated movie**, breaking ties lexicographically.
122+
- Limit the result to **1 movie**.
123+
124+
```sql
125+
(
126+
SELECT name AS results
127+
FROM
128+
Users
129+
JOIN MovieRating USING (user_id)
130+
GROUP BY user_id
131+
ORDER BY COUNT(1) DESC, name
132+
LIMIT 1
133+
)
134+
UNION ALL
135+
(
136+
SELECT title
137+
FROM
138+
MovieRating
139+
JOIN Movies USING (movie_id)
140+
WHERE DATE_FORMAT(created_at, '%Y-%m') = '2020-02'
141+
GROUP BY movie_id
142+
ORDER BY AVG(rating) DESC, title
143+
LIMIT 1
144+
);
145+
```
146+
147+
---
148+
149+
## 🐍 Pandas Solution (Python)
150+
#### **Explanation:**
151+
1. **Find the user with the most ratings:**
152+
- Group by `user_id`, count the ratings.
153+
- Merge with `Users` table to get `name`.
154+
- Sort by **count descending**, then **lexicographically**.
155+
156+
2. **Find the highest-rated movie in February 2020:**
157+
- Filter only `created_at` **in February 2020**.
158+
- Group by `movie_id` and calculate **average rating**.
159+
- Merge with `Movies` to get `title`.
160+
- Sort by **rating descending**, then **lexicographically**.
161+
162+
```python
163+
import pandas as pd
164+
165+
def movie_rating(users: pd.DataFrame, movies: pd.DataFrame, movie_rating: pd.DataFrame) -> pd.DataFrame:
166+
# Most active user
167+
user_counts = movie_rating.groupby("user_id")["rating"].count().reset_index()
168+
most_active_user = user_counts.merge(users, on="user_id")
169+
most_active_user = most_active_user.sort_values(by=["rating", "name"], ascending=[False, True]).iloc[0]["name"]
170+
171+
# Highest-rated movie in February 2020
172+
movie_rating["created_at"] = pd.to_datetime(movie_rating["created_at"])
173+
feb_ratings = movie_rating[movie_rating["created_at"].dt.strftime('%Y-%m') == "2020-02"]
174+
175+
avg_ratings = feb_ratings.groupby("movie_id")["rating"].mean().reset_index()
176+
highest_rated_movie = avg_ratings.merge(movies, on="movie_id")
177+
highest_rated_movie = highest_rated_movie.sort_values(by=["rating", "title"], ascending=[False, True]).iloc[0]["title"]
178+
179+
return pd.DataFrame({"results": [most_active_user, highest_rated_movie]})
180+
```
181+
182+
---
183+
184+
## 📁 File Structure
185+
```
186+
📂 Movie-Rating
187+
│── 📜 README.md
188+
│── 📜 solution.sql
189+
│── 📜 solution_pandas.py
190+
│── 📜 test_cases.sql
191+
```
192+
193+
---
194+
195+
## 🔗 Useful Links
196+
- 📖 [LeetCode Problem](https://leetcode.com/problems/movie-rating/)
197+
- 📚 [SQL `GROUP BY` Clause](https://www.w3schools.com/sql/sql_groupby.asp)
198+
- 🐍 [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
199+
```
200+
201+
### Features of this `README.md`:
202+
✅ **Clear problem description with tables**
203+
✅ **Example with step-by-step explanation**
204+
✅ **SQL and Pandas solutions with detailed breakdowns**
205+
✅ **File structure for easy organization**
206+
✅ **Helpful references for further learning**
207+
208+
Would you like any changes or additions? 🚀

0 commit comments

Comments
 (0)