TopCoder problem "TrophyShelf" used in SRM 386 (Division II Level One)

Problem Statement

    You have several trophies sitting on a shelf in a straight line. Their heights are given in a int[] trophies, from left to right. The shelf is positioned so that whenever people enter your room, they see it directly from the left side. In other words, the leftmost trophy is completely visible to the viewer, the next trophy in line is directly behind it, and so on.

Unfortunately, tall trophies near the left side of the shelf might block the view of other trophies. A trophy is visible only if every trophy in front of it (from the viewer's perspective) is strictly shorter than it is. You wonder if rotating the shelf 180 degrees would increase the number of visible trophies.

Return a int[] containing exactly two elements. The first element should be the number of trophies visible when viewing the shelf directly from the left side, and the second element should be the number of trophies visible when viewing the shelf directly from the right side.


Method signature:int[] countVisible(int[] trophies)
(be sure your method is public)


-trophies will contain between 1 and 50 elements, inclusive.
-Each element of trophies will be between 1 and 100, inclusive.


Returns: {5, 1 }
When viewed from the left, each trophy is taller than all the trophies in front of it. However, when viewed from the right, the first trophy blocks the view of all the other trophies.
Returns: {1, 1 }
Since all trophies have the same height, only the first is visible when viewed from each direction.
Returns: {3, 3 }
This trophy shelf is symmetric.
Returns: {4, 2 }

Problem url:

Problem stats url:




PabloGilberto , Olexiy , lovro , ivan_metelsky

Problem categories:

Simple Search, Iteration