题目链接
分析
排序后枚举楼层间隔的最大值即可
代码实现
class Solution {
public:
int maxConsecutive(int bottom, int top, vector<int>& special) {
ranges::sort(special);
int ans = max(*special.begin() - bottom, top - *special.rbegin());
for(int i = 1; i < special.size(); ++i) {
ans = max(ans, special[i] - special[i - 1] - 1);
}
return ans;
}
};
复杂度分析
- 时间复杂度:$O(n \log n)$,主要为排序时间
- 空间复杂度:$O(1)$