📜  按排名从高到低的顺序打印团队的名称

📅  最后修改于: 2021-04-17 15:27:06             🧑  作者: Mango

给定二维数组字符串arr [] [4] ,它们表示在涉及N个球队的锦标赛中进行的M场足球比赛的得分,任务是按球队排名的升序打印其名称。

  • 比赛规则如下:
    • 每队比赛2场。
    • 获胜的团队获得2分,而失败的团队获得0分。
    • 如果是平局,两支球队各得一分。
  • 如果GD,GAGF分别代表“目标差异”“反对”和“目标” 。团队的排名是按以下优先顺序确定的:
    • 点> GD> GF>名称的字典顺序。

例子:

方法:可以通过按多个属性对字典进行排序来解决该问题。
请按照以下步骤解决问题:

  • 如果arr [i] [0]等于arr [i] [1] ,则遍历列表arr [] []并打印“ Invalid”并中断。
  • 初始化字典以存储特定团队的GA,GFGD
  • 遍历列表arr [] []并执行以下操作:
    • arr [i] [0],表[arr [i] [0]] [0]GF增加arr [i] [2]并将arr [i] [0],表[arr [i]的GA增加[0]] [1]来自arr [i] [3]。
    • arr [i] [1]表[arr [i] [1]] [0]GF递增arr [i] [3] ,将arr [i] [1]表[arr [i]的GA递增[1]] [1]来自arr [i] [2]。
    • 通过table [arr [i] [0] [2] – table [arr [i] [0] [3]更新arr [i] [0],table [arr [i] [0]]GD
    • 通过table [arr [i] [1] [2] – table [arr [i] [1] [3]更新arr [i] [1], table [arr [i] [1]]GD
    • 如果arr [i] [2] == arr [i] [3],则将table [arr [i] [0] [0]table [arr [i] [1]] [0]都加1
    • 如果arr [i] [2]> arr [i] [3],则将table [arr [i] [0] [0]递增2
    • 如果arr [i] [2] 则将table [arr [i] [1] [0]递增2
  • 现在,根据优先级> GD> GF和名称将字典排序:
    • table = sorted(table.items(),key = lambda r:(-r [1] [0],-r [1] [1],-r [1] [2],r [0]))
  • 最后,在排序表中打印团队的名称。

下面是上述方法的实现:

Python3
# Python program for the above approach
  
# Function to find the ranking of teams
def RankTeams(arr):
  
    # Traverse the list arr
    for i in range(len(arr)):
        
        # arr[i][0] is equal to arr[i][1]
        if(arr[i][0] == arr[i][1]):
            print("Invalid")
            return
            
        # Convert the goal to integer
        arr[i][2] = int(arr[i][2])
        arr[i][3] = int(arr[i][3])
  
    # Stores the list of GD, GA, and GF
    table = {}
      
    # Traverse the list
    for i in range(len(arr)):
        
        # Store the list of GA, GF 
        # and GD of first team
        li1 = [0] * 4
          
        # Store the list of GA, GF 
        # and GD of second team
        li2 = [0] * 4
  
        # If arr[i][0] is in table
        if arr[i][0] in table:
            li1 = table[arr[i][0]]
              
        # If arr[i][1] is in table
        if arr[i][1] in table:
            li2 = table[arr[i][1]]
  
        # Increment GF by arr[i][2]
        li1[2] += arr[i][2]
          
        # Increment GA by arr[i][3]
        li1[3] += arr[i][3]
  
        # Increment GF by arr[i][3]
        li2[2] += arr[i][3]
          
        # Increment GA by arr[i][2]
        li2[3] += arr[i][2]
  
        # Update GD
        li1[1] = li1[2] - li1[3]
        li2[1] = li2[2] - li2[3]
  
        # If tie
        if(arr[i][2] == arr[i][3]):
            li1[0] += 1
            li2[0] += 1
  
        # If arr[i][0] wins
        elif(arr[i][2] > arr[i][3]):
            li1[0] += 2
  
        # If arr[i][1] wins
        elif(arr[i][2] < arr[i][3]):
            li2[0] += 2
  
        # Update list in table
        table[arr[i][0]] = li1
        table[arr[i][1]] = li2
  
    # Traverse the sortd table in the given priority
    for key, value in sorted(table.items(),
                             key = lambda r: (-r[1][0],
                                            -r[1][1],
                                            -r[1][2],
                                            r[0])):
        # Print the team name
        print(key, end ='\n')
  
  
# Driver Code
  
# Input
arr = [['Spain', 'England', '3', '0'], 
       ['England', 'France', '1', '1'],
       ['Spain', 'France', '0', '2']]
  
RankTeams(arr)


输出:
France
Spain
England

时间复杂度: O(N * log(N))
辅助空间: O(N)