Advertisement
Nonplussed

Rank teams by votes

Mar 29th, 2025
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | Source Code | 0 0
  1. from functools import cmp_to_key
  2.  
  3. class Solution:
  4.     def rankTeams(self, allVoteEntries: List[str]) -> str:
  5.         allTeamNames = allVoteEntries[0]
  6.         numTeams = len(allTeamNames)
  7.  
  8.         rankings = {teamName: [0]*numTeams for teamName in allTeamNames}
  9.         for voteEntry in allVoteEntries:
  10.             for ith_rank, teamName in enumerate(voteEntry):
  11.                 rankings[teamName][ith_rank] += 1
  12.  
  13.         def compare(teamName1, teamName2):
  14.             for ith_rank in range(numTeams):
  15.                 if rankings[teamName1][ith_rank] > rankings[teamName2][ith_rank]:
  16.                     return -1
  17.                 elif rankings[teamName2][ith_rank] > rankings[teamName1][ith_rank]:
  18.                     return 1
  19.            
  20.             if ord(teamName1) < ord(teamName2):
  21.                 return -1
  22.             else:
  23.                 return 1
  24.  
  25.         return "".join(sorted(allTeamNames, key = cmp_to_key(compare)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement