Skip to content

Commit c692eef

Browse files
authored
feat: add solutions to lc problem: No.3601 (#4543)
No.3601.Find Drivers with Improved Fuel Efficiency
1 parent 280a4b6 commit c692eef

File tree

4 files changed

+202
-2
lines changed

4 files changed

+202
-2
lines changed

solution/3600-3699/3601.Find Drivers with Improved Fuel Efficiency/README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,82 @@ Each row represents a trip made by a driver, including the distance traveled and
152152

153153
<!-- solution:start -->
154154

155-
### 方法一
155+
### 方法一:分组统计 + 连接查询
156+
157+
我们先对 `trips` 表进行分组统计,计算每个司机在上半年和下半年的平均燃油效率。然后通过连接查询将结果与 `drivers` 表关联,筛选出燃油效率有提升的司机,并计算提升的幅度。
156158

157159
<!-- tabs:start -->
158160

159161
#### MySQL
160162

161163
```sql
164+
# Write your MySQL query statement below
165+
WITH
166+
T AS (
167+
SELECT
168+
driver_id,
169+
AVG(distance_km / fuel_consumed) half_avg,
170+
CASE
171+
WHEN MONTH(trip_date) <= 6 THEN 1
172+
ELSE 2
173+
END half
174+
FROM trips
175+
GROUP BY driver_id, half
176+
)
177+
SELECT
178+
t1.driver_id,
179+
d.driver_name,
180+
ROUND(t1.half_avg, 2) first_half_avg,
181+
ROUND(t2.half_avg, 2) second_half_avg,
182+
ROUND(t2.half_avg - t1.half_avg, 2) efficiency_improvement
183+
FROM
184+
T t1
185+
JOIN T t2 ON t1.driver_id = t2.driver_id AND t1.half < t2.half AND t1.half_avg < t2.half_avg
186+
JOIN drivers d ON t1.driver_id = d.driver_id
187+
ORDER BY efficiency_improvement DESC, d.driver_name;
188+
```
162189

190+
#### Pandas
191+
192+
```python
193+
import pandas as pd
194+
195+
196+
def find_improved_efficiency_drivers(
197+
drivers: pd.DataFrame, trips: pd.DataFrame
198+
) -> pd.DataFrame:
199+
trips = trips.copy()
200+
trips["trip_date"] = pd.to_datetime(trips["trip_date"])
201+
trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
202+
trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
203+
half_avg = (
204+
trips.groupby(["driver_id", "half"])["efficiency"]
205+
.mean()
206+
.reset_index(name="half_avg")
207+
)
208+
pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
209+
columns={1: "first_half_avg", 2: "second_half_avg"}
210+
)
211+
pivot = pivot.dropna()
212+
pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
213+
pivot["efficiency_improvement"] = (
214+
pivot["second_half_avg"] - pivot["first_half_avg"]
215+
).round(2)
216+
pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
217+
pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
218+
result = pivot.reset_index().merge(drivers, on="driver_id")
219+
result = result.sort_values(
220+
by=["efficiency_improvement", "driver_name"], ascending=[False, True]
221+
)
222+
return result[
223+
[
224+
"driver_id",
225+
"driver_name",
226+
"first_half_avg",
227+
"second_half_avg",
228+
"efficiency_improvement",
229+
]
230+
]
163231
```
164232

165233
<!-- tabs:end -->

solution/3600-3699/3601.Find Drivers with Improved Fuel Efficiency/README_EN.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,84 @@ Each row represents a trip made by a driver, including the distance traveled and
152152

153153
<!-- solution:start -->
154154

155-
### Solution 1
155+
### Solution 1: Group Aggregation + Join Query
156+
157+
First, we perform group aggregation on the `trips` table to calculate the average fuel efficiency for each driver in the first half and the second half of the year.
158+
159+
Then, we join the results with the `drivers` table, filter out the drivers whose fuel efficiency has improved, and calculate the amount of improvement.
156160

157161
<!-- tabs:start -->
158162

159163
#### MySQL
160164

161165
```sql
166+
# Write your MySQL query statement below
167+
WITH
168+
T AS (
169+
SELECT
170+
driver_id,
171+
AVG(distance_km / fuel_consumed) half_avg,
172+
CASE
173+
WHEN MONTH(trip_date) <= 6 THEN 1
174+
ELSE 2
175+
END half
176+
FROM trips
177+
GROUP BY driver_id, half
178+
)
179+
SELECT
180+
t1.driver_id,
181+
d.driver_name,
182+
ROUND(t1.half_avg, 2) first_half_avg,
183+
ROUND(t2.half_avg, 2) second_half_avg,
184+
ROUND(t2.half_avg - t1.half_avg, 2) efficiency_improvement
185+
FROM
186+
T t1
187+
JOIN T t2 ON t1.driver_id = t2.driver_id AND t1.half < t2.half AND t1.half_avg < t2.half_avg
188+
JOIN drivers d ON t1.driver_id = d.driver_id
189+
ORDER BY efficiency_improvement DESC, d.driver_name;
190+
```
162191

192+
#### Pandas
193+
194+
```python
195+
import pandas as pd
196+
197+
198+
def find_improved_efficiency_drivers(
199+
drivers: pd.DataFrame, trips: pd.DataFrame
200+
) -> pd.DataFrame:
201+
trips = trips.copy()
202+
trips["trip_date"] = pd.to_datetime(trips["trip_date"])
203+
trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
204+
trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
205+
half_avg = (
206+
trips.groupby(["driver_id", "half"])["efficiency"]
207+
.mean()
208+
.reset_index(name="half_avg")
209+
)
210+
pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
211+
columns={1: "first_half_avg", 2: "second_half_avg"}
212+
)
213+
pivot = pivot.dropna()
214+
pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
215+
pivot["efficiency_improvement"] = (
216+
pivot["second_half_avg"] - pivot["first_half_avg"]
217+
).round(2)
218+
pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
219+
pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
220+
result = pivot.reset_index().merge(drivers, on="driver_id")
221+
result = result.sort_values(
222+
by=["efficiency_improvement", "driver_name"], ascending=[False, True]
223+
)
224+
return result[
225+
[
226+
"driver_id",
227+
"driver_name",
228+
"first_half_avg",
229+
"second_half_avg",
230+
"efficiency_improvement",
231+
]
232+
]
163233
```
164234

165235
<!-- tabs:end -->
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pandas as pd
2+
3+
4+
def find_improved_efficiency_drivers(
5+
drivers: pd.DataFrame, trips: pd.DataFrame
6+
) -> pd.DataFrame:
7+
trips = trips.copy()
8+
trips["trip_date"] = pd.to_datetime(trips["trip_date"])
9+
trips["half"] = trips["trip_date"].dt.month.apply(lambda m: 1 if m <= 6 else 2)
10+
trips["efficiency"] = trips["distance_km"] / trips["fuel_consumed"]
11+
half_avg = (
12+
trips.groupby(["driver_id", "half"])["efficiency"]
13+
.mean()
14+
.reset_index(name="half_avg")
15+
)
16+
pivot = half_avg.pivot(index="driver_id", columns="half", values="half_avg").rename(
17+
columns={1: "first_half_avg", 2: "second_half_avg"}
18+
)
19+
pivot = pivot.dropna()
20+
pivot = pivot[pivot["second_half_avg"] > pivot["first_half_avg"]]
21+
pivot["efficiency_improvement"] = (
22+
pivot["second_half_avg"] - pivot["first_half_avg"]
23+
).round(2)
24+
pivot["first_half_avg"] = pivot["first_half_avg"].round(2)
25+
pivot["second_half_avg"] = pivot["second_half_avg"].round(2)
26+
result = pivot.reset_index().merge(drivers, on="driver_id")
27+
result = result.sort_values(
28+
by=["efficiency_improvement", "driver_name"], ascending=[False, True]
29+
)
30+
return result[
31+
[
32+
"driver_id",
33+
"driver_name",
34+
"first_half_avg",
35+
"second_half_avg",
36+
"efficiency_improvement",
37+
]
38+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
driver_id,
6+
AVG(distance_km / fuel_consumed) half_avg,
7+
CASE
8+
WHEN MONTH(trip_date) <= 6 THEN 1
9+
ELSE 2
10+
END half
11+
FROM trips
12+
GROUP BY driver_id, half
13+
)
14+
SELECT
15+
t1.driver_id,
16+
d.driver_name,
17+
ROUND(t1.half_avg, 2) first_half_avg,
18+
ROUND(t2.half_avg, 2) second_half_avg,
19+
ROUND(t2.half_avg - t1.half_avg, 2) efficiency_improvement
20+
FROM
21+
T t1
22+
JOIN T t2 ON t1.driver_id = t2.driver_id AND t1.half < t2.half AND t1.half_avg < t2.half_avg
23+
JOIN drivers d ON t1.driver_id = d.driver_id
24+
ORDER BY efficiency_improvement DESC, d.driver_name;

0 commit comments

Comments
 (0)