Skip to content

Commit 13f8867

Browse files
authored
feat: add new lc problems (#4538)
1 parent 5b01d2f commit 13f8867

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md
5+
tags:
6+
- 数据库
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency)
12+
13+
[English Version](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md)
14+
15+
## 题目描述
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>drivers</code></p>
20+
21+
<pre>
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| driver_id | int |
26+
| driver_name | varchar |
27+
+-------------+---------+
28+
driver_id is the unique identifier for this table.
29+
Each row contains information about a driver.
30+
</pre>
31+
32+
<p>Table: <code>trips</code></p>
33+
34+
<pre>
35+
+---------------+---------+
36+
| Column Name | Type |
37+
+---------------+---------+
38+
| trip_id | int |
39+
| driver_id | int |
40+
| trip_date | date |
41+
| distance_km | decimal |
42+
| fuel_consumed | decimal |
43+
+---------------+---------+
44+
trip_id is the unique identifier for this table.
45+
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
46+
</pre>
47+
48+
<p>Write a solution to find drivers whose <strong>fuel efficiency has improved</strong> by <strong>comparing</strong> their average fuel efficiency in the<strong> first half</strong> of the year with the <strong>second half</strong> of the year.</p>
49+
50+
<ul>
51+
<li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li>
52+
<li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li>
53+
<li>Only include drivers who have trips in <strong>both halves</strong> of the year</li>
54+
<li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li>
55+
<li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li>
56+
</ul>
57+
58+
<p>Return <em>the result table ordered by efficiency improvement in <strong>descending</strong> order, then by driver name in <strong>ascending</strong> order</em>.</p>
59+
60+
<p>The result format is in the following example.</p>
61+
62+
<p>&nbsp;</p>
63+
<p><strong class="example">Example:</strong></p>
64+
65+
<div class="example-block">
66+
<p><strong>Input:</strong></p>
67+
68+
<p>drivers table:</p>
69+
70+
<pre class="example-io">
71+
+-----------+---------------+
72+
| driver_id | driver_name |
73+
+-----------+---------------+
74+
| 1 | Alice Johnson |
75+
| 2 | Bob Smith |
76+
| 3 | Carol Davis |
77+
| 4 | David Wilson |
78+
| 5 | Emma Brown |
79+
+-----------+---------------+
80+
</pre>
81+
82+
<p>trips table:</p>
83+
84+
<pre class="example-io">
85+
+---------+-----------+------------+-------------+---------------+
86+
| trip_id | driver_id | trip_date | distance_km | fuel_consumed |
87+
+---------+-----------+------------+-------------+---------------+
88+
| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |
89+
| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |
90+
| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |
91+
| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |
92+
| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |
93+
| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |
94+
| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |
95+
| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |
96+
| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |
97+
| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |
98+
| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |
99+
| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |
100+
+---------+-----------+------------+-------------+---------------+
101+
</pre>
102+
103+
<p><strong>Output:</strong></p>
104+
105+
<pre class="example-io">
106+
+-----------+---------------+------------------+-------------------+------------------------+
107+
| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |
108+
+-----------+---------------+------------------+-------------------+------------------------+
109+
| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |
110+
| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |
111+
+-----------+---------------+------------------+-------------------+------------------------+
112+
</pre>
113+
114+
<p><strong>Explanation:</strong></p>
115+
116+
<ul>
117+
<li><strong>Alice Johnson (driver_id = 1):</strong>
118+
119+
<ul>
120+
<li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
121+
<li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li>
122+
<li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
123+
<li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li>
124+
<li>Efficiency improvement: 14.02 - 11.97 = 2.05</li>
125+
</ul>
126+
</li>
127+
<li><strong>Bob Smith (driver_id = 2):</strong>
128+
<ul>
129+
<li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
130+
<li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li>
131+
<li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li>
132+
<li>Second half average efficiency: 13.33</li>
133+
<li>Efficiency improvement: 13.33 - 11.24 = 2.09</li>
134+
</ul>
135+
</li>
136+
<li><strong>Drivers not included:</strong>
137+
<ul>
138+
<li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li>
139+
<li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li>
140+
<li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li>
141+
</ul>
142+
</li>
143+
144+
</ul>
145+
146+
<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p>
147+
</div>
148+
149+
<!-- description:end -->
150+
151+
## 解法
152+
153+
<!-- solution:start -->
154+
155+
### 方法一
156+
157+
<!-- tabs:start -->
158+
159+
#### MySQL
160+
161+
```sql
162+
163+
```
164+
165+
<!-- tabs:end -->
166+
167+
<!-- solution:end -->
168+
169+
<!-- problem:end -->
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md
5+
tags:
6+
- Database
7+
---
8+
9+
<!-- problem:start -->
10+
11+
# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.com/problems/find-drivers-with-improved-fuel-efficiency)
12+
13+
[中文文档](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md)
14+
15+
## Description
16+
17+
<!-- description:start -->
18+
19+
<p>Table: <code>drivers</code></p>
20+
21+
<pre>
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| driver_id | int |
26+
| driver_name | varchar |
27+
+-------------+---------+
28+
driver_id is the unique identifier for this table.
29+
Each row contains information about a driver.
30+
</pre>
31+
32+
<p>Table: <code>trips</code></p>
33+
34+
<pre>
35+
+---------------+---------+
36+
| Column Name | Type |
37+
+---------------+---------+
38+
| trip_id | int |
39+
| driver_id | int |
40+
| trip_date | date |
41+
| distance_km | decimal |
42+
| fuel_consumed | decimal |
43+
+---------------+---------+
44+
trip_id is the unique identifier for this table.
45+
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
46+
</pre>
47+
48+
<p>Write a solution to find drivers whose <strong>fuel efficiency has improved</strong> by <strong>comparing</strong> their average fuel efficiency in the<strong> first half</strong> of the year with the <strong>second half</strong> of the year.</p>
49+
50+
<ul>
51+
<li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li>
52+
<li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li>
53+
<li>Only include drivers who have trips in <strong>both halves</strong> of the year</li>
54+
<li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li>
55+
<li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li>
56+
</ul>
57+
58+
<p>Return <em>the result table ordered by efficiency improvement in <strong>descending</strong> order, then by driver name in <strong>ascending</strong> order</em>.</p>
59+
60+
<p>The result format is in the following example.</p>
61+
62+
<p>&nbsp;</p>
63+
<p><strong class="example">Example:</strong></p>
64+
65+
<div class="example-block">
66+
<p><strong>Input:</strong></p>
67+
68+
<p>drivers table:</p>
69+
70+
<pre class="example-io">
71+
+-----------+---------------+
72+
| driver_id | driver_name |
73+
+-----------+---------------+
74+
| 1 | Alice Johnson |
75+
| 2 | Bob Smith |
76+
| 3 | Carol Davis |
77+
| 4 | David Wilson |
78+
| 5 | Emma Brown |
79+
+-----------+---------------+
80+
</pre>
81+
82+
<p>trips table:</p>
83+
84+
<pre class="example-io">
85+
+---------+-----------+------------+-------------+---------------+
86+
| trip_id | driver_id | trip_date | distance_km | fuel_consumed |
87+
+---------+-----------+------------+-------------+---------------+
88+
| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |
89+
| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |
90+
| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |
91+
| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |
92+
| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |
93+
| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |
94+
| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |
95+
| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |
96+
| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |
97+
| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |
98+
| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |
99+
| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |
100+
+---------+-----------+------------+-------------+---------------+
101+
</pre>
102+
103+
<p><strong>Output:</strong></p>
104+
105+
<pre class="example-io">
106+
+-----------+---------------+------------------+-------------------+------------------------+
107+
| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |
108+
+-----------+---------------+------------------+-------------------+------------------------+
109+
| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |
110+
| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |
111+
+-----------+---------------+------------------+-------------------+------------------------+
112+
</pre>
113+
114+
<p><strong>Explanation:</strong></p>
115+
116+
<ul>
117+
<li><strong>Alice Johnson (driver_id = 1):</strong>
118+
119+
<ul>
120+
<li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
121+
<li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li>
122+
<li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
123+
<li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li>
124+
<li>Efficiency improvement: 14.02 - 11.97 = 2.05</li>
125+
</ul>
126+
</li>
127+
<li><strong>Bob Smith (driver_id = 2):</strong>
128+
<ul>
129+
<li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
130+
<li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li>
131+
<li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li>
132+
<li>Second half average efficiency: 13.33</li>
133+
<li>Efficiency improvement: 13.33 - 11.24 = 2.09</li>
134+
</ul>
135+
</li>
136+
<li><strong>Drivers not included:</strong>
137+
<ul>
138+
<li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li>
139+
<li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li>
140+
<li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li>
141+
</ul>
142+
</li>
143+
144+
</ul>
145+
146+
<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p>
147+
</div>
148+
149+
<!-- description:end -->
150+
151+
## Solutions
152+
153+
<!-- solution:start -->
154+
155+
### Solution 1
156+
157+
<!-- tabs:start -->
158+
159+
#### MySQL
160+
161+
```sql
162+
163+
```
164+
165+
<!-- tabs:end -->
166+
167+
<!-- solution:end -->
168+
169+
<!-- problem:end -->

