Find Customer Referee — LeetCode 584 Python Solution
EasyDatabase
- Problem
- #584
- Reading time
- 2 min
- Source
- leetcode.com
The problem
List the names of the customers who were not referred by the customer with id 2, which includes everyone who was not referred by anybody. The Customer table has id (int, primary key), name (varchar) and referee_id (int), the id of the customer who referred them, or null when nobody did. Return the names in a column called name.
Example
- Input
- Customer(id, name, referee_id) = (1, 'Ana', null), (2, 'Ben', null), (3, 'Cleo', 2), (4, 'Dev', 1), (5, 'Eve', 2)
- Output
- name = 'Ana', 'Ben', 'Dev'
- Explanation
- Only Cleo and Eve carry referee_id 2; the rows with a null referee_id are kept because "not referred by 2" includes "not referred at all".
Python solution
Python
import pandas as pd
def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame:
res = customer[(customer['referee_id'].isna()) | (customer['referee_id'] != 2)]
return res[['name']]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 584. Find Customer Referee?
- LeetCode 584. Find Customer Referee is rated Easy on LeetCode.
- What topics does LeetCode 584. Find Customer Referee cover?
- LeetCode 584. Find Customer Referee is tagged Database on LeetCode.