Skip to content

Commit 8cbbbde

Browse files
committed
feat: add new lc problems:2015~2019
1 parent 12ccea1 commit 8cbbbde

File tree

23 files changed

+1776
-824
lines changed

23 files changed

+1776
-824
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [2015. Average Height of Buildings in Each Segment](https://leetcode-cn.com/problems/average-height-of-buildings-in-each-segment)
2+
3+
[English Version](/solution/2000-2099/2015.Average%20Height%20of%20Buildings%20in%20Each%20Segment/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>A perfectly straight street is represented by a number line. The street has building(s) on it and is represented by a 2D integer array <code>buildings</code>, where <code>buildings[i] = [start<sub>i</sub>, end<sub>i</sub>, height<sub>i</sub>]</code>. This means that there is a building with <code>height<sub>i</sub></code> in the <strong>half-closed segment</strong> <code>[start<sub>i</sub>, end<sub>i</sub>)</code>.</p>
10+
11+
<p>You want to <strong>describe</strong> the heights of the buildings on the street with the <strong>minimum</strong> number of non-overlapping <strong>segments</strong>. The street can be represented by the 2D integer array <code>street</code> where <code>street[j] = [left<sub>j</sub>, right<sub>j</sub>, average<sub>j</sub>]</code> describes a <strong>half-closed segment</strong> <code>[left<sub>j</sub>, right<sub>j</sub>)</code> of the road where the <strong>average</strong> heights of the buildings in the<strong> segment</strong> is <code>average<sub>j</sub></code>.</p>
12+
13+
<ul>
14+
<li>For example, if <code>buildings = [[1,5,2],[3,10,4]],</code> the street could be represented by <code>street = [[1,3,2],[3,5,3],[5,10,4]]</code> because:
15+
<ul>
16+
<li>From 1 to 3, there is only the first building with an average height of <code>2 / 1 = 2</code>.</li>
17+
<li>From 3 to 5, both the first and the second building are there with an average height of <code>(2+4) / 2 = 3</code>.</li>
18+
<li>From 5 to 10, there is only the second building with an average height of <code>4 / 1 = 4</code>.</li>
19+
</ul>
20+
</li>
21+
</ul>
22+
23+
<p>Given <code>buildings</code>, return <em>the 2D integer array </em><code>street</code><em> as described above (<strong>excluding</strong> any areas of the street where there are no buldings). You may return the array in <strong>any order</strong></em>.</p>
24+
25+
<p>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</p>
26+
27+
<p>A <strong>half-closed segment</strong> <code>[a, b)</code> is the section of the number line between points <code>a</code> and <code>b</code> <strong>including</strong> point <code>a</code> and <strong>not including</strong> point <code>b</code>.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Example 1:</strong></p>
31+
<img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/2000-2099/2015.Average%20Height%20of%20Buildings%20in%20Each%20Segment/images/image-20210921224001-2.png" style="width: 500px; height: 349px;" />
32+
<pre>
33+
<strong>Input:</strong> buildings = [[1,5,2],[3,10,4]]
34+
<strong>Output:</strong> [[1,3,2],[3,5,3],[5,10,4]]
35+
<strong>Explanation:</strong>
36+
From 1 to 3, there is only the first building with an average height of 2 / 1 = 2.
37+
From 3 to 5, both the first and the second building are there with an average height of (2+4) / 2 = 3.
38+
From 5 to 10, there is only the second building with an average height of 4 / 1 = 4.
39+
</pre>
40+
41+
<p><strong>Example 2:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> buildings = [[1,3,2],[2,5,3],[2,8,3]]
45+
<strong>Output:</strong> [[1,3,2],[3,8,3]]
46+
<strong>Explanation:</strong>
47+
From 1 to 2, there is only the first building with an average height of 2 / 1 = 2.
48+
From 2 to 3, all three buildings are there with an average height of (2+3+3) / 3 = 2.
49+
From 3 to 5, both the second and the third building are there with an average height of (3+3) / 2 = 3.
50+
From 5 to 8, there is only the last building with an average height of 3 / 1 = 3.
51+
The average height from 1 to 3 is the same so we can group them into one segment.
52+
The average height from 3 to 8 is the same so we can group them into one segment.
53+
</pre>
54+
55+
<p><strong>Example 3:</strong></p>
56+
57+
<pre>
58+
<strong>Input:</strong> buildings = [[1,2,1],[5,6,1]]
59+
<strong>Output:</strong> [[1,2,1],[5,6,1]]
60+
<strong>Explanation:</strong>
61+
From 1 to 2, there is only the first building with an average height of 1 / 1 = 1.
62+
From 2 to 5, there are no buildings, so it is not included in the output.
63+
From 5 to 6, there is only the second building with an average height of 1 / 1 = 1.
64+
We cannot group the segments together because an empty space with no buildings seperates the segments.
65+
</pre>
66+
67+
<p>&nbsp;</p>
68+
<p><strong>Constraints:</strong></p>
69+
70+
<ul>
71+
<li><code>1 &lt;= buildings.length &lt;= 10<sup>5</sup></code></li>
72+
<li><code>buildings[i].length == 3</code></li>
73+
<li><code>0 &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 10<sup>8</sup></code></li>
74+
<li><code>1 &lt;= height<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
75+
</ul>
76+
77+
78+
## 解法
79+
80+
<!-- 这里可写通用的实现逻辑 -->
81+
82+
<!-- tabs:start -->
83+
84+
### **Python3**
85+
86+
<!-- 这里可写当前语言的特殊实现逻辑 -->
87+
88+
```python
89+
90+
```
91+
92+
### **Java**
93+
94+
<!-- 这里可写当前语言的特殊实现逻辑 -->
95+
96+
```java
97+
98+
```
99+
100+
### **...**
101+
102+
```
103+
104+
```
105+
106+
<!-- tabs:end -->
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [2015. Average Height of Buildings in Each Segment](https://leetcode.com/problems/average-height-of-buildings-in-each-segment)
2+
3+
[中文文档](/solution/2000-2099/2015.Average%20Height%20of%20Buildings%20in%20Each%20Segment/README.md)
4+
5+
## Description
6+
7+
<p>A perfectly straight street is represented by a number line. The street has building(s) on it and is represented by a 2D integer array <code>buildings</code>, where <code>buildings[i] = [start<sub>i</sub>, end<sub>i</sub>, height<sub>i</sub>]</code>. This means that there is a building with <code>height<sub>i</sub></code> in the <strong>half-closed segment</strong> <code>[start<sub>i</sub>, end<sub>i</sub>)</code>.</p>
8+
9+
<p>You want to <strong>describe</strong> the heights of the buildings on the street with the <strong>minimum</strong> number of non-overlapping <strong>segments</strong>. The street can be represented by the 2D integer array <code>street</code> where <code>street[j] = [left<sub>j</sub>, right<sub>j</sub>, average<sub>j</sub>]</code> describes a <strong>half-closed segment</strong> <code>[left<sub>j</sub>, right<sub>j</sub>)</code> of the road where the <strong>average</strong> heights of the buildings in the<strong> segment</strong> is <code>average<sub>j</sub></code>.</p>
10+
11+
<ul>
12+
<li>For example, if <code>buildings = [[1,5,2],[3,10,4]],</code> the street could be represented by <code>street = [[1,3,2],[3,5,3],[5,10,4]]</code> because:
13+
<ul>
14+
<li>From 1 to 3, there is only the first building with an average height of <code>2 / 1 = 2</code>.</li>
15+
<li>From 3 to 5, both the first and the second building are there with an average height of <code>(2+4) / 2 = 3</code>.</li>
16+
<li>From 5 to 10, there is only the second building with an average height of <code>4 / 1 = 4</code>.</li>
17+
</ul>
18+
</li>
19+
</ul>
20+
21+
<p>Given <code>buildings</code>, return <em>the 2D integer array </em><code>street</code><em> as described above (<strong>excluding</strong> any areas of the street where there are no buldings). You may return the array in <strong>any order</strong></em>.</p>
22+
23+
<p>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</p>
24+
25+
<p>A <strong>half-closed segment</strong> <code>[a, b)</code> is the section of the number line between points <code>a</code> and <code>b</code> <strong>including</strong> point <code>a</code> and <strong>not including</strong> point <code>b</code>.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Example 1:</strong></p>
29+
<img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/2000-2099/2015.Average%20Height%20of%20Buildings%20in%20Each%20Segment/images/image-20210921224001-2.png" style="width: 500px; height: 349px;" />
30+
<pre>
31+
<strong>Input:</strong> buildings = [[1,5,2],[3,10,4]]
32+
<strong>Output:</strong> [[1,3,2],[3,5,3],[5,10,4]]
33+
<strong>Explanation:</strong>
34+
From 1 to 3, there is only the first building with an average height of 2 / 1 = 2.
35+
From 3 to 5, both the first and the second building are there with an average height of (2+4) / 2 = 3.
36+
From 5 to 10, there is only the second building with an average height of 4 / 1 = 4.
37+
</pre>
38+
39+
<p><strong>Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> buildings = [[1,3,2],[2,5,3],[2,8,3]]
43+
<strong>Output:</strong> [[1,3,2],[3,8,3]]
44+
<strong>Explanation:</strong>
45+
From 1 to 2, there is only the first building with an average height of 2 / 1 = 2.
46+
From 2 to 3, all three buildings are there with an average height of (2+3+3) / 3 = 2.
47+
From 3 to 5, both the second and the third building are there with an average height of (3+3) / 2 = 3.
48+
From 5 to 8, there is only the last building with an average height of 3 / 1 = 3.
49+
The average height from 1 to 3 is the same so we can group them into one segment.
50+
The average height from 3 to 8 is the same so we can group them into one segment.
51+
</pre>
52+
53+
<p><strong>Example 3:</strong></p>
54+
55+
<pre>
56+
<strong>Input:</strong> buildings = [[1,2,1],[5,6,1]]
57+
<strong>Output:</strong> [[1,2,1],[5,6,1]]
58+
<strong>Explanation:</strong>
59+
From 1 to 2, there is only the first building with an average height of 1 / 1 = 1.
60+
From 2 to 5, there are no buildings, so it is not included in the output.
61+
From 5 to 6, there is only the second building with an average height of 1 / 1 = 1.
62+
We cannot group the segments together because an empty space with no buildings seperates the segments.
63+
</pre>
64+
65+
<p>&nbsp;</p>
66+
<p><strong>Constraints:</strong></p>
67+
68+
<ul>
69+
<li><code>1 &lt;= buildings.length &lt;= 10<sup>5</sup></code></li>
70+
<li><code>buildings[i].length == 3</code></li>
71+
<li><code>0 &lt;= start<sub>i</sub> &lt; end<sub>i</sub> &lt;= 10<sup>8</sup></code></li>
72+
<li><code>1 &lt;= height<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
73+
</ul>
74+
75+
## Solutions
76+
77+
<!-- tabs:start -->
78+
79+
### **Python3**
80+
81+
```python
82+
83+
```
84+
85+
### **Java**
86+
87+
```java
88+
89+
```
90+
91+
### **...**
92+
93+
```
94+
95+
```
96+
97+
<!-- tabs:end -->
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [2016. 增量元素之间的最大差值](https://leetcode-cn.com/problems/maximum-difference-between-increasing-elements)
2+
3+
[English Version](/solution/2000-2099/2016.Maximum%20Difference%20Between%20Increasing%20Elements/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,该数组的大小为 <code>n</code> ,请你计算 <code>nums[j] - nums[i]</code> 能求得的 <strong>最大差值 </strong>,其中 <code>0 &lt;= i &lt; j &lt; n</code> 且 <code>nums[i] &lt; nums[j]</code> 。</p>
10+
11+
<p>返回 <strong>最大差值</strong> 。如果不存在满足要求的 <code>i</code> 和 <code>j</code> ,返回 <code>-1</code> 。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><strong>输入:</strong>nums = [7,<em><strong>1</strong></em>,<em><strong>5</strong></em>,4]
18+
<strong>输出:</strong>4
19+
<strong>解释:</strong>
20+
最大差值出现在 i = 1 且 j = 2 时,nums[j] - nums[i] = 5 - 1 = 4 。
21+
注意,尽管 i = 1 且 j = 0 时 ,nums[j] - nums[i] = 7 - 1 = 6 &gt; 4 ,但 i &gt; j 不满足题面要求,所以 6 不是有效的答案。
22+
</pre>
23+
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre><strong>输入:</strong>nums = [9,4,3,2]
27+
<strong>输出:</strong>-1
28+
<strong>解释:</strong>
29+
不存在同时满足 i &lt; j 和 nums[i] &lt; nums[j] 这两个条件的 i, j 组合。
30+
</pre>
31+
32+
<p><strong>示例 3:</strong></p>
33+
34+
<pre><strong>输入:</strong>nums = [<em><strong>1</strong></em>,5,2,<em><strong>10</strong></em>]
35+
<strong>输出:</strong>9
36+
<strong>解释:</strong>
37+
最大差值出现在 i = 0 且 j = 3 时,nums[j] - nums[i] = 10 - 1 = 9 。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>n == nums.length</code></li>
46+
<li><code>2 &lt;= n &lt;= 1000</code></li>
47+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
48+
</ul>
49+
50+
## 解法
51+
52+
<!-- 这里可写通用的实现逻辑 -->
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
<!-- 这里可写当前语言的特殊实现逻辑 -->
59+
60+
```python
61+
62+
```
63+
64+
### **Java**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```java
69+
70+
```
71+
72+
### **...**
73+
74+
```
75+
76+
```
77+
78+
<!-- tabs:end -->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [2016. Maximum Difference Between Increasing Elements](https://leetcode.com/problems/maximum-difference-between-increasing-elements)
2+
3+
[中文文档](/solution/2000-2099/2016.Maximum%20Difference%20Between%20Increasing%20Elements/README.md)
4+
5+
## Description
6+
7+
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, find the <strong>maximum difference</strong> between <code>nums[i]</code> and <code>nums[j]</code> (i.e., <code>nums[j] - nums[i]</code>), such that <code>0 &lt;= i &lt; j &lt; n</code> and <code>nums[i] &lt; nums[j]</code>.</p>
8+
9+
<p>Return <em>the <strong>maximum difference</strong>. </em>If no such <code>i</code> and <code>j</code> exists, return <code>-1</code>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [7,<strong><u>1</u></strong>,<strong><u>5</u></strong>,4]
16+
<strong>Output:</strong> 4
17+
<strong>Explanation:</strong>
18+
The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4.
19+
Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i &gt; j, so it is not valid.
20+
</pre>
21+
22+
<p><strong>Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [9,4,3,2]
26+
<strong>Output:</strong> -1
27+
<strong>Explanation:</strong>
28+
There is no i and j such that i &lt; j and nums[i] &lt; nums[j].
29+
</pre>
30+
31+
<p><strong>Example 3:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> nums = [<strong><u>1</u></strong>,5,2,<strong><u>10</u></strong>]
35+
<strong>Output:</strong> 9
36+
<strong>Explanation:</strong>
37+
The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>n == nums.length</code></li>
45+
<li><code>2 &lt;= n &lt;= 1000</code></li>
46+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
57+
```
58+
59+
### **Java**
60+
61+
```java
62+
63+
```
64+
65+
### **...**
66+
67+
```
68+
69+
```
70+
71+
<!-- tabs:end -->

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy