MediumNo. 1747LeetCode Premium

Leetflex Banned Accounts — LeetCode 1747 Python Solution

Topics
Database
Reading time
3 min

Table schema

SQLTable schema
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

SQLExample
+-------------+----------+
| 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

PythonSolution
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

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

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.

Free to download. Check it runs on your machine, then pick a plan.