1366. Rank Teams by Votes LeetCode Solution
In this guide, you will get 1366. Rank Teams by Votes LeetCode Solution with the best time and space complexity. The solution to Rank Teams by Votes problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Rank Teams by Votes solution in C++
- Rank Teams by Votes solution in Java
- Rank Teams by Votes solution in Python
- Additional Resources
Problem Statement of Rank Teams by Votes
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 <= 1000
1 <= votes[i].length <= 26
votes[i].length == votes[j].length for 0 <= 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 in votes[j] where 1 <= j < votes.length.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(26^2)
1366. Rank Teams by Votes LeetCode Solution in C++
struct Team {
char name;
vector<int> rank;
Team(char name, int teamSize) : name(name), rank(teamSize) {}
};
class Solution {
public:
string rankTeams(vector<string>& votes) {
const int teamSize = votes[0].size();
string ans;
vector<Team> teams;
for (int i = 0; i < 26; ++i)
teams.push_back(Team('A' + i, teamSize));
for (const string& vote : votes)
for (int i = 0; i < teamSize; ++i)
++teams[vote[i] - 'A'].rank[i];
ranges::sort(teams, [](const Team& a, const Team& b) {
return a.rank == b.rank ? a.name < b.name : a.rank > b.rank;
});
for (int i = 0; i < teamSize; ++i)
ans += teams[i].name;
return ans;
}
};
/* code provided by PROGIEZ */
1366. Rank Teams by Votes LeetCode Solution in Java
class Team {
public char name;
public int[] rank;
public Team(char name, int teamSize) {
this.name = name;
this.rank = new int[teamSize];
}
}
class Solution {
public String rankTeams(String[] votes) {
final int teamSize = votes[0].length();
StringBuilder sb = new StringBuilder();
Team[] teams = new Team[26];
for (int i = 0; i < 26; ++i)
teams[i] = new Team((char) ('A' + i), teamSize);
for (final String vote : votes)
for (int i = 0; i < teamSize; ++i)
++teams[vote.charAt(i) - 'A'].rank[i];
Arrays.sort(teams, new Comparator<Team>() {
@Override
public int compare(Team a, Team b) {
for (int i = 0; i < a.rank.length; ++i)
if (a.rank[i] > b.rank[i])
return -1;
else if (a.rank[i] < b.rank[i])
return 1;
return a.name - b.name;
}
});
for (int i = 0; i < teamSize; ++i)
sb.append(teams[i].name);
return sb.toString();
}
}
// code provided by PROGIEZ
1366. Rank Teams by Votes LeetCode Solution in Python
from dataclasses import dataclass
@dataclass
class Team:
name: str
rank: list[int]
def __init__(self, name: str, teamSize: int):
self.name = name
self.rank = [0] * teamSize
class Solution:
def rankTeams(self, votes: list[str]) -> str:
teamSize = len(votes[0])
teams = [Team(chr(ord('A') + i), teamSize) for i in range(26)]
for vote in votes:
for i in range(teamSize):
teams[ord(vote[i]) - ord('A')].rank[i] += 1
teams.sort(key=lambda x: (x.rank, -ord(x.name)), reverse=True)
return ''.join(team.name for team in teams[:teamSize])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.