solution/DATABASE_README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@
320320
| 3570 | [查找无可用副本的书籍](/solution/3500-3599/3570.Find%20Books%20with%20No%20Available%20Copies/README.md) | `数据库` | 简单 | |
321321
| 3580 | [寻找持续进步的员工](/solution/3500-3599/3580.Find%20Consistently%20Improving%20Employees/README.md) | `数据库` | 中等 | |
322322
| 3586 | [寻找 COVID 康复患者](/solution/3500-3599/3586.Find%20COVID%20Recovery%20Patients/README.md) | `数据库` | 中等 | |
323+
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
323324

324325
## 版权
325326

solution/DATABASE_README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
318318
| 3570 | [Find Books with No Available Copies](/solution/3500-3599/3570.Find%20Books%20with%20No%20Available%20Copies/README_EN.md) | `Database` | Easy | |
319319
| 3580 | [Find Consistently Improving Employees](/solution/3500-3599/3580.Find%20Consistently%20Improving%20Employees/README_EN.md) | `Database` | Medium | |
320320
| 3586 | [Find COVID Recovery Patients](/solution/3500-3599/3586.Find%20COVID%20Recovery%20Patients/README_EN.md) | `Database` | Medium | |
321+
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md) | | Medium | |
321322

322323
## Copyright
323324

solution/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,7 @@
36113611
| 3598 | [相邻字符串之间的最长公共前缀](/solution/3500-3599/3598.Longest%20Common%20Prefix%20Between%20Adjacent%20Strings%20After%20Removals/README.md) | | 中等 | 第 456 场周赛 |
36123612
| 3599 | [划分数组得到最小 XOR](/solution/3500-3599/3599.Partition%20Array%20to%20Minimize%20XOR/README.md) | | 中等 | 第 456 场周赛 |
36133613
| 3600 | [升级后最大生成树稳定性](/solution/3600-3699/3600.Maximize%20Spanning%20Tree%20Stability%20with%20Upgrades/README.md) | | 困难 | 第 456 场周赛 |
3614+
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
36143615

36153616
## 版权
36163617

solution/README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,6 +3609,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
36093609
| 3598 | [Longest Common Prefix Between Adjacent Strings After Removals](/solution/3500-3599/3598.Longest%20Common%20Prefix%20Between%20Adjacent%20Strings%20After%20Removals/README_EN.md) | | Medium | Weekly Contest 456 |
36103610
| 3599 | [Partition Array to Minimize XOR](/solution/3500-3599/3599.Partition%20Array%20to%20Minimize%20XOR/README_EN.md) | | Medium | Weekly Contest 456 |
36113611
| 3600 | [Maximize Spanning Tree Stability with Upgrades](/solution/3600-3699/3600.Maximize%20Spanning%20Tree%20Stability%20with%20Upgrades/README_EN.md) | | Hard | Weekly Contest 456 |
3612+
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md) | | Medium | |
36123613

36133614
## Copyright
36143615

0 commit comments

Comments
 (0)