|
| 1 | +Here's a well-structured `README.md` file for **LeetCode 1148 - Article Views I** with SQL and Pandas solutions: |
| 2 | + |
| 3 | +```md |
| 4 | +# 📰 Article Views I - LeetCode 1148 |
| 5 | + |
| 6 | +## 📌 Problem Statement |
| 7 | +You are given the **Views** table that records article views. |
| 8 | + |
| 9 | +### Views Table |
| 10 | +| Column Name | Type | |
| 11 | +| ----------- | ---- | |
| 12 | +| article_id | int | |
| 13 | +| author_id | int | |
| 14 | +| viewer_id | int | |
| 15 | +| view_date | date | |
| 16 | + |
| 17 | +- The table **may contain duplicate rows**. |
| 18 | +- Each row indicates that **some viewer viewed an article** written by some author on a specific date. |
| 19 | +- If `author_id = viewer_id`, it means **the author viewed their own article**. |
| 20 | + |
| 21 | +### Task: |
| 22 | +Find **all authors** who have viewed **at least one of their own articles**. |
| 23 | +- **Return the result sorted by `id` in ascending order**. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 📊 Example 1: |
| 28 | +### Input: |
| 29 | +**Views Table** |
| 30 | +| article_id | author_id | viewer_id | view_date | |
| 31 | +| ---------- | --------- | --------- | ---------- | |
| 32 | +| 1 | 3 | 5 | 2019年08月01日 | |
| 33 | +| 1 | 3 | 6 | 2019年08月02日 | |
| 34 | +| 2 | 7 | 7 | 2019年08月01日 | |
| 35 | +| 2 | 7 | 6 | 2019年08月02日 | |
| 36 | +| 4 | 7 | 1 | 2019年07月22日 | |
| 37 | +| 3 | 4 | 4 | 2019年07月21日 | |
| 38 | +| 3 | 4 | 4 | 2019年07月21日 | |
| 39 | + |
| 40 | +### Output: |
| 41 | +| id | |
| 42 | +| --- | |
| 43 | +| 4 | |
| 44 | +| 7 | |
| 45 | + |
| 46 | +### Explanation: |
| 47 | +- **Author 4** viewed their own article (`viewer_id = author_id`). |
| 48 | +- **Author 7** viewed their own article (`viewer_id = author_id`). |
| 49 | +- The result is sorted in **ascending order**. |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 🖥 SQL Solutions |
| 54 | + |
| 55 | +### 1️⃣ Standard MySQL Solution |
| 56 | +#### Explanation: |
| 57 | +- **Filter rows** where `author_id = viewer_id`. |
| 58 | +- Use `DISTINCT` to **remove duplicates**. |
| 59 | +- **Sort the result** in ascending order. |
| 60 | + |
| 61 | +```sql |
| 62 | +SELECT DISTINCT author_id AS id |
| 63 | +FROM Views |
| 64 | +WHERE author_id = viewer_id |
| 65 | +ORDER BY id ASC; |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +### 2️⃣ Alternative Solution Using `GROUP BY` |
| 71 | +#### Explanation: |
| 72 | +- **Group by** `author_id` and **filter authors** who have viewed at least one of their own articles. |
| 73 | + |
| 74 | +```sql |
| 75 | +SELECT author_id AS id |
| 76 | +FROM Views |
| 77 | +WHERE author_id = viewer_id |
| 78 | +GROUP BY author_id |
| 79 | +ORDER BY id ASC; |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## 🐍 Pandas Solution (Python) |
| 85 | +#### Explanation: |
| 86 | +- **Filter rows** where `author_id == viewer_id`. |
| 87 | +- **Select distinct author IDs**. |
| 88 | +- **Sort the result** in ascending order. |
| 89 | + |
| 90 | +```python |
| 91 | +import pandas as pd |
| 92 | + |
| 93 | +def authors_who_viewed_own_articles(views: pd.DataFrame) -> pd.DataFrame: |
| 94 | + # Filter rows where author_id == viewer_id |
| 95 | + filtered = views[views["author_id"] == views["viewer_id"]] |
| 96 | + |
| 97 | + # Select unique author IDs and sort |
| 98 | + result = pd.DataFrame({"id": sorted(filtered["author_id"].unique())}) |
| 99 | + |
| 100 | + return result |
| 101 | +``` |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## 📁 File Structure |
| 106 | +``` |
| 107 | +📂 Article-Views-I |
| 108 | +│── 📜 README.md |
| 109 | +│── 📜 solution.sql |
| 110 | +│── 📜 solution_group_by.sql |
| 111 | +│── 📜 solution_pandas.py |
| 112 | +│── 📜 test_cases.sql |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## 🔗 Useful Links |
| 118 | +- 📖 [LeetCode Problem](https://leetcode.com/problems/article-views-i/) |
| 119 | +- 📚 [SQL DISTINCT vs GROUP BY](https://www.w3schools.com/sql/sql_distinct.asp) |
| 120 | +- 🐍 [Pandas Unique Function](https://pandas.pydata.org/docs/reference/api/pandas.Series.unique.html) |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +Let me know if you need any changes! 🚀 |
0 commit comments