Leetcode 2274: Maximum Consecutive Floors Without Special Floors

grid47
grid47
Exploring patterns and algorithms
Mar 24, 2024 5 min read

Alice has rented a sequence of floors in a building from bottom to top (inclusive). Some of these floors are designated as special floors where relaxation occurs. You are given an array special that contains the indices of these special floors. Your task is to determine the maximum number of consecutive floors that are not designated as special.
Problem
Approach
Steps
Complexity
Input: You are given two integers, `bottom` and `top`, representing the range of floors rented. You are also given an integer array `special` where each element represents a special floor within this range.
Example: Input: bottom = 3, top = 10, special = [4, 5, 8]
Constraints:
• 1 <= special.length <= 10^5
• 1 <= bottom <= special[i] <= top <= 10^9
• All the values in the special array are unique.
Output: Return the maximum number of consecutive floors that do not have a special floor.
Example: Output: 3
Constraints:
Goal: Find the longest stretch of consecutive floors without any special floor in the given range.
Steps:
• Sort the special floors in ascending order.
• Track the difference between each adjacent pair of special floors and find the maximum gap.
• Include the gap before the first special floor and after the last special floor.
• Return the maximum gap.
Goal: Ensure that the solution works efficiently within the given constraints.
Steps:
• The solution should efficiently handle large inputs, including up to 100,000 special floors.
Assumptions:
• The special floors are within the range from `bottom` to `top`.
• There are no repeated special floors in the input array.
Input: Input: bottom = 3, top = 10, special = [4, 5, 8]
Explanation: The special floors are 4, 5, and 8. The largest gap is between 7 and 10, which contains 3 consecutive floors. Therefore, the result is 3.

Input: Input: bottom = 6, top = 8, special = [6, 7, 8]
Explanation: All floors are special, so there are no consecutive floors without a special floor. The result is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus