Skip to content

feat: add new lc problems #4538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md
tags:
- 数据库
---

<!-- problem:start -->

# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency)

[English Version](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md)

## 题目描述

<!-- description:start -->

<p>Table: <code>drivers</code></p>

<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| driver_id | int |
| driver_name | varchar |
+-------------+---------+
driver_id is the unique identifier for this table.
Each row contains information about a driver.
</pre>

<p>Table: <code>trips</code></p>

<pre>
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| trip_id | int |
| driver_id | int |
| trip_date | date |
| distance_km | decimal |
| fuel_consumed | decimal |
+---------------+---------+
trip_id is the unique identifier for this table.
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
</pre>

<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>

<ul>
<li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li>
<li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li>
<li>Only include drivers who have trips in <strong>both halves</strong> of the year</li>
<li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li>
<li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li>
</ul>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>drivers table:</p>

<pre class="example-io">
+-----------+---------------+
| driver_id | driver_name |
+-----------+---------------+
| 1 | Alice Johnson |
| 2 | Bob Smith |
| 3 | Carol Davis |
| 4 | David Wilson |
| 5 | Emma Brown |
+-----------+---------------+
</pre>

<p>trips table:</p>

<pre class="example-io">
+---------+-----------+------------+-------------+---------------+
| trip_id | driver_id | trip_date | distance_km | fuel_consumed |
+---------+-----------+------------+-------------+---------------+
| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |
| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |
| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |
| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |
| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |
| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |
| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |
| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |
| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |
| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |
| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |
| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |
+---------+-----------+------------+-------------+---------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+-----------+---------------+------------------+-------------------+------------------------+
| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |
+-----------+---------------+------------------+-------------------+------------------------+
| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |
| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |
+-----------+---------------+------------------+-------------------+------------------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li><strong>Alice Johnson (driver_id = 1):</strong>

<ul>
<li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
<li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li>
<li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
<li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li>
<li>Efficiency improvement: 14.02 - 11.97 = 2.05</li>
</ul>
</li>
<li><strong>Bob Smith (driver_id = 2):</strong>
<ul>
<li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
<li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li>
<li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li>
<li>Second half average efficiency: 13.33</li>
<li>Efficiency improvement: 13.33 - 11.24 = 2.09</li>
</ul>
</li>
<li><strong>Drivers not included:</strong>
<ul>
<li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li>
<li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li>
<li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li>
</ul>
</li>

</ul>

<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### MySQL

```sql

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
---
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md
tags:
- Database
---

<!-- problem:start -->

# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.com/problems/find-drivers-with-improved-fuel-efficiency)

[中文文档](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md)

## Description

<!-- description:start -->

<p>Table: <code>drivers</code></p>

<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| driver_id | int |
| driver_name | varchar |
+-------------+---------+
driver_id is the unique identifier for this table.
Each row contains information about a driver.
</pre>

<p>Table: <code>trips</code></p>

<pre>
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| trip_id | int |
| driver_id | int |
| trip_date | date |
| distance_km | decimal |
| fuel_consumed | decimal |
+---------------+---------+
trip_id is the unique identifier for this table.
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
</pre>

<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>

<ul>
<li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li>
<li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li>
<li>Only include drivers who have trips in <strong>both halves</strong> of the year</li>
<li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li>
<li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li>
</ul>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>drivers table:</p>

<pre class="example-io">
+-----------+---------------+
| driver_id | driver_name |
+-----------+---------------+
| 1 | Alice Johnson |
| 2 | Bob Smith |
| 3 | Carol Davis |
| 4 | David Wilson |
| 5 | Emma Brown |
+-----------+---------------+
</pre>

<p>trips table:</p>

<pre class="example-io">
+---------+-----------+------------+-------------+---------------+
| trip_id | driver_id | trip_date | distance_km | fuel_consumed |
+---------+-----------+------------+-------------+---------------+
| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |
| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |
| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |
| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |
| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |
| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |
| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |
| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |
| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |
| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |
| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |
| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |
+---------+-----------+------------+-------------+---------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+-----------+---------------+------------------+-------------------+------------------------+
| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |
+-----------+---------------+------------------+-------------------+------------------------+
| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |
| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |
+-----------+---------------+------------------+-------------------+------------------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li><strong>Alice Johnson (driver_id = 1):</strong>

<ul>
<li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
<li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li>
<li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
<li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li>
<li>Efficiency improvement: 14.02 - 11.97 = 2.05</li>
</ul>
</li>
<li><strong>Bob Smith (driver_id = 2):</strong>
<ul>
<li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
<li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li>
<li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li>
<li>Second half average efficiency: 13.33</li>
<li>Efficiency improvement: 13.33 - 11.24 = 2.09</li>
</ul>
</li>
<li><strong>Drivers not included:</strong>
<ul>
<li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li>
<li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li>
<li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li>
</ul>
</li>

</ul>

<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### MySQL

```sql

```

<!-- tabs:end -->

<!-- solution:end -->

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

## 版权

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

## Copyright

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

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3609,6 +3609,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 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 |
| 3599 | [Partition Array to Minimize XOR](/solution/3500-3599/3599.Partition%20Array%20to%20Minimize%20XOR/README_EN.md) | | Medium | Weekly Contest 456 |
| 3600 | [Maximize Spanning Tree Stability with Upgrades](/solution/3600-3699/3600.Maximize%20Spanning%20Tree%20Stability%20with%20Upgrades/README_EN.md) | | Hard | Weekly Contest 456 |
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md) | | Medium | |

## Copyright

Expand Down