Maximum Consecutive Floors Without Special Floors - Problem

Alice manages a company and has rented a contiguous range of floors in a skyscraper for office space. She has designated some of these floors as "special floors" for relaxation and recreation only.

Given two integers bottom and top representing the inclusive range of floors Alice has rented, and an integer array special where special[i] represents a floor designated for relaxation, find the maximum number of consecutive regular office floors (floors that are NOT special).

Goal: Find the largest gap between special floors to maximize consecutive workspace.

Example: If Alice rented floors 2-9 and floors [4, 6] are special, the consecutive sequences are [2,3], [5], [7,8,9] with lengths 2, 1, 3 respectively. The answer is 3.

Input & Output

example_1.py โ€” Basic case
$ Input: bottom = 2, top = 9, special = [4, 6]
โ€บ Output: 3
๐Ÿ’ก Note: The floors are [2,3,4,5,6,7,8,9] where 4 and 6 are special. The consecutive sequences of regular floors are [2,3], [5], [7,8,9] with lengths 2, 1, 3. Maximum is 3.
example_2.py โ€” No special floors
$ Input: bottom = 6, top = 8, special = []
โ€บ Output: 3
๐Ÿ’ก Note: All floors [6,7,8] are regular, so the maximum consecutive count is 3.
example_3.py โ€” All floors are special
$ Input: bottom = 1, top = 3, special = [1, 2, 3]
โ€บ Output: 0
๐Ÿ’ก Note: All floors are special, so there are no consecutive regular floors.

Visualization

Tap to expand
Maximum Consecutive Floors Algorithm23456789โ–  Regular Office Floorโ–  Special Relaxation FloorGap 1: 2 floorsGap 2: 1 floorGap 3: 3 floors (MAXIMUM!)Algorithm Steps1. Sort special floors: [4, 6]2. Calculate gaps: Before(4-2=2), Between(6-4-1=1), After(9-6=3)3. Return maximum gap: max(2, 1, 3) = 3โฐ Time: O(n log n) for sorting | ๐Ÿ’พ Space: O(1)
Understanding the Visualization
1
Sort Reserved Spots
Arrange all special floors in ascending order to easily identify gaps
2
Check Beginning Gap
Count floors from the start (bottom) to the first special floor
3
Check Middle Gaps
For each pair of consecutive special floors, count the floors between them
4
Check Ending Gap
Count floors from the last special floor to the end (top)
5
Return Maximum
The largest gap represents the maximum consecutive regular floors
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking every possible range, we only need to examine the gaps between sorted special floors, making the solution much more efficient!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting the special floors array dominates the time complexity

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using constant extra space for variables (excluding input)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค bottom โ‰ค top โ‰ค 109
  • 0 โ‰ค special.length โ‰ค 105
  • bottom โ‰ค special[i] โ‰ค top
  • All values in special are unique
  • special is given in any order (not necessarily sorted)
Asked in
Google 42 Amazon 38 Microsoft 35 Meta 28
42.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen