Instant Runoff Election
Problem statement
Every row in ballots ranks all candidates from most to least preferred. In each round, count each ballot for its highest-ranked remaining candidate. A candidate with strictly more than half the ballots wins.
Otherwise eliminate the candidate with the fewest votes, breaking such ties by lexicographically largest name, and repeat. If all remaining candidates tie, return the lexicographically smallest candidate.
Function
instantRunoffWinner(ballots: String[][]) → StringExamples
Example 1
ballots = [["A","B","C"],["A","C","B"],["B","C","A"],["C","B","A"]]return = "A"C is eliminated from the initial 2-1-1 round, and its ballot transfers to B; the 2-2 tie eliminates B, leaving A.
Constraints
- Every ballot contains the same distinct candidate names.
- At least one ballot and candidate exist.