LeetCode 5345. Rank Teams by Votes - Java - Sort

题目链接:5345. Rank Teams by Votes

In a special ranking system, each voter gives a rank from highest to lowest to all teams participated 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.

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 was ranked third by 3 voters.
Team C was ranked second by 3 voters and was 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 tie-breaking rule. X has same votes as W for the first position but X has one 
vote as second position while W doesn't have any votes as second position. 
Example 3:
Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
Explanation: Only one voter so his votes are used for the ranking.
Example 4:
Input: votes = ["BCA","CAB","CBA","ABC","ACB","BAC"]
Output: "ABC"
Explanation: 
Team A was ranked first by 2 voters, second by 2 voters and third by 2 voters.
Team B was ranked first by 2 voters, second by 2 voters and third by 2 voters.
Team C was ranked first by 2 voters, second by 2 voters and third by 2 voters.
There is a tie and we rank teams ascending by their IDs.
Example 5:
Input: votes = ["M","M","M","M"]
Output: "M"
Explanation: Only team M in the competition so it has the first rank.
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 upper-case 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.
题解

用二维数组 voteAns[i][j] 保存团队 i 在位置 j 的选票数。根据选票数将二维数组排序。
避免无法对应,需要用一位来保存团队编号。由于团队名不是固定的,所以需要建立映射表方便转换。

时间复杂度 O ( m n ) O(mn) O(mn),m表示团队数,n表示投票人数
空间复杂度 O ( m 2 ) O(m^2) O(m2),m表示团队数

Java代码
public class Solution {
    public String rankTeams(String[] votes) {
        if (votes.length == 1) {
            // 只有一位投票人时,直接返回此人的投票结果
            return votes[0];
        }
        // teams保存所有团队,用于根据编号找团队名(确保升序排序)
        char[] teams = votes[0].toCharArray();
        // 字典序升序排序
        Arrays.sort(teams);
        // map保存团队和编号的映射表,即根据团队名找编号
        Map<Character, Integer> map = new HashMap<>(teams.length);
        for (int i = 0; i < teams.length; ++i) {
            map.put(teams[i], i);
        }
        // voteAns[i][j]表示编号为i的团队在j位置的票数(不包括最后一列)
        int[][] voteAns = new int[teams.length][teams.length + 1];
        for (int i = 0; i < teams.length; ++i) {
            // 最后一列保存团队编号,保证排序后能找到对应的团队
            voteAns[i][teams.length] = i;
        }
        // 将投票结果转换为数组的值,便于排序
        for (String vote : votes) {
            for (int i = 0; i < vote.length(); ++i) {
                // 获取团队对应的编号
                int id = map.get(vote.charAt(i));
                // 团队id在i位置的选票加一
                voteAns[id][i] += 1;
            }
        }
        // 将投票结果排序
        Arrays.sort(voteAns, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                for (int i = 0; i < o1.length - 1; ++i) {
                    if (o1[i] != o2[i]) {
                        // 按照同一位置选票个数降序排序
                        return o2[i] - o1[i];
                    }
                }
                // 如果所有位置的选票个数都相同,按照团队名的字典序升序排序
                return o1[o1.length - 1] - o2[o2.length - 1];
            }
        });
        // 得到最终结果
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < voteAns.length; ++i) {
            // 获取第i名的id
            int id = voteAns[i][teams.length];
            // 根据id获取团队名并添加到最终结果末尾
            result.append(teams[id]);
        }
        return result.toString();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值