Active Users — LeetCode 1454 Python Solution
MediumLeetCode PremiumDatabase
- Problem
- #1454
- Reading time
- 6 min
- Source
- leetcode.com
Table schema
SQL
Table: Accounts +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+ id is the primary key (column with unique values) for this table. This table contains the account id and the user name of each account.Example
SQL
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id is the primary key (column with unique values) for this table.
This table contains the account id and the user name of each account.Python solution
Python
import duckdb
import pandas as pd
def solution(accounts: pd.DataFrame, logins: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Accounts", accounts)
con.register("Logins", logins)
return con.execute("""WITH
T AS (
SELECT DISTINCT *
FROM
Logins
JOIN Accounts USING (id)
),
P AS (
SELECT
*,
DATE_SUB(
login_date,
INTERVAL ROW_NUMBER() OVER (
PARTITION BY id
ORDER BY login_date
) DAY
) g
FROM T
)
SELECT DISTINCT id, name
FROM P
GROUP BY id, g
HAVING COUNT(*) >= 5
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 1454. Active Users?
- LeetCode 1454. Active Users is rated Medium on LeetCode.
- What topics does LeetCode 1454. Active Users cover?
- LeetCode 1454. Active Users is tagged Database on LeetCode.
- Is LeetCode 1454. Active Users a premium problem?
- Yes. LeetCode 1454. Active Users is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.