LeetCode 1045: Customers Who Bought All Products Solution

Master LeetCode problem 1045 (Customers Who Bought All Products), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1045. Customers Who Bought All Products

Medium

Problem Explanation

Explanation:

To solve this problem, we need to find the customer ids who bought all the products in the Product table. We can achieve this by counting the distinct products bought by each customer and comparing it with the total number of distinct products in the Product table.

Algorithm:

  1. Count the number of distinct products in the Product table.
  2. For each customer, count the number of distinct products they bought.
  3. If the count of distinct products bought by a customer is equal to the total number of distinct products, add that customer id to the result.

Time Complexity: O(n), where n is the total number of rows in the Customer table. Space Complexity: O(n), for storing the result.

:

Solution Code

# Write your Java solution here
SELECT customer_id
FROM Customer
GROUP BY customer_id
HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(DISTINCT product_key) FROM Product);

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1045 (Customers Who Bought All Products)?

This page provides optimized solutions for LeetCode problem 1045 (Customers Who Bought All Products) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1045 (Customers Who Bought All Products)?

The time complexity for LeetCode 1045 (Customers Who Bought All Products) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1045 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1045 in Java, C++, or Python.

Back to LeetCode Solutions