TopCoder problem "TournamentRanker" used in SRM 216 (Division II Level Three)

Problem Statement

    A single elimination tournament consists of several rounds. In the first round, each competitor plays against one other competitor and the loser is eliminated. In the second round, each competitor that was not eliminated in the first round plays against another such competitor, and the loser is again eliminated. This process is repeated, eliminating half of the remaining competitors each round, until precisely one competitor remains. This competitor is declared the champion.

In practice, one is often interested in ranking all the competitors in a tournament, not just the champion. Here is one way of doing this:
  • If competitor A won more games than competitor B did in the tournament, then A should be ranked higher than B.
  • If competitor A and competitor B won the same number of games in the tournament, recursively compare the ranks of the competitor C that eliminated A and the competitor D that eliminated B. Then, A should be ranked above B if and only if C is ranked above D.
The intuition behind the second rule is that losing to a good competitor is better than losing to a bad competitor.

You must implement this scheme for ranking the competitors in a single elimination tournament. You will be given a String[] names and a String[] lostTo. The former argument gives the names of the competitors in a tournament, and the latter argument describes if and how each competitor was eliminated. Specifically, the ith element of lostTo will be the name of the competitor that beat the ith competitor in names, or it will be "" if that competitor never lost. You should return a String[] giving the names of the competitors, sorted from highest rank to lowest rank as described above.


Parameters:String[], String[]
Method signature:String[] rankTeams(String[] names, String[] lostTo)
(be sure your method is public)


-The constraints ensure that names and lostTo uniquely specify a valid single elimination tournament.


-The number of elements in names must be a power of 2 and must be between 2 and 32 inclusive.
-Each element of names will contain between 1 and 50 characters inclusive.
-Each character in names will either be a space (' '), or a capital letter ('A'-'Z').
-No two elements of names will be equal.
-The number of elements in lostTo will be equal to the number of elements in names.
-Exactly one element of lostTo will be equal to ""; each remaining element of lostTo will be equal to an element of names.
-If competitor A has n wins, as specified by names and lostTo, then it will have eliminated exactly one competitor with k wins for each k satisfying 0 <= k < n.


This test case represents the semifinals and finals of the 2003 Australian Open tennis tournament, illustrated below:
    RODDICK ----+
                +--- SCHUETTLER -+
    SCHUETTLER -+                |
                                 +--- AGASSI
    FERREIRA ---+                |
                +--- AGASSI -----+
    AGASSI -----+
AGASSI is ranked highest with two wins, followed by SCHUETTLER with one win. FERREIRA and RODDICK both have zero wins, so we compare the rankings of the competitors that beat them. Since FERREIRA lost to AGASSI, RODDICK lost to SCHUETTLER, and AGASSI is ranked above SCHUETTLER, we rank FERREIRA above RODDICK.
{"", "DUKE", "DUKE", "ILLINOIS",
{ "DUKE",
This test case represents three rounds of the 2004 NCAA men's basketball tournament, illustrated below:
    DUKE --------------+
                       +--- DUKE -----+
    SETON HALL --------+              |
                                      +--- DUKE ---+
    ILLINOIS ----------+              |            |
                       +--- ILLINOIS -+            |
    CINCINNATI --------+                           |
                                                   +--- DUKE
    NORTH CAROLINA ----+                           |
                       +--- TEXAS ----+            |
    TEXAS -------------+              |            |
                                      +--- XAVIER -+
    XAVIER ------------+              |
                       +--- XAVIER ---+
DUKE is ranked first with three wins, followed by XAVIER with two wins. ILLINOIS and TEXAS come next, having one win each. Since ILLINOIS lost to DUKE and XAVIER lost to TEXAS, ILLINOIS should be ranked above TEXAS. The remaining teams are ranked similarly. SETON HALL is ranked highest among them since they lost to top-ranked DUKE, whereas NORTH CAROLINA is ranked lowest among them since they lost to fourth-ranked TEXAS.
Returns: { "VISUAL BASIC",  "JAVA" }

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , brett1479

Problem categories:

Recursion, Sorting