Skip to content

Commit 9e7cb82

Browse files
committed
Update readme.md
1 parent daf5cf6 commit 9e7cb82

File tree

1 file changed

+147
-0
lines changed
  • LeetCode SQL 50 Solution/585. Investments in 2016

1 file changed

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

0 commit comments

Comments
 (0)