MediumNo. 1045
Customers Who Bought All Products — LeetCode 1045 Python Solution
- Topics
- Database
- Reading time
- 2 min
- Source
- leetcode.com
Table schema
Table: Customer +-------------+---------+ | Column Name | Type | +-------------+---------+ | customer_id | int | | product_key | int | +-------------+---------+ This table may contain duplicates rows. customer_id is not NULL.Example
+-------------+---------+
| 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
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
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1050 | Actors and Directors Who Cooperated At Least Three Times | Easy |
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
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.