Customers Who Bought All Products — LeetCode 1045 Python Solution
MediumDatabase
- Problem
- #1045
- Reading time
- 2 min
- Source
- leetcode.com
Table schema
SQL
Table: Customer +-------------+---------+ | Column Name | Type | +-------------+---------+ | customer_id | int | | product_key | int | +-------------+---------+ This table may contain duplicates rows. customer_id is not NULL.Example
SQL
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
This table may contain duplicates rows.
customer_id is not NULL.
product_key is a foreign key (reference column) to Product table.Python solution
Python
import duckdb
import pandas as pd
def solution(customer: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Customer", customer)
con.register("Product", product)
return con.execute("""SELECT customer_id
FROM Customer
GROUP BY 1
HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(1) FROM Product);""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1045. Customers Who Bought All Products?
- LeetCode 1045. Customers Who Bought All Products is rated Medium on LeetCode.
- What topics does LeetCode 1045. Customers Who Bought All Products cover?
- LeetCode 1045. Customers Who Bought All Products is tagged Database on LeetCode.