Rank Teams by Votes Problem
Description
LeetCode Problem 1366.
In a special ranking system, each voter gives a rank from highest to lowest to all teams participating in the competition.
The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second position to resolve the conflict, if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, we rank them alphabetically based on their team letter.
You are given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.
Return a string of all teams sorted by the ranking system.
Example 1:
Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
Output: "ACB"
Explanation:
Team A was ranked first place by 5 voters. No other team was voted as first place, so team A is the first team.
Team B was ranked second by 2 voters and ranked third by 3 voters.
Team C was ranked second by 3 voters and ranked third by 2 voters.
As most of the voters ranked C second, team C is the second team, and team B is the third.
Example 2:
Input: votes = ["WXYZ","XYZW"]
Output: "XWYZ"
Explanation:
X is the winner due to the tie-breaking rule. X has the same votes as W for the first position, but X has one vote in the second position, while W does not have any votes in the second position.
Example 3:
Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
Explanation: Only one voter, so their votes are used for the ranking.
Constraints:
1 <= votes.length <= 10001 <= votes[i].length <= 26votes[i].length == votes[j].lengthfor0 <= i, j < votes.length.votes[i][j]is an English uppercase letter.- All characters of
votes[i]are unique. - All the characters that occur in
votes[0]also occur invotes[j]where1 <= j < votes.length.
Solution
Approach: Count Votes per Position, Then Sort
The ranking rule compares two teams position by position: whoever has more votes at position 1 wins; on a tie, look at position 2, and so on; if every position ties, the earlier letter wins. That is a lexicographic comparison of each team's vote-count vector.
- For each team, build
count[team], wherecount[team][k]is the number of voters who put that team at positionk. One pass over all votes fills it. - Sort the teams (the letters of
votes[0]; every vote contains the same teams) with a comparator: scank = 0, 1, ...and at the first position where the counts differ, the team with the larger count goes first. If all counts are equal, compare the letters.
In Example 2, W and X both have one first-place vote, but X has one second-place vote and W has none, so X comes first.
Note the two directions: counts are compared in descending order, letters in ascending order. In Python this is the key ([-c for c in count[team]], team). In C++, vectors already compare lexicographically, so count[a] > count[b] does the position-by-position scan.
Complexity: O(n * m + m2 log m) time, where n is the number of votes and m the number of teams: counting reads every character once, and each of the O(m log m) comparisons scans up to m positions. O(m2) space for the counts.
class Solution:
def rankTeams(self, votes: List[str]) -> str:
m = len(votes[0])
# count[team][k] = number of voters who put team at position k
count = {team: [0] * m for team in votes[0]}
for vote in votes:
for k, team in enumerate(vote):
count[team][k] += 1
# More votes at an earlier position first; full ties in alphabetical order.
return "".join(sorted(votes[0], key=lambda team: ([-c for c in count[team]], team)))