← Home

183. Customers Who Never Order

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
183

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 183. Customers Who Never Order, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The solution is organized around the main LeetCode entry point and a few local helpers.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The code keeps the moving pieces local, so the main idea stays visible.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01# Runtime: 271 ms, faster than 39.92% of MySQL online submissions for Customers Who Never Order.
02# Memory Usage: N/A
03
04# Write your MySQL query statement below
05select Name as Customers from Customers left join Orders on Customers.Id=Orders.CustomerId where Orders.CustomerId is NULL
06
07/**
08Approach: Using sub-query and NOT IN clause [Accepted]
09Algorithm
10
11If we have a list of customers who have ever ordered, it will be easy to know who never ordered.
12
13We can use the following code to get such list.
14
15select customerid from orders;
16Then, we can use NOT IN to query the customers who are not in this list.
17**/
18
19# select Name as Customers from Customers where Customers.Id not in (select CustomerId from Orders)

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.