Article Views I — LeetCode 1148 Python Solution
EasyDatabase
- Problem
- #1148
- Reading time
- 2 min
- Source
- leetcode.com
Table schema
SQL
Table: Views +---------------+---------+ | Column Name | Type | +---------------+---------+ | article_id | int | | author_id | int | | viewer_id | int | | view_date | date | +---------------+---------+ There is no primary key (column with unique values) for this table, the table may have duplicate rows. Each row of this table indicates that some viewer viewed an article (written by some author) on some date.Example
SQL
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| article_id | int |
| author_id | int |
| viewer_id | int |
| view_date | date |
+---------------+---------+
There is no primary key (column with unique values) for this table, the table may have duplicate rows.
Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
Note that equal author_id and viewer_id indicate the same person.Python solution
Python
import duckdb
import pandas as pd
def solution(views: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Views", views)
return con.execute("""SELECT DISTINCT author_id AS id
FROM Views
WHERE author_id = viewer_id
ORDER BY 1;""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1148. Article Views I?
- LeetCode 1148. Article Views I is rated Easy on LeetCode.
- What topics does LeetCode 1148. Article Views I cover?
- LeetCode 1148. Article Views I is tagged Database on LeetCode.