Skip to content

Commit 2e04896

Browse files
committed
feat: add new lc problems
1 parent c2e0141 commit 2e04896

File tree

11 files changed

+660
-341
lines changed

11 files changed

+660
-341
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2237. Count Positions on Street With Required Brightness](https://leetcode-cn.com/problems/count-positions-on-street-with-required-brightness)
2+
3+
[English Version](/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given an integer <code>n</code>. A perfectly straight street is represented by a number line ranging from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>lights</code> representing the street lamp(s) on the street. Each <code>lights[i] = [position<sub>i</sub>, range<sub>i</sub>]</code> indicates that there is a street lamp at position <code>position<sub>i</sub></code> that lights up the area from <code>[max(0, position<sub>i</sub> - range<sub>i</sub>), min(n - 1, position<sub>i</sub> + range<sub>i</sub>)]</code> (<strong>inclusive</strong>).</p>
10+
11+
<p>The <strong>brightness</strong> of a position <code>p</code> is defined as the number of street lamps that light up the position <code>p</code>. You are given a <strong>0-indexed</strong> integer array <code>requirement</code> of size <code>n</code> where <code>requirement[i]</code> is the minimum <strong>brightness</strong> of the <code>i<sup>th</sup></code> position on the street.</p>
12+
13+
<p>Return <em>the number of positions </em><code>i</code><em> on the street between </em><code>0</code><em> and </em><code>n - 1</code><em> that have a <strong>brightness</strong> </em><em>of <strong>at least</strong> </em><code>requirement[i]</code><em>.</em></p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/images/screenshot-2022-04-11-at-22-24-43-diagramdrawio-diagramsnet.png" style="height: 150px; width: 579px;" />
18+
<pre>
19+
<strong>Input:</strong> n = 5, lights = [[0,1],[2,1],[3,2]], requirement = [0,2,1,4,1]
20+
<strong>Output:</strong> 4
21+
<strong>Explanation:</strong>
22+
- The first street lamp lights up the area from [max(0, 0 - 1), min(n - 1, 0 + 1)] = [0, 1] (inclusive).
23+
- The second street lamp lights up the area from [max(0, 2 - 1), min(n - 1, 2 + 1)] = [1, 3] (inclusive).
24+
- The third street lamp lights up the area from [max(0, 3 - 2), min(n - 1, 3 + 2)] = [1, 4] (inclusive).
25+
26+
- Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is greater than requirement[0].
27+
- Position 1 is covered by the first, second, and third street lamps. It is covered by 3 street lamps which is greater than requirement[1].
28+
- Position 2 is covered by the second and third street lamps. It is covered by 2 street lamps which is greater than requirement[2].
29+
- Position 3 is covered by the second and third street lamps. It is covered by 2 street lamps which is less than requirement[3].
30+
- Position 4 is covered by the third street lamp. It is covered by 1 street lamp which is equal to requirement[4].
31+
32+
Positions 0, 1, 2, and 4 meet the requirement so we return 4.
33+
</pre>
34+
35+
<p><strong>Example 2:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> n = 1, lights = [[0,1]], requirement = [2]
39+
<strong>Output:</strong> 0
40+
<strong>Explanation:</strong>
41+
- The first street lamp lights up the area from [max(0, 0 - 1), min(n - 1, 0 + 1)] = [0, 0] (inclusive).
42+
- Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is less than requirement[0].
43+
- We return 0 because no position meets their brightness requirement.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
51+
<li><code>1 &lt;= lights.length &lt;= 10<sup>5</sup></code></li>
52+
<li><code>0 &lt;= position<sub>i</sub> &lt; n</code></li>
53+
<li><code>0 &lt;= range<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
54+
<li><code>requirement.length == n</code></li>
55+
<li><code>0 &lt;= requirement[i] &lt;= 10<sup>5</sup></code></li>
56+
</ul>
57+
58+
59+
## 解法
60+
61+
<!-- 这里可写通用的实现逻辑 -->
62+
63+
<!-- tabs:start -->
64+
65+
### **Python3**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```python
70+
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
79+
```
80+
81+
### **TypeScript**
82+
83+
```ts
84+
85+
```
86+
87+
### **...**
88+
89+
```
90+
91+
```
92+
93+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [2237. Count Positions on Street With Required Brightness](https://leetcode.com/problems/count-positions-on-street-with-required-brightness)
2+
3+
[中文文档](/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/README.md)
4+
5+
## Description
6+
7+
<p>You are given an integer <code>n</code>. A perfectly straight street is represented by a number line ranging from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>lights</code> representing the street lamp(s) on the street. Each <code>lights[i] = [position<sub>i</sub>, range<sub>i</sub>]</code> indicates that there is a street lamp at position <code>position<sub>i</sub></code> that lights up the area from <code>[max(0, position<sub>i</sub> - range<sub>i</sub>), min(n - 1, position<sub>i</sub> + range<sub>i</sub>)]</code> (<strong>inclusive</strong>).</p>
8+
9+
<p>The <strong>brightness</strong> of a position <code>p</code> is defined as the number of street lamps that light up the position <code>p</code>. You are given a <strong>0-indexed</strong> integer array <code>requirement</code> of size <code>n</code> where <code>requirement[i]</code> is the minimum <strong>brightness</strong> of the <code>i<sup>th</sup></code> position on the street.</p>
10+
11+
<p>Return <em>the number of positions </em><code>i</code><em> on the street between </em><code>0</code><em> and </em><code>n - 1</code><em> that have a <strong>brightness</strong> </em><em>of <strong>at least</strong> </em><code>requirement[i]</code><em>.</em></p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/2200-2299/2237.Count%20Positions%20on%20Street%20With%20Required%20Brightness/images/screenshot-2022-04-11-at-22-24-43-diagramdrawio-diagramsnet.png" style="height: 150px; width: 579px;" />
16+
<pre>
17+
<strong>Input:</strong> n = 5, lights = [[0,1],[2,1],[3,2]], requirement = [0,2,1,4,1]
18+
<strong>Output:</strong> 4
19+
<strong>Explanation:</strong>
20+
- The first street lamp lights up the area from [max(0, 0 - 1), min(n - 1, 0 + 1)] = [0, 1] (inclusive).
21+
- The second street lamp lights up the area from [max(0, 2 - 1), min(n - 1, 2 + 1)] = [1, 3] (inclusive).
22+
- The third street lamp lights up the area from [max(0, 3 - 2), min(n - 1, 3 + 2)] = [1, 4] (inclusive).
23+
24+
- Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is greater than requirement[0].
25+
- Position 1 is covered by the first, second, and third street lamps. It is covered by 3 street lamps which is greater than requirement[1].
26+
- Position 2 is covered by the second and third street lamps. It is covered by 2 street lamps which is greater than requirement[2].
27+
- Position 3 is covered by the second and third street lamps. It is covered by 2 street lamps which is less than requirement[3].
28+
- Position 4 is covered by the third street lamp. It is covered by 1 street lamp which is equal to requirement[4].
29+
30+
Positions 0, 1, 2, and 4 meet the requirement so we return 4.
31+
32+
</pre>
33+
34+
<p><strong>Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> n = 1, lights = [[0,1]], requirement = [2]
38+
<strong>Output:</strong> 0
39+
<strong>Explanation:</strong>
40+
- The first street lamp lights up the area from [max(0, 0 - 1), min(n - 1, 0 + 1)] = [0, 0] (inclusive).
41+
- Position 0 is covered by the first street lamp. It is covered by 1 street lamp which is less than requirement[0].
42+
- We return 0 because no position meets their brightness requirement.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
50+
<li><code>1 &lt;= lights.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>0 &lt;= position<sub>i</sub> &lt; n</code></li>
52+
<li><code>0 &lt;= range<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
53+
<li><code>requirement.length == n</code></li>
54+
<li><code>0 &lt;= requirement[i] &lt;= 10<sup>5</sup></code></li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
```java
70+
71+
```
72+
73+
### **TypeScript**
74+
75+
```ts
76+
77+
```
78+
79+
### **...**
80+
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [2238. Number of Times a Driver Was a Passenger](https://leetcode-cn.com/problems/number-of-times-a-driver-was-a-passenger)
2+
3+
[English Version](/solution/2200-2299/2238.Number%20of%20Times%20a%20Driver%20Was%20a%20Passenger/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Rides</code></p>
10+
11+
<pre>
12+
+--------------+------+
13+
| Column Name | Type |
14+
+--------------+------+
15+
| ride_id | int |
16+
| driver_id | int |
17+
| passenger_id | int |
18+
+--------------+------+
19+
ride_id is the primary key for this table.
20+
Each row of this table contains the ID of the driver and the ID of the passenger that rode in ride_id.
21+
Note that driver_id != passenger_id.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
26+
<p>Write an SQL query to report the ID of each driver and the number of times they were a passenger.</p>
27+
28+
<p>Return the result table in <strong>any order</strong>.</p>
29+
30+
<p>The query result format is in the following example.</p>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Example 1:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong>
37+
Rides table:
38+
+---------+-----------+--------------+
39+
| ride_id | driver_id | passenger_id |
40+
+---------+-----------+--------------+
41+
| 1 | 7 | 1 |
42+
| 2 | 7 | 2 |
43+
| 3 | 11 | 1 |
44+
| 4 | 11 | 7 |
45+
| 5 | 11 | 7 |
46+
| 6 | 11 | 3 |
47+
+---------+-----------+--------------+
48+
<strong>Output:</strong>
49+
+-----------+-----+
50+
| driver_id | cnt |
51+
+-----------+-----+
52+
| 7 | 2 |
53+
| 11 | 0 |
54+
+-----------+-----+
55+
<strong>Explanation:</strong>
56+
There are two drivers in all the given rides: 7 and 11.
57+
The driver with ID = 7 was a passenger two times.
58+
The driver with ID = 11 was never a passenger.
59+
</pre>
60+
61+
## 解法
62+
63+
<!-- 这里可写通用的实现逻辑 -->
64+
65+
<!-- tabs:start -->
66+
67+
### **SQL**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```sql
72+
73+
```
74+
75+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [2238. Number of Times a Driver Was a Passenger](https://leetcode.com/problems/number-of-times-a-driver-was-a-passenger)
2+
3+
[中文文档](/solution/2200-2299/2238.Number%20of%20Times%20a%20Driver%20Was%20a%20Passenger/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Rides</code></p>
8+
9+
<pre>
10+
+--------------+------+
11+
| Column Name | Type |
12+
+--------------+------+
13+
| ride_id | int |
14+
| driver_id | int |
15+
| passenger_id | int |
16+
+--------------+------+
17+
ride_id is the primary key for this table.
18+
Each row of this table contains the ID of the driver and the ID of the passenger that rode in ride_id.
19+
Note that driver_id != passenger_id.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Write an SQL query to report the ID of each driver and the number of times they were a passenger.</p>
25+
26+
<p>Return the result table in <strong>any order</strong>.</p>
27+
28+
<p>The query result format is in the following example.</p>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Example 1:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong>
35+
Rides table:
36+
+---------+-----------+--------------+
37+
| ride_id | driver_id | passenger_id |
38+
+---------+-----------+--------------+
39+
| 1 | 7 | 1 |
40+
| 2 | 7 | 2 |
41+
| 3 | 11 | 1 |
42+
| 4 | 11 | 7 |
43+
| 5 | 11 | 7 |
44+
| 6 | 11 | 3 |
45+
+---------+-----------+--------------+
46+
<strong>Output:</strong>
47+
+-----------+-----+
48+
| driver_id | cnt |
49+
+-----------+-----+
50+
| 7 | 2 |
51+
| 11 | 0 |
52+
+-----------+-----+
53+
<strong>Explanation:</strong>
54+
There are two drivers in all the given rides: 7 and 11.
55+
The driver with ID = 7 was a passenger two times.
56+
The driver with ID = 11 was never a passenger.
57+
</pre>
58+
59+
## Solutions
60+
61+
<!-- tabs:start -->
62+
63+
### **SQL**
64+
65+
```sql
66+
67+
```
68+
69+
<!-- tabs:end -->

0 commit comments

Comments
 (0)