Skip to content

Commit c285f5f

Browse files
authored
feat: add new lc problems: No.3061~3064 (#2390)
1 parent 4f3ccae commit c285f5f

File tree

14 files changed

+678
-0
lines changed

14 files changed

+678
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [3061. Calculate Trapping Rain Water](https://leetcode.cn/problems/calculate-trapping-rain-water)
2+
3+
[English Version](/solution/3000-3099/3061.Calculate%20Trapping%20Rain%20Water/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>Table: <font face="monospace">Heights</font></p>
12+
13+
<pre>
14+
+-------------+------+
15+
| Column Name | Type |
16+
+-------------+------+
17+
| id | int |
18+
| height | int |
19+
+-------------+------+
20+
id is the primary key (column with unique values) for this table, and it is guaranteed to be in sequential order.
21+
Each row of this table contains an id and height.
22+
</pre>
23+
24+
<p>Write a solution to calculate the amount of rainwater can be <strong>trapped between the bars</strong> in the landscape, considering that each bar has a <strong>width</strong> of <code>1</code> unit.</p>
25+
26+
<p>Return <em>the result table in </em><strong>any</strong><em> order.</em></p>
27+
28+
<p>The result format is in the following example.</p>
29+
30+
<p>&nbsp;</p>
31+
<p><strong class="example">Example 1:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong>
35+
Heights table:
36+
+-----+--------+
37+
| id | height |
38+
+-----+--------+
39+
| 1 | 0 |
40+
| 2 | 1 |
41+
| 3 | 0 |
42+
| 4 | 2 |
43+
| 5 | 1 |
44+
| 6 | 0 |
45+
| 7 | 1 |
46+
| 8 | 3 |
47+
| 9 | 2 |
48+
| 10 | 1 |
49+
| 11 | 2 |
50+
| 12 | 1 |
51+
+-----+--------+
52+
<strong>Output:</strong>
53+
+---------------------+
54+
| total_trapped_water |
55+
+---------------------+
56+
| 6 |
57+
+---------------------+
58+
<strong>Explanation:</strong>
59+
<img src="https://assets.leetcode.com/uploads/2024/02/26/trapping_rain_water.png" style="width:500px; height:200px;" />
60+
61+
The elevation map depicted above (in the black section) is graphically represented with the x-axis denoting the id and the y-axis representing the heights [0,1,0,2,1,0,1,3,2,1,2,1]. In this scenario, 6 units of rainwater are trapped within the blue section.
62+
</pre>
63+
64+
## 解法
65+
66+
### 方法一
67+
68+
<!-- tabs:start -->
69+
70+
```sql
71+
72+
```
73+
74+
<!-- tabs:end -->
75+
76+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [3061. Calculate Trapping Rain Water](https://leetcode.com/problems/calculate-trapping-rain-water)
2+
3+
[中文文档](/solution/3000-3099/3061.Calculate%20Trapping%20Rain%20Water/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>Table: <font face="monospace">Heights</font></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| id | int |
16+
| height | int |
17+
+-------------+------+
18+
id is the primary key (column with unique values) for this table, and it is guaranteed to be in sequential order.
19+
Each row of this table contains an id and height.
20+
</pre>
21+
22+
<p>Write a solution to calculate the amount of rainwater can be <strong>trapped between the bars</strong> in the landscape, considering that each bar has a <strong>width</strong> of <code>1</code> unit.</p>
23+
24+
<p>Return <em>the result table in </em><strong>any</strong><em> order.</em></p>
25+
26+
<p>The result format is in the following example.</p>
27+
28+
<p>&nbsp;</p>
29+
<p><strong class="example">Example 1:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong>
33+
Heights table:
34+
+-----+--------+
35+
| id | height |
36+
+-----+--------+
37+
| 1 | 0 |
38+
| 2 | 1 |
39+
| 3 | 0 |
40+
| 4 | 2 |
41+
| 5 | 1 |
42+
| 6 | 0 |
43+
| 7 | 1 |
44+
| 8 | 3 |
45+
| 9 | 2 |
46+
| 10 | 1 |
47+
| 11 | 2 |
48+
| 12 | 1 |
49+
+-----+--------+
50+
<strong>Output:</strong>
51+
+---------------------+
52+
| total_trapped_water |
53+
+---------------------+
54+
| 6 |
55+
+---------------------+
56+
<strong>Explanation:</strong>
57+
<img src="https://assets.leetcode.com/uploads/2024/02/26/trapping_rain_water.png" style="width:500px; height:200px;" />
58+
59+
The elevation map depicted above (in the black section) is graphically represented with the x-axis denoting the id and the y-axis representing the heights [0,1,0,2,1,0,1,3,2,1,2,1]. In this scenario, 6 units of rainwater are trapped within the blue section.
60+
</pre>
61+
62+
## Solutions
63+
64+
### Solution 1
65+
66+
<!-- tabs:start -->
67+
68+
```sql
69+
70+
```
71+
72+
<!-- tabs:end -->
73+
74+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# [3062. Winner of the Linked List Game](https://leetcode.cn/problems/winner-of-the-linked-list-game)
2+
3+
[English Version](/solution/3000-3099/3062.Winner%20of%20the%20Linked%20List%20Game/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>You are given the <code>head</code> of a linked list of <strong>even</strong> length containing integers.</p>
12+
13+
<p>Each <strong>odd-indexed</strong> node contains an odd integer and each <strong>even-indexed</strong> node contains an even integer.</p>
14+
15+
<p>We call each even-indexed node and its next node a <strong>pair</strong>, e.g., the nodes with indices <code>0</code> and <code>1</code> are a pair, the nodes with indices <code>2</code> and <code>3</code> are a pair, and so on.</p>
16+
17+
<p>For every <strong>pair</strong>, we compare the values of the nodes in the pair:</p>
18+
19+
<ul>
20+
<li>If the odd-indexed node is higher, the <code>&quot;Odd&quot;</code> team gets a point.</li>
21+
<li>If the even-indexed node is higher, the <code>&quot;Even&quot;</code> team gets a point.</li>
22+
</ul>
23+
24+
<p>Return <em>the name of the team with the <strong>higher</strong> points, if the points are equal, return</em> <code>&quot;Tie&quot;</code>.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1: </strong></p>
28+
29+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
30+
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [2,1] </span></p>
31+
32+
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> &quot;Even&quot; </span></p>
33+
34+
<p><strong>Explanation: </strong> There is only one pair in this linked list and that is <code>(2,1)</code>. Since <code>2 &gt; 1</code>, the Even team gets the point.</p>
35+
36+
<p>Hence, the answer would be <code>&quot;Even&quot;</code>.</p>
37+
</div>
38+
39+
<p><strong class="example">Example 2: </strong></p>
40+
41+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
42+
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [2,5,4,7,20,5] </span></p>
43+
44+
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> &quot;Odd&quot; </span></p>
45+
46+
<p><strong>Explanation: </strong> There are <code>3</code> pairs in this linked list. Let&#39;s investigate each pair individually:</p>
47+
48+
<p><code>(2,5)</code> -&gt; Since <code>2 &lt; 5</code>, The Odd team gets the point.</p>
49+
50+
<p><code>(4,7)</code> -&gt; Since <code>4 &lt; 7</code>, The Odd team gets the point.</p>
51+
52+
<p><code>(20,5)</code> -&gt; Since <code>20 &gt; 5</code>, The Even team gets the point.</p>
53+
54+
<p>The Odd team earned <code>2</code> points while the Even team got <code>1</code> point and the Odd team has the higher points.</p>
55+
56+
<p>Hence, the answer would be <code>&quot;Odd&quot;</code>.</p>
57+
</div>
58+
59+
<p><strong class="example">Example 3: </strong></p>
60+
61+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
62+
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [4,5,2,1] </span></p>
63+
64+
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> &quot;Tie&quot; </span></p>
65+
66+
<p><strong>Explanation: </strong> There are <code>2</code> pairs in this linked list. Let&#39;s investigate each pair individually:</p>
67+
68+
<p><code>(4,5)</code> -&gt; Since <code>4 &lt; 5</code>, the Odd team gets the point.</p>
69+
70+
<p><code>(2,1)</code> -&gt; Since <code>2 &gt; 1</code>, the Even team gets the point.</p>
71+
72+
<p>Both teams earned <code>1</code> point.</p>
73+
74+
<p>Hence, the answer would be <code>&quot;Tie&quot;</code>.</p>
75+
</div>
76+
77+
<p>&nbsp;</p>
78+
<p><strong>Constraints:</strong></p>
79+
80+
<ul>
81+
<li>The number of nodes in the list is in the range <code>[2, 100]</code>.</li>
82+
<li>The number of nodes in the list is even.</li>
83+
<li><code>1 &lt;= Node.val &lt;= 100</code></li>
84+
<li>The value of each odd-indexed node is odd.</li>
85+
<li>The value of each even-indexed node is even.</li>
86+
</ul>
87+
88+
## 解法
89+
90+
### 方法一
91+
92+
<!-- tabs:start -->
93+
94+
```python
95+
96+
```
97+
98+
```java
99+
100+
```
101+
102+
```cpp
103+
104+
```
105+
106+
```go
107+
108+
```
109+
110+
<!-- tabs:end -->
111+
112+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# [3062. Winner of the Linked List Game](https://leetcode.com/problems/winner-of-the-linked-list-game)
2+
3+
[中文文档](/solution/3000-3099/3062.Winner%20of%20the%20Linked%20List%20Game/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>You are given the <code>head</code> of a linked list of <strong>even</strong> length containing integers.</p>
10+
11+
<p>Each <strong>odd-indexed</strong> node contains an odd integer and each <strong>even-indexed</strong> node contains an even integer.</p>
12+
13+
<p>We call each even-indexed node and its next node a <strong>pair</strong>, e.g., the nodes with indices <code>0</code> and <code>1</code> are a pair, the nodes with indices <code>2</code> and <code>3</code> are a pair, and so on.</p>
14+
15+
<p>For every <strong>pair</strong>, we compare the values of the nodes in the pair:</p>
16+
17+
<ul>
18+
<li>If the odd-indexed node is higher, the <code>&quot;Odd&quot;</code> team gets a point.</li>
19+
<li>If the even-indexed node is higher, the <code>&quot;Even&quot;</code> team gets a point.</li>
20+
</ul>
21+
22+
<p>Return <em>the name of the team with the <strong>higher</strong> points, if the points are equal, return</em> <code>&quot;Tie&quot;</code>.</p>
23+
24+
<p>&nbsp;</p>
25+
<p><strong class="example">Example 1: </strong></p>
26+
27+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
28+
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [2,1] </span></p>
29+
30+
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> &quot;Even&quot; </span></p>
31+
32+
<p><strong>Explanation: </strong> There is only one pair in this linked list and that is <code>(2,1)</code>. Since <code>2 &gt; 1</code>, the Even team gets the point.</p>
33+
34+
<p>Hence, the answer would be <code>&quot;Even&quot;</code>.</p>
35+
</div>
36+
37+
<p><strong class="example">Example 2: </strong></p>
38+
39+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
40+
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [2,5,4,7,20,5] </span></p>
41+
42+
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> &quot;Odd&quot; </span></p>
43+
44+
<p><strong>Explanation: </strong> There are <code>3</code> pairs in this linked list. Let&#39;s investigate each pair individually:</p>
45+
46+
<p><code>(2,5)</code> -&gt; Since <code>2 &lt; 5</code>, The Odd team gets the point.</p>
47+
48+
<p><code>(4,7)</code> -&gt; Since <code>4 &lt; 7</code>, The Odd team gets the point.</p>
49+
50+
<p><code>(20,5)</code> -&gt; Since <code>20 &gt; 5</code>, The Even team gets the point.</p>
51+
52+
<p>The Odd team earned <code>2</code> points while the Even team got <code>1</code> point and the Odd team has the higher points.</p>
53+
54+
<p>Hence, the answer would be <code>&quot;Odd&quot;</code>.</p>
55+
</div>
56+
57+
<p><strong class="example">Example 3: </strong></p>
58+
59+
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
60+
<p><strong>Input: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> head = [4,5,2,1] </span></p>
61+
62+
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> &quot;Tie&quot; </span></p>
63+
64+
<p><strong>Explanation: </strong> There are <code>2</code> pairs in this linked list. Let&#39;s investigate each pair individually:</p>
65+
66+
<p><code>(4,5)</code> -&gt; Since <code>4 &lt; 5</code>, the Odd team gets the point.</p>
67+
68+
<p><code>(2,1)</code> -&gt; Since <code>2 &gt; 1</code>, the Even team gets the point.</p>
69+
70+
<p>Both teams earned <code>1</code> point.</p>
71+
72+
<p>Hence, the answer would be <code>&quot;Tie&quot;</code>.</p>
73+
</div>
74+
75+
<p>&nbsp;</p>
76+
<p><strong>Constraints:</strong></p>
77+
78+
<ul>
79+
<li>The number of nodes in the list is in the range <code>[2, 100]</code>.</li>
80+
<li>The number of nodes in the list is even.</li>
81+
<li><code>1 &lt;= Node.val &lt;= 100</code></li>
82+
<li>The value of each odd-indexed node is odd.</li>
83+
<li>The value of each even-indexed node is even.</li>
84+
</ul>
85+
86+
## Solutions
87+
88+
### Solution 1
89+
90+
<!-- tabs:start -->
91+
92+
```python
93+
94+
```
95+
96+
```java
97+
98+
```
99+
100+
```cpp
101+
102+
```
103+
104+
```go
105+
106+
```
107+
108+
<!-- tabs:end -->
109+
110+
<!-- end -->

0 commit comments

Comments
 (0)