Find Followers Count — LeetCode 1729 Python Solution
EasyDatabase
- Problem
- #1729
- Reading time
- 2 min
- Source
- leetcode.com
Table schema
SQL
Table: Followers +-------------+------+ | Column Name | Type | +-------------+------+ | user_id | int | | follower_id | int | +-------------+------+ (user_id, follower_id) is the primary key (combination of columns with unique values) for this table. This table contains the IDs of a user and a follower in a social media app where the follower follows the user.Example
SQL
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| follower_id | int |
+-------------+------+
(user_id, follower_id) is the primary key (combination of columns with unique values) for this table.
This table contains the IDs of a user and a follower in a social media app where the follower follows the user.Python solution
Python
import duckdb
import pandas as pd
def solution(followers: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Followers", followers)
return con.execute("""SELECT user_id, COUNT(1) AS followers_count
FROM Followers
GROUP BY 1
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 1729. Find Followers Count?
- LeetCode 1729. Find Followers Count is rated Easy on LeetCode.
- What topics does LeetCode 1729. Find Followers Count cover?
- LeetCode 1729. Find Followers Count is tagged Database on LeetCode.