[LeetCode] Maximum Consecutive Floors Without Special Floors

2274. Maximum Consecutive Floors Without Special Floors

Alice manages a company and has rented some floors of a building as office space. Alice has decided some of these floors should be special floors, used for relaxation only.

You are given two integers bottom and top, which denote that Alice has rented all the floors from bottom to top (inclusive). You are also given the integer array special, where special[i] denotes a special floor that Alice has designated for relaxation.

Return the maximum number of consecutive floors without a special floor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int maxConsecutive(int bottom, int top, vector<int>& sp) {
sort(begin(sp), end(sp));
int res = 0;
for(int i = 0, l = bottom; i < sp.size(); i++) {
res = max(res, sp[i] - l);
l = sp[i] + 1;
}
res = max(res, top - sp.back());
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/15/PS/LeetCode/maximum-consecutive-floors-without-special-floors/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.