Skip to content

Commit 354f9d9

Browse files
authoredAug 29, 2023
feat: add solutions to lc problem: No.2837 (#1537)
No.2837.Total Traveled Distance
1 parent 7115c2e commit 354f9d9

File tree

7 files changed

+225
-2
lines changed

7 files changed

+225
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# [2837. Total Traveled Distance](https://leetcode.cn/problems/total-traveled-distance)
2+
3+
[English Version](/solution/2800-2899/2837.Total%20Traveled%20Distance/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code><font face="monospace">Users</font></code></p>
10+
11+
<pre>
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| user_id | int |
16+
| name | varchar |
17+
+-------------+---------+
18+
<code>user_id</code> is the primary key for this table.
19+
Each row of this table contains user id and name.
20+
</pre>
21+
22+
<p>Table: <code>Rides</code></p>
23+
24+
<pre>
25+
+--------------+------+
26+
| Column Name | Type |
27+
+--------------+------+
28+
| ride_id | int |
29+
| user_id | int |
30+
| distance | int |
31+
+--------------+------+
32+
ride_id is the primary key for this table.
33+
Each row of this table contains ride id, user id, and traveled distance.
34+
</pre>
35+
36+
<p>Write an SQL query to calculate the <code>distance</code> traveled by <strong>each user</strong>. If there is a user&nbsp;who hasn&#39;t completed any rides, then their <code>distance</code> should be considered&nbsp;as <code>0</code>. Output the <code>user_id</code>, <code>name</code> and total traveled <code>distance</code>.</p>
37+
38+
<p>Return<em> the result table ordered by </em><code>user_id</code><em> in <strong>ascending</strong> order.</em></p>
39+
40+
<p>The query result format is in the following example.</p>
41+
42+
<p>&nbsp;</p>
43+
<p><strong class="example">Example 1:</strong></p>
44+
45+
<pre>
46+
<strong>Input:</strong>
47+
Users table:
48+
+---------+---------+
49+
| user_id | name |
50+
+---------+---------+
51+
| 17 | Addison |
52+
| 14 | Ethan |
53+
| 4 | Michael |
54+
| 2 | Avery |
55+
| 10 | Eleanor |
56+
+---------+---------+
57+
Rides table:
58+
+---------+---------+----------+
59+
| ride_id | user_id | distance |
60+
+---------+---------+----------+
61+
| 72 | 17 | 160 |
62+
| 42 | 14 | 161 |
63+
| 45 | 4 | 59 |
64+
| 32 | 2 | 197 |
65+
| 15 | 4 | 357 |
66+
| 56 | 2 | 196 |
67+
| 10 | 14 | 25 |
68+
+---------+---------+----------+
69+
<strong>Output:</strong>
70+
+---------+---------+-------------------+
71+
| user_id | name | traveled distance |
72+
+---------+---------+-------------------+
73+
| 2 | Avery | 393 |
74+
| 4 | Michael | 416 |
75+
| 10 | Eleanor | 0 |
76+
| 14 | Ethan | 186 |
77+
| 17 | Addison | 160 |
78+
+---------+---------+-------------------+
79+
<strong>Explanation:</strong>
80+
- User id 2 completed two journeys of 197 and 196, resulting in a combined travel distance of 393.
81+
- User id 4 completed two journeys of 59 and 357, resulting in a combined travel distance of 416.
82+
- User id 14 completed two journeys of 161 and 25, resulting in a combined travel distance of 186.
83+
- User id 16 completed only one journey of 160.
84+
- User id 10 did not complete any journeys, thus the total travel distance remains at 0.
85+
Returning the table orderd by user_id in ascending order.</pre>
86+
87+
## 解法
88+
89+
<!-- 这里可写通用的实现逻辑 -->
90+
91+
**方法一:左连接 + 分组求和**
92+
93+
我们可以使用左连接将两张表连接起来,然后使用分组求和的方式计算每个用户的总距离。注意,如果用户没有完成任何骑行,那么他的距离应该被视为 $0$。
94+
95+
<!-- tabs:start -->
96+
97+
### **SQL**
98+
99+
<!-- 这里可写当前语言的特殊实现逻辑 -->
100+
101+
```sql
102+
# Write your MySQL query statement below
103+
SELECT user_id, name, ifnull(sum(distance), 0) AS 'traveled distance'
104+
FROM
105+
Users
106+
LEFT JOIN Rides USING (user_id)
107+
GROUP BY 1
108+
ORDER BY 1;
109+
```
110+
111+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [2837. Total Traveled Distance](https://leetcode.com/problems/total-traveled-distance)
2+
3+
[中文文档](/solution/2800-2899/2837.Total%20Traveled%20Distance/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code><font face="monospace">Users</font></code></p>
8+
9+
<pre>
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| user_id | int |
14+
| name | varchar |
15+
+-------------+---------+
16+
<code>user_id</code> is the primary key for this table.
17+
Each row of this table contains user id and name.
18+
</pre>
19+
20+
<p>Table: <code>Rides</code></p>
21+
22+
<pre>
23+
+--------------+------+
24+
| Column Name | Type |
25+
+--------------+------+
26+
| ride_id | int |
27+
| user_id | int |
28+
| distance | int |
29+
+--------------+------+
30+
ride_id is the primary key for this table.
31+
Each row of this table contains ride id, user id, and traveled distance.
32+
</pre>
33+
34+
<p>Write an SQL query to calculate the <code>distance</code> traveled by <strong>each user</strong>. If there is a user&nbsp;who hasn&#39;t completed any rides, then their <code>distance</code> should be considered&nbsp;as <code>0</code>. Output the <code>user_id</code>, <code>name</code> and total traveled <code>distance</code>.</p>
35+
36+
<p>Return<em> the result table ordered by </em><code>user_id</code><em> in <strong>ascending</strong> order.</em></p>
37+
38+
<p>The query result format is in the following example.</p>
39+
40+
<p>&nbsp;</p>
41+
<p><strong class="example">Example 1:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong>
45+
Users table:
46+
+---------+---------+
47+
| user_id | name |
48+
+---------+---------+
49+
| 17 | Addison |
50+
| 14 | Ethan |
51+
| 4 | Michael |
52+
| 2 | Avery |
53+
| 10 | Eleanor |
54+
+---------+---------+
55+
Rides table:
56+
+---------+---------+----------+
57+
| ride_id | user_id | distance |
58+
+---------+---------+----------+
59+
| 72 | 17 | 160 |
60+
| 42 | 14 | 161 |
61+
| 45 | 4 | 59 |
62+
| 32 | 2 | 197 |
63+
| 15 | 4 | 357 |
64+
| 56 | 2 | 196 |
65+
| 10 | 14 | 25 |
66+
+---------+---------+----------+
67+
<strong>Output:</strong>
68+
+---------+---------+-------------------+
69+
| user_id | name | traveled distance |
70+
+---------+---------+-------------------+
71+
| 2 | Avery | 393 |
72+
| 4 | Michael | 416 |
73+
| 10 | Eleanor | 0 |
74+
| 14 | Ethan | 186 |
75+
| 17 | Addison | 160 |
76+
+---------+---------+-------------------+
77+
<strong>Explanation:</strong>
78+
- User id 2 completed two journeys of 197 and 196, resulting in a combined travel distance of 393.
79+
- User id 4 completed two journeys of 59 and 357, resulting in a combined travel distance of 416.
80+
- User id 14 completed two journeys of 161 and 25, resulting in a combined travel distance of 186.
81+
- User id 16 completed only one journey of 160.
82+
- User id 10 did not complete any journeys, thus the total travel distance remains at 0.
83+
Returning the table orderd by user_id in ascending order.</pre>
84+
85+
## Solutions
86+
87+
<!-- tabs:start -->
88+
89+
### **SQL**
90+
91+
```sql
92+
# Write your MySQL query statement below
93+
SELECT user_id, name, ifnull(sum(distance), 0) AS 'traveled distance'
94+
FROM
95+
Users
96+
LEFT JOIN Rides USING (user_id)
97+
GROUP BY 1
98+
ORDER BY 1;
99+
```
100+
101+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT user_id, name, ifnull(sum(distance), 0) AS 'traveled distance'
3+
FROM
4+
Users
5+
LEFT JOIN Rides USING (user_id)
6+
GROUP BY 1
7+
ORDER BY 1;

‎solution/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2725,7 +2725,7 @@
27252725
| 2712 | [使所有字符相等的最小成本](/solution/2700-2799/2712.Minimum%20Cost%20to%20Make%20All%20Characters%20Equal/README.md) | `贪心`,`字符串`,`动态规划` | 中等 | 第 347 场周赛 |
27262726
| 2713 | [矩阵中严格递增的单元格数](/solution/2700-2799/2713.Maximum%20Strictly%20Increasing%20Cells%20in%20a%20Matrix/README.md) | `记忆化搜索`,`数组`,`二分查找`,`动态规划`,`矩阵`,`排序` | 困难 | 第 347 场周赛 |
27272727
| 2714 | [找到最短路径的 K 次跨越](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README.md) | `图`,`最短路`,`堆(优先队列)` | 困难 | 🔒 |
2728-
| 2715 | [执行可取消的延迟函数](/solution/2700-2799/2715.Timeout%20Cancellation/README.md) | | 简单 | |
2728+
| 2715 | [Timeout Cancellation](/solution/2700-2799/2715.Timeout%20Cancellation/README.md) | | 简单 | |
27292729
| 2716 | [最小化字符串长度](/solution/2700-2799/2716.Minimize%20String%20Length/README.md) | `哈希表`,`字符串` | 简单 | 第 348 场周赛 |
27302730
| 2717 | [半有序排列](/solution/2700-2799/2717.Semi-Ordered%20Permutation/README.md) | `数组`,`模拟` | 简单 | 第 348 场周赛 |
27312731
| 2718 | [查询后矩阵的和](/solution/2700-2799/2718.Sum%20of%20Matrix%20After%20Queries/README.md) | `数组`,`哈希表` | 中等 | 第 348 场周赛 |
@@ -2847,6 +2847,7 @@
28472847
| 2834 | [找出美丽数组的最小和](/solution/2800-2899/2834.Find%20the%20Minimum%20Possible%20Sum%20of%20a%20Beautiful%20Array/README.md) | | 中等 | 第 360 场周赛 |
28482848
| 2835 | [使子序列的和等于目标的最少操作次数](/solution/2800-2899/2835.Minimum%20Operations%20to%20Form%20Subsequence%20With%20Target%20Sum/README.md) | | 困难 | 第 360 场周赛 |
28492849
| 2836 | [在传球游戏中最大化函数值](/solution/2800-2899/2836.Maximize%20Value%20of%20Function%20in%20a%20Ball%20Passing%20Game/README.md) | | 困难 | 第 360 场周赛 |
2850+
| 2837 | [Total Traveled Distance](/solution/2800-2899/2837.Total%20Traveled%20Distance/README.md) | | 简单 | 🔒 |
28502851

28512852
## 版权
28522853

‎solution/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -2845,6 +2845,7 @@ Press <kbd>Control</kbd>+<kbd>F</kbd>(or <kbd>Command</kbd>+<kbd>F</kbd> on the
28452845
| 2834 | [Find the Minimum Possible Sum of a Beautiful Array](/solution/2800-2899/2834.Find%20the%20Minimum%20Possible%20Sum%20of%20a%20Beautiful%20Array/README_EN.md) | | Medium | Weekly Contest 360 |
28462846
| 2835 | [Minimum Operations to Form Subsequence With Target Sum](/solution/2800-2899/2835.Minimum%20Operations%20to%20Form%20Subsequence%20With%20Target%20Sum/README_EN.md) | | Hard | Weekly Contest 360 |
28472847
| 2836 | [Maximize Value of Function in a Ball Passing Game](/solution/2800-2899/2836.Maximize%20Value%20of%20Function%20in%20a%20Ball%20Passing%20Game/README_EN.md) | | Hard | Weekly Contest 360 |
2848+
| 2837 | [Total Traveled Distance](/solution/2800-2899/2837.Total%20Traveled%20Distance/README_EN.md) | | Easy | 🔒 |
28482849

28492850
## Copyright
28502851

‎solution/summary.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2768,7 +2768,7 @@
27682768
- [2712.使所有字符相等的最小成本](/solution/2700-2799/2712.Minimum%20Cost%20to%20Make%20All%20Characters%20Equal/README.md)
27692769
- [2713.矩阵中严格递增的单元格数](/solution/2700-2799/2713.Maximum%20Strictly%20Increasing%20Cells%20in%20a%20Matrix/README.md)
27702770
- [2714.找到最短路径的 K 次跨越](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README.md)
2771-
- [2715.执行可取消的延迟函数](/solution/2700-2799/2715.Timeout%20Cancellation/README.md)
2771+
- [2715.Timeout Cancellation](/solution/2700-2799/2715.Timeout%20Cancellation/README.md)
27722772
- [2716.最小化字符串长度](/solution/2700-2799/2716.Minimize%20String%20Length/README.md)
27732773
- [2717.半有序排列](/solution/2700-2799/2717.Semi-Ordered%20Permutation/README.md)
27742774
- [2718.查询后矩阵的和](/solution/2700-2799/2718.Sum%20of%20Matrix%20After%20Queries/README.md)
@@ -2892,3 +2892,4 @@
28922892
- [2834.找出美丽数组的最小和](/solution/2800-2899/2834.Find%20the%20Minimum%20Possible%20Sum%20of%20a%20Beautiful%20Array/README.md)
28932893
- [2835.使子序列的和等于目标的最少操作次数](/solution/2800-2899/2835.Minimum%20Operations%20to%20Form%20Subsequence%20With%20Target%20Sum/README.md)
28942894
- [2836.在传球游戏中最大化函数值](/solution/2800-2899/2836.Maximize%20Value%20of%20Function%20in%20a%20Ball%20Passing%20Game/README.md)
2895+
- [2837.Total Traveled Distance](/solution/2800-2899/2837.Total%20Traveled%20Distance/README.md)

‎solution/summary_en.md

+1
Original file line numberDiff line numberDiff line change
@@ -2892,3 +2892,4 @@
28922892
- [2834.Find the Minimum Possible Sum of a Beautiful Array](/solution/2800-2899/2834.Find%20the%20Minimum%20Possible%20Sum%20of%20a%20Beautiful%20Array/README_EN.md)
28932893
- [2835.Minimum Operations to Form Subsequence With Target Sum](/solution/2800-2899/2835.Minimum%20Operations%20to%20Form%20Subsequence%20With%20Target%20Sum/README_EN.md)
28942894
- [2836.Maximize Value of Function in a Ball Passing Game](/solution/2800-2899/2836.Maximize%20Value%20of%20Function%20in%20a%20Ball%20Passing%20Game/README_EN.md)
2895+
- [2837.Total Traveled Distance](/solution/2800-2899/2837.Total%20Traveled%20Distance/README_EN.md)

0 commit comments

Comments
 (0)
Please sign in to comment.