LeetCode 2274: Maximum Consecutive Floors Without Special Floors Solution

Master LeetCode problem 2274 (Maximum Consecutive Floors Without Special Floors), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2274. Maximum Consecutive Floors Without Special Floors

Problem Explanation

Explanation

To solve this problem, we can iterate through each special floor and find the maximum gap between them. The maximum gap can occur at the beginning before the first special floor, in between two special floors, or at the end after the last special floor. We can calculate these gaps and return the maximum gap as the result.

  • Sort the special floors array.
  • Initialize maxGap to be the maximum of the first special floor index and the difference between the first special floor and the bottom floor.
  • Iterate through the special floors array and calculate the gap between consecutive special floors.
  • Update maxGap with the maximum of the current gap and the difference between the current special floor and the previous special floor.
  • Calculate the gap between the last special floor and the top floor.
  • Update maxGap with the maximum of the last gap and the difference between the top floor and the last special floor.

Solution Code

class Solution {
    public int maxConsecutiveFloors(int bottom, int top, int[] special) {
        Arrays.sort(special);
        int maxGap = Math.max(special[0] - bottom, 0);
        
        for (int i = 1; i < special.length; i++) {
            int gap = special[i] - special[i - 1] - 1;
            maxGap = Math.max(maxGap, gap);
        }
        
        maxGap = Math.max(maxGap, top - special[special.length - 1]);
        
        return maxGap;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2274 (Maximum Consecutive Floors Without Special Floors)?

This page provides optimized solutions for LeetCode problem 2274 (Maximum Consecutive Floors Without Special Floors) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2274 (Maximum Consecutive Floors Without Special Floors)?

The time complexity for LeetCode 2274 (Maximum Consecutive Floors Without Special Floors) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2274 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2274 in Java, C++, or Python.

Back to LeetCode Solutions