Skip to content

Commit f063133

Browse files
committed
feat: update main script and add new lc problems
1 parent f80561e commit f063133

File tree

47 files changed

+2584
-551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2584
-551
lines changed

images/contributors.svg

+4-4
Loading

solution/1600-1699/1645.Hopper Company Queries II/README.md

+147-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,155 @@
1-
# [1645. ](https://leetcode-cn.com/problems/hopper-company-queries-ii)
1+
# [1645. Hopper Company Queries II](https://leetcode-cn.com/problems/hopper-company-queries-ii)
22

33
[English Version](/solution/1600-1699/1645.Hopper%20Company%20Queries%20II/README_EN.md)
44

55
## 题目描述
66

77
<!-- 这里写题目描述 -->
88

9-
None
9+
<p>Table: <code>Drivers</code></p>
10+
11+
<pre>
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| driver_id | int |
16+
| join_date | date |
17+
+-------------+---------+
18+
driver_id is the primary key for this table.
19+
Each row of this table contains the driver&#39;s ID and the date they joined the Hopper company.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Table: <code>Rides</code></p>
25+
26+
<pre>
27+
+--------------+---------+
28+
| Column Name | Type |
29+
+--------------+---------+
30+
| ride_id | int |
31+
| user_id | int |
32+
| requested_at | date |
33+
+--------------+---------+
34+
ride_id is the primary key for this table.
35+
Each row of this table contains the ID of a ride, the user&#39;s ID that requested it, and the day they requested it.
36+
There may be some ride requests in this table that were not accepted.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p>Table: <code>AcceptedRides</code></p>
42+
43+
<pre>
44+
+---------------+---------+
45+
| Column Name | Type |
46+
+---------------+---------+
47+
| ride_id | int |
48+
| driver_id | int |
49+
| ride_distance | int |
50+
| ride_duration | int |
51+
+---------------+---------+
52+
ride_id is the primary key for this table.
53+
Each row of this table contains some information about an accepted ride.
54+
It is guaranteed that each accepted ride exists in the Rides table.
55+
</pre>
56+
57+
<p>&nbsp;</p>
58+
59+
<p>Write an SQL query to report the <strong>percentage</strong> of working drivers (<code>working_percentage</code>) for each month of <strong>2020</strong> where:</p>
60+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1600-1699/1645.Hopper%20Company%20Queries%20II/images/codecogseqn.png" style="width: 970px; height: 44px;" />
61+
<p><strong>Note</strong> that if the number of available drivers during a month is zero, we consider the <code>working_percentage</code> to be <code>0</code>.</p>
62+
63+
<p>Return the result table ordered by <code>month</code> in <strong>ascending</strong> order, where <code>month</code> is the month&#39;s number (January is <code>1</code>, February is <code>2</code>, etc.). Round <code>working_percentage</code> to the nearest <strong>2 decimal places</strong>.</p>
64+
65+
<p>The query result format is in the following example.</p>
66+
67+
<p>&nbsp;</p>
68+
69+
<pre>
70+
Drivers table:
71+
+-----------+------------+
72+
| driver_id | join_date |
73+
+-----------+------------+
74+
| 10 | 2019-12-10 |
75+
| 8 | 2020-1-13 |
76+
| 5 | 2020-2-16 |
77+
| 7 | 2020-3-8 |
78+
| 4 | 2020-5-17 |
79+
| 1 | 2020-10-24 |
80+
| 6 | 2021-1-5 |
81+
+-----------+------------+
82+
83+
Rides table:
84+
+---------+---------+--------------+
85+
| ride_id | user_id | requested_at |
86+
+---------+---------+--------------+
87+
| 6 | 75 | 2019-12-9 |
88+
| 1 | 54 | 2020-2-9 |
89+
| 10 | 63 | 2020-3-4 |
90+
| 19 | 39 | 2020-4-6 |
91+
| 3 | 41 | 2020-6-3 |
92+
| 13 | 52 | 2020-6-22 |
93+
| 7 | 69 | 2020-7-16 |
94+
| 17 | 70 | 2020-8-25 |
95+
| 20 | 81 | 2020-11-2 |
96+
| 5 | 57 | 2020-11-9 |
97+
| 2 | 42 | 2020-12-9 |
98+
| 11 | 68 | 2021-1-11 |
99+
| 15 | 32 | 2021-1-17 |
100+
| 12 | 11 | 2021-1-19 |
101+
| 14 | 18 | 2021-1-27 |
102+
+---------+---------+--------------+
103+
104+
AcceptedRides table:
105+
+---------+-----------+---------------+---------------+
106+
| ride_id | driver_id | ride_distance | ride_duration |
107+
+---------+-----------+---------------+---------------+
108+
| 10 | 10 | 63 | 38 |
109+
| 13 | 10 | 73 | 96 |
110+
| 7 | 8 | 100 | 28 |
111+
| 17 | 7 | 119 | 68 |
112+
| 20 | 1 | 121 | 92 |
113+
| 5 | 7 | 42 | 101 |
114+
| 2 | 4 | 6 | 38 |
115+
| 11 | 8 | 37 | 43 |
116+
| 15 | 8 | 108 | 82 |
117+
| 12 | 8 | 38 | 34 |
118+
| 14 | 1 | 90 | 74 |
119+
+---------+-----------+---------------+---------------+
120+
121+
Result table:
122+
+-------+--------------------+
123+
| month | working_percentage |
124+
+-------+--------------------+
125+
| 1 | 0.00 |
126+
| 2 | 0.00 |
127+
| 3 | 25.00 |
128+
| 4 | 0.00 |
129+
| 5 | 0.00 |
130+
| 6 | 20.00 |
131+
| 7 | 20.00 |
132+
| 8 | 20.00 |
133+
| 9 | 0.00 |
134+
| 10 | 0.00 |
135+
| 11 | 33.33 |
136+
| 12 | 16.67 |
137+
+-------+--------------------+
138+
139+
By the end of January --&gt; two active drivers (10, 8) and no accepted rides. The percentage is 0%.
140+
By the end of February --&gt; three active drivers (10, 8, 5) and no accepted rides. The percentage is 0%.
141+
By the end of March --&gt; four active drivers (10, 8, 5, 7) and one accepted ride by driver (10). The percentage is (1 / 4) * 100 = 25%.
142+
By the end of April --&gt; four active drivers (10, 8, 5, 7) and no accepted rides. The percentage is 0%.
143+
By the end of May --&gt; five active drivers (10, 8, 5, 7, 4) and no accepted rides. The percentage is 0%.
144+
By the end of June --&gt; five active drivers (10, 8, 5, 7, 4) and one accepted ride by driver (10). The percentage is (1 / 5) * 100 = 20%.
145+
By the end of July --&gt; five active drivers (10, 8, 5, 7, 4) and one accepted ride by driver (8). The percentage is (1 / 5) * 100 = 20%.
146+
By the end of August --&gt; five active drivers (10, 8, 5, 7, 4) and one accepted ride by driver (7). The percentage is (1 / 5) * 100 = 20%.
147+
By the end of Septemeber --&gt; five active drivers (10, 8, 5, 7, 4) and no accepted rides. The percentage is 0%.
148+
By the end of October --&gt; six active drivers (10, 8, 5, 7, 4, 1) and no accepted rides. The percentage is 0%.
149+
By the end of November --&gt; six active drivers (10, 8, 5, 7, 4, 1) and two accepted rides by <strong>two different</strong> drivers (1, 7). The percentage is (2 / 6) * 100 = 33.33%.
150+
By the end of December --&gt; six active drivers (10, 8, 5, 7, 4, 1) and one accepted ride by driver (4). The percentage is (1 / 6) * 100 = 16.67%.
151+
</pre>
152+
10153

11154
## 解法
12155

@@ -16,6 +159,8 @@ None
16159

17160
### **SQL**
18161

162+
<!-- 这里可写当前语言的特殊实现逻辑 -->
163+
19164
```sql
20165

21166
```
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,164 @@
1-
# [1651. ](https://leetcode-cn.com/problems/hopper-company-queries-iii)
1+
# [1651. Hopper Company Queries III](https://leetcode-cn.com/problems/hopper-company-queries-iii)
22

33
[English Version](/solution/1600-1699/1651.Hopper%20Company%20Queries%20III/README_EN.md)
44

55
## 题目描述
66

77
<!-- 这里写题目描述 -->
88

9-
None
9+
<p>Table: <code>Drivers</code></p>
1010

11-
## 解法
11+
<pre>
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| driver_id | int |
16+
| join_date | date |
17+
+-------------+---------+
18+
driver_id is the primary key for this table.
19+
Each row of this table contains the driver&#39;s ID and the date they joined the Hopper company.
20+
</pre>
1221

13-
<!-- 这里可写通用的实现逻辑 -->
22+
<p>&nbsp;</p>
1423

15-
<!-- tabs:start -->
24+
<p>Table: <code>Rides</code></p>
1625

17-
### **Python3**
26+
<pre>
27+
+--------------+---------+
28+
| Column Name | Type |
29+
+--------------+---------+
30+
| ride_id | int |
31+
| user_id | int |
32+
| requested_at | date |
33+
+--------------+---------+
34+
ride_id is the primary key for this table.
35+
Each row of this table contains the ID of a ride, the user&#39;s ID that requested it, and the day they requested it.
36+
There may be some ride requests in this table that were not accepted.
37+
</pre>
1838

19-
<!-- 这里可写当前语言的特殊实现逻辑 -->
39+
<p>&nbsp;</p>
40+
41+
<p>Table: <code>AcceptedRides</code></p>
42+
43+
<pre>
44+
+---------------+---------+
45+
| Column Name | Type |
46+
+---------------+---------+
47+
| ride_id | int |
48+
| driver_id | int |
49+
| ride_distance | int |
50+
| ride_duration | int |
51+
+---------------+---------+
52+
ride_id is the primary key for this table.
53+
Each row of this table contains some information about an accepted ride.
54+
It is guaranteed that each accepted ride exists in the Rides table.
55+
</pre>
56+
57+
<p>&nbsp;</p>
58+
59+
<p>Write an SQL query to compute the <code>average_ride_distance</code> and <code>average_ride_duration</code> of every 3-month window starting from <strong>January - March 2020</strong> to <strong>October - December 2020</strong>. Round <code>average_ride_distance</code> and <code>average_ride_duration</code> to the nearest <strong>two decimal places</strong>.</p>
60+
61+
<p>The <code>average_ride_distance</code> is calculated by summing up the total <code>ride_distance</code> values from the three months and dividing it by <code>3</code>. The <code>average_ride_duration</code> is calculated in a similar way.</p>
62+
63+
<p>Return the result table ordered by <code>month</code> in ascending order, where <code>month</code> is the starting month&#39;s number (January is <code>1</code>, February is <code>2</code>, etc.).</p>
64+
65+
<p>The query result format is in the following example.</p>
66+
67+
<p>&nbsp;</p>
68+
69+
<pre>
70+
Drivers table:
71+
+-----------+------------+
72+
| driver_id | join_date |
73+
+-----------+------------+
74+
| 10 | 2019-12-10 |
75+
| 8 | 2020-1-13 |
76+
| 5 | 2020-2-16 |
77+
| 7 | 2020-3-8 |
78+
| 4 | 2020-5-17 |
79+
| 1 | 2020-10-24 |
80+
| 6 | 2021-1-5 |
81+
+-----------+------------+
82+
83+
Rides table:
84+
+---------+---------+--------------+
85+
| ride_id | user_id | requested_at |
86+
+---------+---------+--------------+
87+
| 6 | 75 | 2019-12-9 |
88+
| 1 | 54 | 2020-2-9 |
89+
| 10 | 63 | 2020-3-4 |
90+
| 19 | 39 | 2020-4-6 |
91+
| 3 | 41 | 2020-6-3 |
92+
| 13 | 52 | 2020-6-22 |
93+
| 7 | 69 | 2020-7-16 |
94+
| 17 | 70 | 2020-8-25 |
95+
| 20 | 81 | 2020-11-2 |
96+
| 5 | 57 | 2020-11-9 |
97+
| 2 | 42 | 2020-12-9 |
98+
| 11 | 68 | 2021-1-11 |
99+
| 15 | 32 | 2021-1-17 |
100+
| 12 | 11 | 2021-1-19 |
101+
| 14 | 18 | 2021-1-27 |
102+
+---------+---------+--------------+
103+
104+
AcceptedRides table:
105+
+---------+-----------+---------------+---------------+
106+
| ride_id | driver_id | ride_distance | ride_duration |
107+
+---------+-----------+---------------+---------------+
108+
| 10 | 10 | 63 | 38 |
109+
| 13 | 10 | 73 | 96 |
110+
| 7 | 8 | 100 | 28 |
111+
| 17 | 7 | 119 | 68 |
112+
| 20 | 1 | 121 | 92 |
113+
| 5 | 7 | 42 | 101 |
114+
| 2 | 4 | 6 | 38 |
115+
| 11 | 8 | 37 | 43 |
116+
| 15 | 8 | 108 | 82 |
117+
| 12 | 8 | 38 | 34 |
118+
| 14 | 1 | 90 | 74 |
119+
+---------+-----------+---------------+---------------+
120+
121+
Result table:
122+
+-------+-----------------------+-----------------------+
123+
| month | average_ride_distance | average_ride_duration |
124+
+-------+-----------------------+-----------------------+
125+
| 1 | 21.00 | 12.67 |
126+
| 2 | 21.00 | 12.67 |
127+
| 3 | 21.00 | 12.67 |
128+
| 4 | 24.33 | 32.00 |
129+
| 5 | 57.67 | 41.33 |
130+
| 6 | 97.33 | 64.00 |
131+
| 7 | 73.00 | 32.00 |
132+
| 8 | 39.67 | 22.67 |
133+
| 9 | 54.33 | 64.33 |
134+
| 10 | 56.33 | 77.00 |
135+
+-------+-----------------------+-----------------------+
136+
137+
By the end of January --&gt; average_ride_distance = (0+0+63)/3=21, average_ride_duration = (0+0+38)/3=12.67
138+
By the end of February --&gt; average_ride_distance = (0+63+0)/3=21, average_ride_duration = (0+38+0)/3=12.67
139+
By the end of March --&gt; average_ride_distance = (63+0+0)/3=21, average_ride_duration = (38+0+0)/3=12.67
140+
By the end of April --&gt; average_ride_distance = (0+0+73)/3=24.33, average_ride_duration = (0+0+96)/3=32.00
141+
By the end of May --&gt; average_ride_distance = (0+73+100)/3=57.67, average_ride_duration = (0+96+28)/3=41.33
142+
By the end of June --&gt; average_ride_distance = (73+100+119)/3=97.33, average_ride_duration = (96+28+68)/3=64.00
143+
By the end of July --&gt; average_ride_distance = (100+119+0)/3=73.00, average_ride_duration = (28+68+0)/3=32.00
144+
By the end of August --&gt; average_ride_distance = (119+0+0)/3=39.67, average_ride_duration = (68+0+0)/3=22.67
145+
By the end of Septemeber --&gt; average_ride_distance = (0+0+163)/3=54.33, average_ride_duration = (0+0+193)/3=64.33
146+
By the end of October --&gt; average_ride_distance = (0+163+6)/3=56.33, average_ride_duration = (0+193+38)/3=77.00
147+
</pre>
148+
149+
150+
## 解法
151+
152+
<!-- 这里可写通用的实现逻辑 -->
20153

21154
<!-- tabs:start -->
22155

23156
### **SQL**
24157

158+
<!-- 这里可写当前语言的特殊实现逻辑 -->
159+
25160
```sql
26161

27162
```
28163

29-
<!-- tabs:end -->
164+
<!-- tabs:end -->

0 commit comments

Comments
 (0)