Ranked Ballots
MediumHash Map · Atlassian
A team is voting for its next project lead. Each ballot in ballots lists one to three distinct candidate names in order of preference. A candidate earns 3 points for each first-place mention, 2 for each second-place mention and 1 for each third-place mention.
Return every candidate who appears on at least one ballot, ordered from winner to last place using these rules in turn:
- More total points ranks higher.
- If points are tied, more first-place mentions ranks higher.
- If still tied, more second-place mentions ranks higher.
- If still tied, the alphabetically smaller name (plain string comparison) ranks higher.
Return an empty list if there are no ballots.
Example 1
- Input:
- ballots = [["Max", "Ivy"], ["Ivy", "Jo", "Max"], ["Jo", "Kai", "Max"]]
- Output:
- ["Ivy", "Jo", "Max", "Kai"]
- Explanation
- Max scores 3 + 1 + 1, Ivy 2 + 3 and Jo 2 + 3, so all three have 5 points and one first-place vote each. Ivy and Jo each also have a second-place vote, which puts them ahead of Max, and Ivy beats Jo alphabetically. Kai has 2 points.
Example 2
- Input:
- ballots = [["Dee", "Eve", "Fay"], ["Fay", "Eve"], ["Gus", "Dee"]]
- Output:
- ["Dee", "Fay", "Eve", "Gus"]
- Explanation
- Dee has 3 + 2 = 5 points. Eve and Fay both have 4, but Fay has a first-place vote and Eve has none, so Fay ranks higher. Gus has 3.
Example 3
- Input:
- ballots = []
- Output:
- []
- Explanation
- With no ballots there are no candidates.
Constraints
0 <= ballots.length <= 10^51 <= ballots[i].length <= 3, and the names on one ballot are distinct.- Names are non-empty strings of letters; comparison is case-sensitive.