Problem Description
Given a range of floors from bottom to top (inclusive) and a list of special floors within that range, determine the maximum number of consecutive floors that do not include any special floor.
Key Insights
- Sort the list of special floors.
- Compute the gap from bottom to the first special floor.
- Compute gaps between consecutive special floors (subtracting one to exclude the special floors).
- Compute the gap from the last special floor to the top.
- The answer is the largest gap computed.
Space and Time Complexity
Time Complexity: O(n log n), due to sorting the special floors array where n is the length of the array. Space Complexity: O(n), used for sorting (depending on implementation) and storing the special floors.
Solution
The algorithm starts by sorting the special floors array. The first potential gap is the distance between the bottom floor and the first special floor. For each pair of consecutive special floors, the gap is their difference minus one, since the special floors themselves are not counted. Finally, the gap from the last special floor to the top floor is considered. The maximum of these values is the answer.