📜  比赛中球队的排名,以使每个球队都可以击败其连续的球队

📅  最后修改于: 2021-05-06 19:56:41             🧑  作者: Mango

给定N队,而循环赛的结果是平局或平局。任务是找到各队的顺序,以使每个队都可以胜过其连续的队。

例子:

方法:想法是为每个团队维护一个哈希图,该哈希图存储该团队在循环赛中获胜的团队。这可以通过遍历结果的元素并将每个获胜者添加到该团队的哈希映射对象列表中来完成。

The hashtable for an example is as follows:

Key          Value

Team-1 => [Team-4, Team-2]
Team-2 => [Team-3, Team-1, Team-4]
Team-3 => [Team-1]
Team-4 => [Team-3]

最后,递归地使用计算团队的顺序。下面是递归函数的说明:

  • 基本案例:当团队的当前长度为1时,退还该团队。
  • 递归的情况:计算N-1个团队的顺序,并在计算团队之后,迭代各个团队的顺序,找到当前团队相对于先前团队失去的位置。如果没有这样的位置,则将团队附加在最后一个位置。

下面是上述方法的实现:

Java
// Java implementation to find the
// order of the teams in a round
// robin tournament such that every
// team has won against to its next team
  
import java.util.*;
import java.lang.*;
  
// A class to represent result
// of a match of a tournament.
class Result {
    int winner;
    int loser;
  
    Result(int winner, int loser)
    {
        this.winner = winner;
        this.loser = loser;
    }
}
  
public class TeamOrdering {
  
    // Function to arrange the teams of
    // the round-robin tournament
    static void arrangeTeams(int[] teams,
                             Result[] results)
    {
        HashMap >
            map = new HashMap<>();
        int winner = 0;
  
        // Creating a hashmap of teams
        // and the opponents against
        // which they have won, using
        // the results of the tournament
        for (int i = 0; i < results.length; i++) {
            winner = results[i].winner;
            if (map.containsKey(winner)) {
                map.get(winner).add(
                    results[i].loser);
            }
            else {
                List list
                    = new ArrayList();
                list.add(results[i].loser);
                map.put(winner, list);
            }
        }
        List output = new ArrayList<>();
  
        // Arrange the teams in required order
        setInOrder(teams, map, teams.length, output);
        Iterator it = output.iterator();
  
        // Displaying the final output
        while (it.hasNext()) {
            System.out.print(it.next());
            System.out.print(" ");
        }
    }
  
    // Function to determine
    // the order of teams
    static void setInOrder(
        int[] teams,
        HashMap > map,
        int n, List output)
    {
        // Base Cases
        if (n < 1) {
            return;
        }
        // If there is only 1 team,
        // add it to the output
        else if (n == 1) {
            output.add(teams[n - 1]);
            return;
        }
  
        // Recursive call to generate
        // output for N-1 teams
        setInOrder(teams, map, n - 1, output);
        int key = teams[n - 1];
        int i;
  
        // Finding the position for the
        // current team in the output list.
        for (i = 0; i < output.size(); i++) {
  
            // Obtain the team at current
            // index in the list
            int team = output.get(i);
  
            // Check if it has lost against
            // current team
            List losers = map.get(key);
            if (losers.indexOf(team) != -1)
                break;
        }
        // Add the current team
        // to its final position
        output.add(i, key);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] teams = { 1, 2, 3, 4 };
        Result[] results = {
            new Result(1, 4),
            new Result(4, 3),
            new Result(2, 3),
            new Result(1, 2),
            new Result(2, 1),
            new Result(3, 1),
            new Result(2, 4)
        };
  
        // Function Call
        arrangeTeams(teams, results);
    }
}


输出:
2 4 3 1

性能分析:

  • 时间复杂度: O(N 2 )
  • 辅助空间: O(N)