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(mn)O(mn),m表示團隊數,n表示投票人數
空間複雜度O(m2)O(m^2),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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章