The "Beautiful Towers I interview question" is an optimization problem involving heights. You are given an array of "maximum heights" for a row of towers. You need to assign an actual height to each tower such that:
Meta and Salesforce ask the "Beautiful Towers I coding problem" to evaluate a candidate's ability to handle array traversals and optimization. While the first version of this problem allows for a simpler solution, it sets the stage for more advanced "Monotonic Stack interview pattern" techniques used in the harder variation.
For the "Easy/Medium" version, we use an Exhaustive Peak Search.
maxHeight and the height of the tower to its right.maxHeight and the height of the tower to its left.maxHeights = [5, 3, 4, 1, 1]
1, 1 (limited by 1).3, 3 (limited by 3).[3, 3, 4, 1, 1]. Sum = 12.3, 3, 1, 1 (limited by subsequent smaller values).[5, 3, 3, 1, 1]. Sum = 13.
The maximum sum is 13.maxHeight for that position.Mountain-array problems are very common. Always start by identifying the "peak." If the constraints are small, a "try every peak" strategy is acceptable. For larger constraints, you'll need to pre-calculate prefix and suffix sums using a monotonic stack.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Beautiful Towers II | Medium | Solve | |
| Buildings With an Ocean View | Medium | Solve | |
| Sum of Subarray Ranges | Medium | Solve | |
| Next Greater Element II | Medium | Solve | |
| Daily Temperatures | Medium | Solve |