2274. Maximum Consecutive Floors Without Special Floors LeetCode Solution

In this guide, you will get 2274. Maximum Consecutive Floors Without Special Floors LeetCode Solution with the best time and space complexity. The solution to Maximum Consecutive Floors Without Special Floors problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Consecutive Floors Without Special Floors solution in C++
  4. Maximum Consecutive Floors Without Special Floors solution in Java
  5. Maximum Consecutive Floors Without Special Floors solution in Python
  6. Additional Resources
2274. Maximum Consecutive Floors Without Special Floors LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: bottom = 2, top = 9, special = [4,6]
Output: 3
Explanation: The following are the ranges (inclusive) of consecutive floors without a special floor:
– (2, 3) with a total amount of 2 floors.
– (5, 5) with a total amount of 1 floor.
– (7, 9) with a total amount of 3 floors.
Therefore, we return the maximum number which is 3 floors.

Example 2:

Input: bottom = 6, top = 8, special = [7,6,8]
Output: 0
Explanation: Every floor rented is a special floor, so we return 0.

Constraints:

1 <= special.length <= 105
1 <= bottom <= special[i] <= top <= 109
All the values of special are unique.

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(1)

2274. Maximum Consecutive Floors Without Special Floors LeetCode Solution in C++

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()});
  }
};
/* code provided by PROGIEZ */

2274. Maximum Consecutive Floors Without Special Floors LeetCode Solution in Java

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]));
  }
}
// code provided by PROGIEZ

2274. Maximum Consecutive Floors Without Special Floors LeetCode Solution in Python

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])
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.