TopCoder problem "BestView" used in SRM 436 (Division I Level One , Division II Level Two)



Problem Statement

    There are several skyscrapers arranged in a row. You're interested in finding the one from which the maximal number of other skyscrapers can be seen. The i-th skyscraper can be represented as a line segment on a plane with endpoints (i, 0) and (i, heights[i]). A skyscraper can be seen from the roof of another skyscraper if a line segment connecting their roofs does not intersect with or touch any other skyscraper. Return the maximal number of other skyscrapers that can be seen from the roof of some skyscraper.
 

Definition

    
Class:BestView
Method:numberOfBuildings
Parameters:int[]
Returns:int
Method signature:int numberOfBuildings(int[] heights)
(be sure your method is public)
    
 

Constraints

-heights will contain between 1 and 50 elements, inclusive.

-Each element of heights will be between 1 and 1,000,000,000, inclusive.

 

Examples

0)
    
{10}
Returns: 0
There's only a single skyscraper, so you can see no other skyscrapers from its roof.
1)
    
{5,5,5,5}
Returns: 2
From each skyscraper, you can only see its adjacent neighbors.
2)
    
{1,2,7,3,2}
Returns: 4
You can see all the other skyscrapers from the central one.
3)
    
{1,5,3,2,6,3,2,6,4,2,5,7,3,1,5}
Returns: 7

You can see seven skyscrapers from the skyscraper with height 7:

4)
    
{1000000000,999999999,999999998,999999997,999999996,1,2,3,4,5}
Returns: 6
You can see 6 skyscrapers from the skyscraper with height 999999996 - the nearest one to the left and all 5 skyscrapers to the right.
5)
    
{244645169,956664793,752259473,577152868,605440232,569378507,111664772,653430457,454612157,397154317}
Returns: 7

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10341

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13698&pm=10341

Writer:

Gluk

Testers:

PabloGilberto , ivan_metelsky , ged , zhuzeyuan

Problem categories:

Math