Skip to content

Commit 1386716

Browse files
authored
Merge pull request #77 from iamAntimPal/Branch-1
Branch 1
2 parents 2cbe8d3 + c329b6f commit 1386716

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pandas as pd
2+
3+
def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame:
4+
# Filter rows where referee_id is not equal to 2 or is null
5+
result = customer[(customer['referee_id'] != 2) | (customer['referee_id'].isnull())][['name']]
6+
return result
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# **584. Find Customer Referee**
2+
3+
## **Problem Statement**
4+
You are given a table `Customer` that stores customer details along with the ID of the customer who referred them.
5+
6+
### **Customer Table**
7+
```
8+
+-------------+---------+
9+
| Column Name | Type |
10+
+-------------+---------+
11+
| id | int |
12+
| name | varchar |
13+
| referee_id | int |
14+
+-------------+---------+
15+
```
16+
- `id` is the **primary key**.
17+
- Each row represents a customer with their `id`, `name`, and `referee_id`.
18+
- `referee_id` indicates the customer who referred them. It can be **NULL** if no one referred the customer.
19+
20+
### **Task:**
21+
Find the names of the customers who are **not referred** by the customer with `id = 2`.
22+
23+
---
24+
25+
## **Example 1:**
26+
27+
### **Input:**
28+
#### **Customer Table**
29+
```
30+
+----+------+------------+
31+
| id | name | referee_id |
32+
+----+------+------------+
33+
| 1 | Will | null |
34+
| 2 | Jane | null |
35+
| 3 | Alex | 2 |
36+
| 4 | Bill | null |
37+
| 5 | Zack | 1 |
38+
| 6 | Mark | 2 |
39+
+----+------+------------+
40+
```
41+
42+
### **Output:**
43+
```
44+
+------+
45+
| name |
46+
+------+
47+
| Will |
48+
| Jane |
49+
| Bill |
50+
| Zack |
51+
+------+
52+
```
53+
54+
### **Explanation:**
55+
- **Alex** (id = 3) and **Mark** (id = 6) are referred by the customer with `id = 2` and are excluded.
56+
- The remaining customers (**Will**, **Jane**, **Bill**, **Zack**) are not referred by the customer with `id = 2`.
57+
58+
---
59+
60+
## **Solution Approaches**
61+
62+
### **SQL Solution (Using WHERE Clause)**
63+
```sql
64+
SELECT name
65+
FROM Customer
66+
WHERE referee_id != 2 OR referee_id IS NULL;
67+
```
68+
**Explanation:**
69+
- The query selects customer names where `referee_id` is either not equal to `2` or is `NULL`.
70+
- This effectively filters out customers referred by the customer with `id = 2`.
71+
72+
---
73+
74+
### **Pandas Solution**
75+
```python
76+
import pandas as pd
77+
78+
def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame:
79+
# Filter rows where referee_id is not equal to 2 or is null
80+
result = customer[(customer['referee_id'] != 2) | (customer['referee_id'].isnull())][['name']]
81+
return result
82+
```
83+
**Explanation:**
84+
- The Pandas solution filters the DataFrame for rows where `referee_id` is not 2 or is `NaN`.
85+
- It then returns the `name` column containing the desired customer names.
86+
87+
---
88+
89+
## **File Structure**
90+
```
91+
LeetCode584/
92+
├── problem_statement.md # Contains the problem description and constraints.
93+
├── sql_solution.sql # Contains the SQL solution.
94+
├── pandas_solution.py # Contains the Pandas solution.
95+
├── README.md # Overview of the problem and solutions.
96+
```
97+
98+
---
99+
100+
## **Useful Links**
101+
- [LeetCode Problem 584](https://leetcode.com/problems/find-customer-referee/)
102+
- [SQL WHERE Clause Documentation](https://www.w3schools.com/sql/sql_where.asp)
103+
- [Pandas Filtering DataFrames](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html)
104+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pandas as pd
2+
3+
def investments_in_2016(insurance: pd.DataFrame) -> pd.DataFrame:
4+
# Count the number of occurrences for each tiv_2015 value
5+
insurance['tiv_2015_count'] = insurance.groupby('tiv_2015')['tiv_2015'].transform('count')
6+
7+
# Count the number of occurrences for each (lat, lon) pair
8+
insurance['city_count'] = insurance.groupby(['lat', 'lon'])['lat'].transform('count')
9+
10+
# Filter rows that meet both criteria:
11+
# 1. tiv_2015 appears more than once.
12+
# 2. The location (lat, lon) is unique (appears only once).
13+
valid_rows = insurance[(insurance['tiv_2015_count'] > 1) & (insurance['city_count'] == 1)]
14+
15+
# Calculate the sum of tiv_2016 and round to 2 decimal places
16+
total_tiv_2016 = round(valid_rows['tiv_2016'].sum(), 2)
17+
18+
# Return result as a DataFrame
19+
return pd.DataFrame({'tiv_2016': [total_tiv_2016]})
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
3+
# **585. Investments in 2016**
4+
5+
## **Problem Statement**
6+
You are given a table `Insurance` that contains details about policy investments.
7+
8+
### **Insurance Table**
9+
```
10+
+-------------+-------+
11+
| Column Name | Type |
12+
+-------------+-------+
13+
| pid | int |
14+
| tiv_2015 | float |
15+
| tiv_2016 | float |
16+
| lat | float |
17+
| lon | float |
18+
+-------------+-------+
19+
```
20+
- `pid` is the **primary key**.
21+
- `tiv_2015` is the total investment value in **2015**.
22+
- `tiv_2016` is the total investment value in **2016**.
23+
- `lat` and `lon` represent the **latitude** and **longitude** of the policyholder's city. Both values are guaranteed not to be `NULL`.
24+
25+
### **Task:**
26+
Report the **sum** of all `tiv_2016` values (rounded to two decimal places) for all policyholders who:
27+
1. Have the **same `tiv_2015`** value as one or more other policyholders.
28+
2. Are **not located** in the same city as any other policyholder (i.e., the `(lat, lon)` attribute pair is **unique**).
29+
30+
---
31+
32+
## **Example 1:**
33+
34+
### **Input:**
35+
#### **Insurance Table**
36+
```
37+
+-----+----------+----------+-----+-----+
38+
| pid | tiv_2015 | tiv_2016 | lat | lon |
39+
+-----+----------+----------+-----+-----+
40+
| 1 | 10 | 5 | 10 | 10 |
41+
| 2 | 20 | 20 | 20 | 20 |
42+
| 3 | 10 | 30 | 20 | 20 |
43+
| 4 | 10 | 40 | 40 | 40 |
44+
+-----+----------+----------+-----+-----+
45+
```
46+
47+
### **Output:**
48+
```
49+
+----------+
50+
| tiv_2016 |
51+
+----------+
52+
| 45.00 |
53+
+----------+
54+
```
55+
56+
### **Explanation:**
57+
- The policyholders with `tiv_2015 = 10` appear in multiple rows.
58+
- Among these, only the records with **unique locations** count.
59+
- Policy `pid = 1` and `pid = 4` meet both criteria, so the result is the sum of their `tiv_2016`: **5 + 40 = 45.00**.
60+
61+
---
62+
63+
## **Solution Approaches**
64+
65+
### **SQL Solution (Using Window Functions)**
66+
```sql
67+
WITH InsuranceWithCounts AS (
68+
SELECT
69+
tiv_2016,
70+
COUNT(*) OVER(PARTITION BY tiv_2015) AS tiv_2015_count,
71+
COUNT(*) OVER(PARTITION BY lat, lon) AS city_count
72+
FROM Insurance
73+
)
74+
SELECT ROUND(SUM(tiv_2016), 2) AS tiv_2016
75+
FROM InsuranceWithCounts
76+
WHERE tiv_2015_count > 1
77+
AND city_count = 1;
78+
```
79+
**Explanation:**
80+
- The CTE `InsuranceWithCounts` computes:
81+
- `tiv_2015_count`: Number of records with the same `tiv_2015`.
82+
- `city_count`: Number of records with the same `(lat, lon)` pair.
83+
- The outer query filters rows where:
84+
- `tiv_2015_count > 1` (i.e., policyholders share their 2015 investment value with others).
85+
- `city_count = 1` (i.e., their location is unique).
86+
- Finally, it sums `tiv_2016` and rounds the result to 2 decimal places.
87+
88+
---
89+
90+
### **Pandas Solution**
91+
```python
92+
import pandas as pd
93+
94+
def investments_in_2016(insurance: pd.DataFrame) -> pd.DataFrame:
95+
# Count the number of occurrences for each tiv_2015 value
96+
insurance['tiv_2015_count'] = insurance.groupby('tiv_2015')['tiv_2015'].transform('count')
97+
98+
# Count the number of occurrences for each (lat, lon) pair
99+
insurance['city_count'] = insurance.groupby(['lat', 'lon'])['lat'].transform('count')
100+
101+
# Filter rows that meet both criteria:
102+
# 1. tiv_2015 appears more than once.
103+
# 2. The location (lat, lon) is unique (appears only once).
104+
valid_rows = insurance[(insurance['tiv_2015_count'] > 1) & (insurance['city_count'] == 1)]
105+
106+
# Calculate the sum of tiv_2016 and round to 2 decimal places
107+
total_tiv_2016 = round(valid_rows['tiv_2016'].sum(), 2)
108+
109+
# Return result as a DataFrame
110+
return pd.DataFrame({'tiv_2016': [total_tiv_2016]})
111+
112+
# Example usage:
113+
# df = pd.read_csv('insurance.csv')
114+
# print(investments_in_2016(df))
115+
```
116+
**Explanation:**
117+
- The code computes two new columns:
118+
- `tiv_2015_count` for the number of policyholders with the same 2015 investment.
119+
- `city_count` for the count of policyholders in each unique city (using `(lat, lon)`).
120+
- Rows that meet the conditions are filtered.
121+
- The `tiv_2016` values of the valid rows are summed and rounded to 2 decimal places.
122+
- The result is returned as a DataFrame.
123+
124+
---
125+
126+
## **File Structure**
127+
```
128+
LeetCode585/
129+
├── problem_statement.md # Contains the problem description and constraints.
130+
├── sql_solution.sql # SQL solution using window functions.
131+
├── pandas_solution.py # Pandas solution for Python users.
132+
├── README.md # Overview of the problem and available solutions.
133+
```
134+
135+
---
136+
137+
## **Useful Links**
138+
- [LeetCode Problem 585](https://leetcode.com/problems/investments-in-2016/)
139+
- [SQL Window Functions](https://www.w3schools.com/sql/sql_window.asp)
140+
- [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
141+
- [Pandas Transform Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.transform.html)

0 commit comments

Comments
 (0)