Maximum Consecutive Floors Without Special Floors

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
        special.sort()  # Sorting the special floors to easily iterate through them
        max_consecutive = 0
        current_floor = bottom

        for floor in special:
            if floor < bottom:
                continue
            if floor > top:
                break

            # Calculate the consecutive floors without a special floor
            consecutive = floor - current_floor
            if consecutive > max_consecutive:
                max_consecutive = consecutive

            current_floor = floor + 1

        # Consider the remaining floors from the last special floor to the top floor
        remaining_consecutive = top - current_floor + 1
        if remaining_consecutive > max_consecutive:
            max_consecutive = remaining_consecutive

        return max_consecutive