I'm not sure if that's what you meant, but my answer is aimed to achieve the roof with the maximal peek height, and not the maximal average height.
The maximal peek height in this problem is determined by the maximal square that can fit into your structure.
So to find it, you just need to look for the maximal square that can fit in and perform a simple pyramid height calculation. For example, if your found a square with an edge of a
, and you are constructing the roof with a 45 degrees angle from the base as you mentioned, then: Peek = sqrt(3)*a
.
Finding the maximal square shouldn't be a complicated task: for each corner in the structure, go in every direction (up, down, left, right) on a straight line until you go out of the structure (assume we obtained these values up, down, left, right
), the maximal square that can be constructed from a corner is the maximum value between min{up, left}, min{up, right}, min{down, left}, min{down, right}
. and the maximal square is the maximum value obtained from any corner.
Now construct a pyramid where from the corner with the maximal value. in the rest of the structure you can do whatever you want, as it won't surpass the height of this pyramid.