Buildings stand in a row from left to right. Every building has a unique height.
From the rooftop of a building, a later building is visible if every building between them is shorter than both buildings.
For each building, count how many later buildings are visible from its rooftop.
BuildingVisibilityAnalyzer
List<Integer> countVisibleBuildings( List<Integer> buildingHeights )
buildingHeights: The unique heights of the buildings in their left-to-right order.Return a list where the value at index i is the number of later buildings visible from building i.
0.i can only see a building j when i < j.i and j must be shorter than both buildingHeights[i] and buildingHeights[j]. 1 ≤ buildingHeights.size() ≤ 100,000 1 ≤ buildingHeights[i] ≤ 100,000 buildingHeights are unique. countVisibleBuildings( buildingHeights = List.of(12, 5, 10, 3, 8, 14, 7) )
Output: List.of(3, 1, 3, 1, 1, 1, 0)
Building 0, with height 12, can see buildings 1, 2, and 5.
Building 2, with height 10, can see buildings 3, 4, and 5.
countVisibleBuildings( buildingHeights = List.of(4, 11, 7, 9, 2) )
Output: List.of(1, 2, 1, 1, 0)
Building 1, with height 11, can see buildings 2 and 3. The building between them has height 7, which is shorter than both 11 and 9.
countVisibleBuildings( buildingHeights = List.of(18) )
Output: List.of(0)
There is only one building, so there are no later buildings to see.