跳到主要内容

1366.通过投票对团队排名

链接:1366.通过投票对团队排名
难度:Medium
标签:数组、哈希表、字符串、计数、排序
简介:请你返回能表示按排名系统 排序后 的所有团队排名的字符串。

题解 1 - python

  • 编辑时间:2024-12-29
  • 执行用时:20ms
  • 内存消耗:18.21MB
  • 编程语言:python
  • 解法介绍:遍历后dfs比较
class Solution:
def rankTeams(self, votes: List[str]) -> str:
n = len(votes)
m = len(votes[0])
map = [[0 for _ in range(26)] for _ in range(m)]
for i in range(n):
for j in range(m):
map[j][ord(votes[i][j]) - ord('A')] += 1
candidates = [ord(c) - ord('A') for c in votes[0]]
def compare(i: int, data: List[int]) -> int:
if i == m: return min(data)
if len(data) == 1: return data[0]
nmax = max(map[i][j] for j in data)
idxs = [j for j in data if map[i][j] == nmax]
return compare(i + 1, idxs)
res = ''
for i in range(m):
j = compare(0, candidates)
candidates.remove(j)
res += chr(j + ord('A'))
return res