MediumNo. 2084LeetCode Premium

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

Topics
Database
Reading time
3 min

Table schema

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

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

PythonSolution
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

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 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.

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