Drop Type 1 Orders for Customers With Type 0 Orders — LeetCode 2084 Python Solution

MediumLeetCode PremiumDatabase
Problem
#2084
Reading time
3 min

Table schema

SQL
Table: Orders +-------------+------+ | Column Name | Type | +-------------+------+ | order_id | int | | customer_id | int | | order_type | int | +-------------+------+ order_id is the column with unique values for this table. Each row of this table indicates the ID of an order, the ID of the customer who ordered it, and the order type.

Example

SQL
+-------------+------+
| Column Name | Type |
+-------------+------+
| order_id    | int  | 
| customer_id | int  |
| order_type  | int  | 
+-------------+------+
order_id is the column with unique values for this table.
Each row of this table indicates the ID of an order, the ID of the customer who ordered it, and the order type.
The orders could be of type 0 or type 1.

Python solution

Python
import duckdb
import pandas as pd

def solution(orders: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Orders", orders)
    return con.execute("""WITH
    T AS (
        SELECT DISTINCT customer_id
        FROM Orders
        WHERE order_type = 0
    )
SELECT *
FROM Orders AS o
WHERE order_type = 0 OR NOT EXISTS (SELECT 1 FROM T AS t WHERE t.customer_id = o.customer_id);""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 2084. Drop Type 1 Orders for Customers With Type 0 Orders?
LeetCode 2084. Drop Type 1 Orders for Customers With Type 0 Orders is rated Medium on LeetCode.
What topics does LeetCode 2084. Drop Type 1 Orders for Customers With Type 0 Orders cover?
LeetCode 2084. Drop Type 1 Orders for Customers With Type 0 Orders is tagged Database on LeetCode.
Is LeetCode 2084. Drop Type 1 Orders for Customers With Type 0 Orders a premium problem?
Yes. LeetCode 2084. Drop Type 1 Orders for Customers With Type 0 Orders 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