701228. Roof Top

Easy

Roof Top

Slug: roof-top-1587115621 Difficulty: Easy Id: 701228 Topic Tags: Arrays, Searching, Data Structures, Algorithms Company Tags: None

Summary

The Roof Top problem involves finding the maximum height of a rooftop that can be built given certain constraints. The key concepts involved are arrays and searching.

Detailed Explanation

The problem statement is not provided, but we can assume it's about building rooftops with a specific condition: each rooftop must have at least one window on every floor, and all windows must be of the same size. We need to find the maximum height of the rooftop that satisfies this condition.

To solve this problem, we can use a simple iterative approach. Initialize a variable max_height to 0 and iterate over the array of window sizes. For each window size, check if it's possible to build a rooftop of that height by checking if all previous windows are of the same size as the current one. If it is, update max_height with the new value. Finally, return max_height.

Here's a step-by-step breakdown of the solution:

  1. Initialize max_height to 0.
  2. Iterate over the array of window sizes.
  3. For each window size:
    • Check if all previous windows are of the same size as the current one.
    • If they are, update max_height with the new value.
  4. Return max_height.

Time complexity: O(n), where n is the number of window sizes in the array. Space complexity: O(1), as we only use a constant amount of space to store max_height.

Optimized Solutions

Java

View on GeeksforGeeks
public int maxRoofHeight(int[] windows) {
    int max_height = 0;
    for (int i = 0; i < windows.length; i++) {
        if (i == 0 || windows[i] == windows[i - 1]) {
            max_height = i + 1;
        }
    }
    return max_height;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.