Solution: Rank Teams by Votes
Let’s solve the Rank Teams by Votes problem using the Knowing What to Track pattern.
We'll cover the following
Statement
Assume a ranking system where each gives a rank to all competing teams from highest to lowest. The final ranking is decided on the basis of the number of first-place votes they receive. If there’s a tie for first place, the second-place votes are used to break the tie; if there’s still a tie, the third-place votes are considered, and this continues until all positions have been evaluated. If teams remain tied after all positions have been reviewed, they are ranked alphabetically by their team names.
Given an array of strings votes
that represents the rankings given by all voters, sort the teams according to the specified ranking system and return a string that lists all the teams in the ranked order.
Constraints:
votes.length
votes[i].length
votes[i].length == votes[j].length
fori, j
votes.length
votes[i][j]
is an uppercase English letter.All characters of
votes[i]
are unique.All characters present in
votes[0]
are also found invotes[i]
wherei
votes.length
.
Solution
The key intuition for solving this problem is to efficiently keep track of the vote counts each team receives for each position. We create a 2D array for this purpose, where the rows represent the teams and the columns represent the vote counts for each position. For each team in votes[i][j]
, we update its vote count at the
Using the above intuition, the solution can be implemented as follows:
Create a 2D array
counts
with dimensionsto store vote counts for each team. The first dimension (
) corresponds to the teams (A–Z), as there can be a maximum of teams. The second dimension (
) holds the vote counts for each rank. Because there can be a maximum of teams, the maximum rank a team can get is . It includes an additional column to store the team’s name to ensure that we don’t lose track of which team’s data is in each row after sorting.
For each team index
t
, store the team’s name atcounts[t][26]
. This keeps track of which team corresponds to each row in thecounts
array.Start iterating through each string
votes[i]
to process the ranking given by each voter.For each character
votes[i][j]
representing a team in the vote, decrement its rank in thecounts
. This is because each row incounts
denotes the rank of a team; a lower count indicates a higher rank.Sort the
counts
array based on the values in each row. The sorting is done such that the row with the lowest value at the first index comes first. If there is a tie, the row with the lowest value at the second index takes precedence, and so on.Construct the final result by retrieving the team name stored at the last index (
counts[i][26]
) in the sortedcounts.
Return the constructed result, which contains all teams sorted according to the specified ranking system.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.