Say there are positions and
represents the vote counts for
at different positions. Like,
got
votes for position
, got
votes for position
, etc. We can consider an extended pair:
and sort these pairs by reverse-lexicographic of the first element and lexicographic of the second element.
To have custom comparison while sorting we encapsulate each team’s vote counts in a Rank object.
Say there are votes and
teams.
Time: , space:
.
class Rank:
def __init__(self, tally, team):
self.tally = tally
self.team = team
def update(self, i):
self.tally[i] += 1
def __lt__(self, other):
if self.tally != other.tally:
return self.tally >= other.tally
return self.team < other.team
class Solution:
def rankTeams(self, votes: List[str]) -> str:
ranks = {t: Rank([0] * len(votes[0]), t) for t in votes[0]}
for v in votes:
for i, t in enumerate(v):
ranks[t].update(i)
return "".join([r.team for r in sorted(ranks.values())])
Leave a comment