Skip to content

Commit fdd4633

Browse files
committed
Update readme.md
1 parent ebec9dc commit fdd4633

File tree

1 file changed

+111
-0
lines changed
  • LeetCode SQL 50 Solution/584. Find Customer Referee

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Below is a structured README.md snippet for **LeetCode 584: Find Customer Referee**:
2+
3+
---
4+
5+
# **584. Find Customer Referee**
6+
7+
## **Problem Statement**
8+
You are given a table `Customer` that stores customer details along with the ID of the customer who referred them.
9+
10+
### **Customer Table**
11+
```
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| id | int |
16+
| name | varchar |
17+
| referee_id | int |
18+
+-------------+---------+
19+
```
20+
- `id` is the **primary key**.
21+
- Each row represents a customer with their `id`, `name`, and `referee_id`.
22+
- `referee_id` indicates the customer who referred them. It can be **NULL** if no one referred the customer.
23+
24+
### **Task:**
25+
Find the names of the customers who are **not referred** by the customer with `id = 2`.
26+
27+
---
28+
29+
## **Example 1:**
30+
31+
### **Input:**
32+
#### **Customer Table**
33+
```
34+
+----+------+------------+
35+
| id | name | referee_id |
36+
+----+------+------------+
37+
| 1 | Will | null |
38+
| 2 | Jane | null |
39+
| 3 | Alex | 2 |
40+
| 4 | Bill | null |
41+
| 5 | Zack | 1 |
42+
| 6 | Mark | 2 |
43+
+----+------+------------+
44+
```
45+
46+
### **Output:**
47+
```
48+
+------+
49+
| name |
50+
+------+
51+
| Will |
52+
| Jane |
53+
| Bill |
54+
| Zack |
55+
+------+
56+
```
57+
58+
### **Explanation:**
59+
- **Alex** (id = 3) and **Mark** (id = 6) are referred by the customer with `id = 2` and are excluded.
60+
- The remaining customers (**Will**, **Jane**, **Bill**, **Zack**) are not referred by the customer with `id = 2`.
61+
62+
---
63+
64+
## **Solution Approaches**
65+
66+
### **SQL Solution (Using WHERE Clause)**
67+
```sql
68+
SELECT name
69+
FROM Customer
70+
WHERE referee_id != 2 OR referee_id IS NULL;
71+
```
72+
**Explanation:**
73+
- The query selects customer names where `referee_id` is either not equal to `2` or is `NULL`.
74+
- This effectively filters out customers referred by the customer with `id = 2`.
75+
76+
---
77+
78+
### **Pandas Solution**
79+
```python
80+
import pandas as pd
81+
82+
def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame:
83+
# Filter rows where referee_id is not equal to 2 or is null
84+
result = customer[(customer['referee_id'] != 2) | (customer['referee_id'].isnull())][['name']]
85+
return result
86+
```
87+
**Explanation:**
88+
- The Pandas solution filters the DataFrame for rows where `referee_id` is not 2 or is `NaN`.
89+
- It then returns the `name` column containing the desired customer names.
90+
91+
---
92+
93+
## **File Structure**
94+
```
95+
LeetCode584/
96+
├── problem_statement.md # Contains the problem description and constraints.
97+
├── sql_solution.sql # Contains the SQL solution.
98+
├── pandas_solution.py # Contains the Pandas solution.
99+
├── README.md # Overview of the problem and solutions.
100+
```
101+
102+
---
103+
104+
## **Useful Links**
105+
- [LeetCode Problem 584](https://leetcode.com/problems/find-customer-referee/)
106+
- [SQL WHERE Clause Documentation](https://www.w3schools.com/sql/sql_where.asp)
107+
- [Pandas Filtering DataFrames](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html)
108+
109+
---
110+
111+
This structured format provides a clear problem overview, multiple solution approaches, file organization, and useful references to help contributors understand and work on the problem. Happy coding! 🚀

0 commit comments

Comments
 (0)