Leetflex Banned Accounts — LeetCode 1747 Python Solution

MediumLeetCode PremiumDatabase
Problem
#1747
Reading time
3 min

Table schema

SQL
Table: LogInfo +-------------+----------+ | Column Name | Type | +-------------+----------+ | account_id | int | | ip_address | int | | login | datetime | | logout | datetime | +-------------+----------+ This table may contain duplicate rows. The table contains information about the login and logout dates of Leetflex accounts.

Example

SQL
+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| account_id  | int      |
| ip_address  | int      |
| login       | datetime |
| logout      | datetime |
+-------------+----------+
This table may contain duplicate rows.
The table contains information about the login and logout dates of Leetflex accounts. It also contains the IP address from which the account was logged in and out.
It is guaranteed that the logout time is after the login time.

Python solution

Python
import duckdb
import pandas as pd

def solution(log_info: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("LogInfo", log_info)
    return con.execute("""SELECT DISTINCT
    a.account_id
FROM
    LogInfo AS a
    JOIN LogInfo AS b
        ON a.account_id = b.account_id
        AND a.ip_address != b.ip_address
        AND a.login BETWEEN b.login AND b.logout;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1747. Leetflex Banned Accounts?
LeetCode 1747. Leetflex Banned Accounts is rated Medium on LeetCode.
What topics does LeetCode 1747. Leetflex Banned Accounts cover?
LeetCode 1747. Leetflex Banned Accounts is tagged Database on LeetCode.
Is LeetCode 1747. Leetflex Banned Accounts a premium problem?
Yes. LeetCode 1747. Leetflex Banned Accounts is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview