Leetcode Problem 2274. Maximum Consecutive Floors Without Special Floors

2274. Maximum Consecutive Floors Without Special Floors

Leetcode Solutions

Sorting and Finding Maximum Consecutive Floors

  1. Sort the special array in ascending order.
  2. Initialize maxConsecutive to 0, which will hold the maximum number of consecutive floors without a special floor.
  3. Calculate the number of consecutive floors before the first special floor and update maxConsecutive if it's greater than the current value.
  4. Iterate through the sorted special array starting from the second element.
    • For each element, calculate the difference between the current and previous special floors minus one (to exclude the special floor itself).
    • Update maxConsecutive if the calculated difference is greater than the current value.
  5. After the loop, calculate the number of consecutive floors after the last special floor and update maxConsecutive if it's greater.
  6. Return maxConsecutive as the result.
UML Thumbnail

Iterative Comparison without Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...