#607

Sales Person

Easy
DatabaseHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

We can optimize by using a set to track salespersons who have orders, allowing us to quickly check which salespersons did not make any sales.

⚙️

Algorithm

3 steps
  1. 1Step 1: Retrieve all sales_ids from the Orders table and store them in a set.
  2. 2Step 2: Retrieve all salespersons from the SalesPerson table.
  3. 3Step 3: For each salesperson, check if their sales_id is in the set. If not, add their name to the result.
solution.py3 lines
1# Full working Python code
2WITH SalesWithOrders AS (SELECT DISTINCT sales_id FROM Orders)
3SELECT name FROM SalesPerson WHERE sales_id NOT IN (SELECT sales_id FROM SalesWithOrders);

Complexity note: This complexity is due to the single pass to create the set of sales_ids and another pass to check against it, making it linear.

  • 1Using sets can significantly reduce the time complexity for membership checks.
  • 2Understanding how to leverage SQL's capabilities, like WITH clauses, can simplify complex queries.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.