Skip to content

Commit 226ccba

Browse files
authored
Merge pull request #692 from LetMeFly666/2274
添加问题“2274.不含特殊楼层的最大连续楼层数”的代码和题解
2 parents ef8d500 + ed0db94 commit 226ccba

12 files changed

+250
-11
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-06 20:32:56
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-06 20:35:34
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maxConsecutive(int bottom, int top, vector<int>& special) {
14+
sort(special.begin(), special.end());
15+
int ans = top - special.back();
16+
bottom--;
17+
for (int t : special) {
18+
ans = max(ans, t - bottom - 1);
19+
bottom = t;
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-06 20:41:20
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-06 20:43:19
6+
*/
7+
package main
8+
import "sort"
9+
10+
func max_MCFWSF(a, b int) int {
11+
if a > b {
12+
return a
13+
}
14+
return b
15+
}
16+
17+
func maxConsecutive(bottom int, top int, special []int) (ans int) {
18+
sort.Ints(special)
19+
ans = max_MCFWSF(special[0] - bottom, top - special[len(special) - 1])
20+
for i := 1; i < len(special); i++ {
21+
ans = max_MCFWSF(ans, special[i] - special[i - 1] - 1)
22+
}
23+
return
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-01-06 20:39:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-01-06 20:40:24
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
public int maxConsecutive(int bottom, int top, int[] special) {
11+
Arrays.sort(special);
12+
int ans = Math.max(special[0] - bottom, top - special[special.length - 1]);
13+
for (int i = 1; i < special.length; i++) {
14+
ans = Math.max(ans, special[i] - special[i - 1] - 1);
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-01-06 20:36:27
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-01-06 20:38:18
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
11+
special.sort()
12+
ans = max(special[0] - bottom, top - special[-1])
13+
for i in range(1, len(special)):
14+
ans = max(ans, special[i] - special[i - 1] - 1)
15+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@
538538
|2244.完成所有任务需要的最少轮数|中等|<a href="https://leetcode.cn/problems/minimum-rounds-to-complete-all-tasks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/14/LeetCode%202244.%E5%AE%8C%E6%88%90%E6%89%80%E6%9C%89%E4%BB%BB%E5%8A%A1%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E5%B0%91%E8%BD%AE%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138849002" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-rounds-to-complete-all-tasks/solutions/2777174/letmefly-2244wan-cheng-suo-you-ren-wu-xu-n7bh/" target="_blank">LeetCode题解</a>|
539539
|2251.花期内花的数目|困难|<a href="https://leetcode.cn/problems/number-of-flowers-in-full-bloom/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/09/28/LeetCode%202251.%E8%8A%B1%E6%9C%9F%E5%86%85%E8%8A%B1%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133378624" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-flowers-in-full-bloom/solutions/2462220/letmefly-2251hua-qi-nei-hua-de-shu-mu-pa-nfvo/" target="_blank">LeetCode题解</a>|
540540
|2258.逃离火灾|困难|<a href="https://leetcode.cn/problems/escape-the-spreading-fire/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/09/LeetCode%202258.%E9%80%83%E7%A6%BB%E7%81%AB%E7%81%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134331955" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/escape-the-spreading-fire/solutions/2520786/letmefly-2258tao-chi-huo-zai-yan-du-you-kj2r4/" target="_blank">LeetCode题解</a>|
541+
|2274.不含特殊楼层的最大连续楼层数|中等|<a href="https://leetcode.cn/problems/maximum-consecutive-floors-without-special-floors/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/06/LeetCode%202274.%E4%B8%8D%E5%90%AB%E7%89%B9%E6%AE%8A%E6%A5%BC%E5%B1%82%E7%9A%84%E6%9C%80%E5%A4%A7%E8%BF%9E%E7%BB%AD%E6%A5%BC%E5%B1%82%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144972086" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-consecutive-floors-without-special-floors/solutions/3040396/letmefly-2274bu-han-te-shu-lou-ceng-de-z-321m/" target="_blank">LeetCode题解</a>|
541542
|2276.统计区间中的整数数目|困难|<a href="https://leetcode.cn/problems/count-integers-in-intervals/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/16/LeetCode%202276.%E7%BB%9F%E8%AE%A1%E5%8C%BA%E9%97%B4%E4%B8%AD%E7%9A%84%E6%95%B4%E6%95%B0%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135036679" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-integers-in-intervals/solutions/2568803/letmefly-2276tong-ji-qu-jian-zhong-de-zh-y3i6/" target="_blank">LeetCode题解</a>|
542543
|2283.判断一个数的数字计数是否等于数位的值|简单|<a href="https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/11/LeetCode%202283.%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E7%9A%84%E6%95%B0%E5%AD%97%E8%AE%A1%E6%95%B0%E6%98%AF%E5%90%A6%E7%AD%89%E4%BA%8E%E6%95%B0%E4%BD%8D%E7%9A%84%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128652351" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/solutions/2057234/letmefly-2283pan-duan-yi-ge-shu-de-shu-z-4olo/" target="_blank">LeetCode题解</a>|
543544
|2287.重排字符形成目标字符串|简单|<a href="https://leetcode.cn/problems/rearrange-characters-to-make-target-string/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/01/13/LeetCode%202287.%E9%87%8D%E6%8E%92%E5%AD%97%E7%AC%A6%E5%BD%A2%E6%88%90%E7%9B%AE%E6%A0%87%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128675652" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/rearrange-characters-to-make-target-string/solutions/2060137/letmefly-2287zhong-pai-zi-fu-xing-cheng-32wj7/" target="_blank">LeetCode题解</a>|
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
title: 2274.不含特殊楼层的最大连续楼层数
3+
date: 2025-01-06 20:45:47
4+
tags: [题解, LeetCode, 中等, 数组, 排序]
5+
---
6+
7+
# 【LetMeFly】2274.不含特殊楼层的最大连续楼层数:排序
8+
9+
力扣题目链接:[https://leetcode.cn/problems/maximum-consecutive-floors-without-special-floors/](https://leetcode.cn/problems/maximum-consecutive-floors-without-special-floors/)
10+
11+
<p>Alice 管理着一家公司,并租用大楼的部分楼层作为办公空间。Alice 决定将一些楼层作为 <strong>特殊楼层</strong> ,仅用于放松。</p>
12+
13+
<p>给你两个整数 <code>bottom</code> 和 <code>top</code> ,表示 Alice 租用了从 <code>bottom</code> 到 <code>top</code>(含 <code>bottom</code> 和 <code>top</code> 在内)的所有楼层。另给你一个整数数组 <code>special</code> ,其中 <code>special[i]</code> 表示&nbsp; Alice 指定用于放松的特殊楼层。</p>
14+
15+
<p>返回不含特殊楼层的 <strong>最大</strong> 连续楼层数。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>bottom = 2, top = 9, special = [4,6]
23+
<strong>输出:</strong>3
24+
<strong>解释:</strong>下面列出的是不含特殊楼层的连续楼层范围:
25+
- (2, 3) ,楼层数为 2 。
26+
- (5, 5) ,楼层数为 1 。
27+
- (7, 9) ,楼层数为 3 。
28+
因此,返回最大连续楼层数 3 。
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>bottom = 6, top = 8, special = [7,6,8]
35+
<strong>输出:</strong>0
36+
<strong>解释:</strong>每层楼都被规划为特殊楼层,所以返回 0 。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= special.length &lt;= 10<sup>5</sup></code></li>
45+
<li><code>1 &lt;= bottom &lt;= special[i] &lt;= top &lt;= 10<sup>9</sup></code></li>
46+
<li><code>special</code> 中的所有值 <strong>互不相同</strong></li>
47+
</ul>
48+
49+
50+
51+
## 解题方法:排序
52+
53+
对“特殊楼层”从小到大排序,统计如下三者的最大值即为答案:
54+
55+
1. $special[0] - bottom$
56+
2. $special[i] - special[i - 1] - 1$,其中$1\leq i\lt len(special)$
57+
3. $top - special[len(special) - 1]$
58+
59+
+ 时间复杂度$O(n\log n)$,其中$n=len(special)$
60+
+ 空间复杂度$O(\log n)$
61+
62+
### AC代码
63+
64+
#### Python
65+
66+
```python
67+
'''
68+
Author: LetMeFly
69+
Date: 2025-01-06 20:36:27
70+
LastEditors: LetMeFly.xyz
71+
LastEditTime: 2025-01-06 20:38:18
72+
'''
73+
from typing import List
74+
75+
class Solution:
76+
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
77+
special.sort()
78+
ans = max(special[0] - bottom, top - special[-1])
79+
for i in range(1, len(special)):
80+
ans = max(ans, special[i] - special[i - 1] - 1)
81+
return ans
82+
```
83+
84+
#### C++
85+
86+
```cpp
87+
/*
88+
* @Author: LetMeFly
89+
* @Date: 2025-01-06 20:32:56
90+
* @LastEditors: LetMeFly.xyz
91+
* @LastEditTime: 2025-01-06 20:35:34
92+
*/
93+
class Solution {
94+
public:
95+
int maxConsecutive(int bottom, int top, vector<int>& special) {
96+
sort(special.begin(), special.end());
97+
int ans = top - special.back();
98+
bottom--;
99+
for (int t : special) {
100+
ans = max(ans, t - bottom - 1);
101+
bottom = t;
102+
}
103+
return ans;
104+
}
105+
};
106+
```
107+
108+
#### Java
109+
110+
```java
111+
/*
112+
* @Author: LetMeFly
113+
* @Date: 2025-01-06 20:39:00
114+
* @LastEditors: LetMeFly.xyz
115+
* @LastEditTime: 2025-01-06 20:40:24
116+
*/
117+
import java.util.Arrays;
118+
119+
class Solution {
120+
public int maxConsecutive(int bottom, int top, int[] special) {
121+
Arrays.sort(special);
122+
int ans = Math.max(special[0] - bottom, top - special[special.length - 1]);
123+
for (int i = 1; i < special.length; i++) {
124+
ans = Math.max(ans, special[i] - special[i - 1] - 1);
125+
}
126+
return ans;
127+
}
128+
}
129+
```
130+
131+
#### Go
132+
133+
```go
134+
/*
135+
* @Author: LetMeFly
136+
* @Date: 2025-01-06 20:41:20
137+
* @LastEditors: LetMeFly.xyz
138+
* @LastEditTime: 2025-01-06 20:43:19
139+
*/
140+
package main
141+
import "sort"
142+
143+
func max_MCFWSF(a, b int) int {
144+
if a > b {
145+
return a
146+
}
147+
return b
148+
}
149+
150+
func maxConsecutive(bottom int, top int, special []int) (ans int) {
151+
sort.Ints(special)
152+
ans = max_MCFWSF(special[0] - bottom, top - special[len(special) - 1])
153+
for i := 1; i < len(special); i++ {
154+
ans = max_MCFWSF(ans, special[i] - special[i - 1] - 1)
155+
}
156+
return
157+
}
158+
```
159+
160+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/01/06/LeetCode%202274.%E4%B8%8D%E5%90%AB%E7%89%B9%E6%AE%8A%E6%A5%BC%E5%B1%82%E7%9A%84%E6%9C%80%E5%A4%A7%E8%BF%9E%E7%BB%AD%E6%A5%BC%E5%B1%82%E6%95%B0/)哦~
161+
>
162+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/144972086](https://letmefly.blog.csdn.net/article/details/144972086)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,9 @@ tags: [其他, 知识, 英语, Notes]
924924
|torment|n. 痛苦,折磨,苦难之源<br/>v. 折磨,使痛苦|
925925
|procurement|n. (政府/机构的)采购|
926926
|righteous|ajd. 公正的,正直的,正当的,公平合理的|
927+
|||
928+
|snore|n. 鼾声<br/>v. 打鼾|
929+
|lark|n. 云雀,百灵鸟,玩乐,嬉戏<br/>v. 捉云雀,嬉戏,骑马越野,愚弄|
927930

928931
<p class="wordCounts">单词收录总数</p>
929932

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,8 @@ xx型
11361136
|彼わ背が低いです。<br/>他个子很低。|
11371137
|昨日、ワンさんが死にました。<br/>昨天,小王死了。|
11381138
|このから耳が痛くなりました。<br/>昨天耳朵开始疼。|
1139+
|若い時はたくさん旅行しました。<br/>年轻的时候,旅行了很多。|
1140+
|<br/>|
11391141
|<br/>|
11401142
|<br/>|
11411143
|<br/>|

WhatCanISay.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

WhatHappened.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)