Skip to content

2274. Maximum Consecutive Floors Without Special Floors 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  int maxConsecutive(int bottom, int top, vector<int>& special) {
    int ans = 0;

    ranges::sort(special);

    for (int i = 1; i < special.size(); ++i)
      ans = max(ans, special[i] - special[i - 1] - 1);

    return max({ans, special.front() - bottom, top - special.back()});
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
  public int maxConsecutive(int bottom, int top, int[] special) {
    int ans = 0;

    Arrays.sort(special);

    for (int i = 1; i < special.length; ++i)
      ans = Math.max(ans, special[i] - special[i - 1] - 1);

    return Math.max(ans, Math.max(special[0] - bottom, top - special[special.length - 1]));
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
    ans = 0

    special.sort()

    for a, b in zip(special, special[1:]):
      ans = max(ans, b - a - 1)

    return max(ans, special[0] - bottom, top - special[-1])