LeetCode #1366 Rank Teams by Votes 通過投票對團隊排名 1366 Rank Teams by Votes 通過投票對團隊排名

1366 Rank Teams by Votes 通過投票對團隊排名

Description:

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:

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.

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.

題目描述:

現在有一個特殊的排名系統,依據參賽團隊在投票人心中的次序進行排名,每個投票者都需要按從高到低的順序對參與排名的所有團隊進行排位。

排名規則如下:

參賽團隊的排名次序依照其所獲「排位第一」的票的多少決定。如果存在多個團隊並列的情況,將繼續考慮其「排位第二」的票的數量。以此類推,直到不再存在並列的情況。
如果在考慮完所有投票情況後仍然出現並列現象,則根據團隊字母的字母順序進行排名。
給你一個字符串數組 votes 代表全體投票者給出的排位情況,請你根據上述排名規則對所有參賽團隊進行排名。

請你返回能表示按排名系統 排序後 的所有團隊排名的字符串。

示例:

示例 1:

輸入:votes = ["ABC","ACB","ABC","ACB","ACB"]
輸出:"ACB"
解釋:A 隊獲得五票「排位第一」,沒有其他隊獲得「排位第一」,所以 A 隊排名第一。
B 隊獲得兩票「排位第二」,三票「排位第三」。
C 隊獲得三票「排位第二」,兩票「排位第三」。
由於 C 隊「排位第二」的票數較多,所以 C 隊排第二,B 隊排第三。

示例 2:

輸入:votes = ["WXYZ","XYZW"]
輸出:"XWYZ"
解釋:X 隊在並列僵局打破後成爲排名第一的團隊。X 隊和 W 隊的「排位第一」票數一樣,但是 X 隊有一票「排位第二」,而 W 沒有獲得「排位第二」。

示例 3:

輸入:votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
輸出:"ZMNAGUEDSJYLBOPHRQICWFXTVK"
解釋:只有一個投票者,所以排名完全按照他的意願。

示例 4:

輸入:votes = ["BCA","CAB","CBA","ABC","ACB","BAC"]
輸出:"ABC"
解釋:
A 隊獲得兩票「排位第一」,兩票「排位第二」,兩票「排位第三」。
B 隊獲得兩票「排位第一」,兩票「排位第二」,兩票「排位第三」。
C 隊獲得兩票「排位第一」,兩票「排位第二」,兩票「排位第三」。
完全並列,所以我們需要按照字母升序排名。

示例 5:

輸入:votes = ["M","M","M","M"]
輸出:"M"
解釋:只有 M 隊參賽,所以它排名第一。

提示:

1 <= votes.length <= 1000
1 <= votes[i].length <= 26
votes[i].length == votes[j].length for 0 <= i, j < votes.length
votes[i][j] 是英文 大寫 字母
votes[i] 中的所有字母都是唯一的
votes[0] 中出現的所有字母 同樣也 出現在 votes[j] 中,其中 1 <= j < votes.length

思路:

貪心
統計所有數字出現的次數和所有數字之和 s
如果 s 能整除 3, 從大到小輸出所有數字即可
如果 s 除 3 餘 1, 要麼去掉一個最小的除 3 餘 1 的數, 要麼去掉 2 個最小除 3 餘 2 的數
如果 s 除 3 餘 2, 要麼去掉一個最小的除 3 餘 2 的數, 要麼去掉 2 個最小除 3 餘 2 的數
時間複雜度爲 O(n), 空間複雜度爲 O(1)

代碼:

C++:

class Solution 
{
public:
    string rankTeams(vector<string>& votes) 
    {
        map<char, vector<int>> m;
        for (const auto& v : votes)
        {
            for (int i = 0; i < v.size(); i++)
            {
                m[v[i]].resize(v.size());
                m[v[i]][i]++;
            }      
        }
        string result = votes.front();
        sort(result.begin(), result.end(), [&](const auto& a, const auto& b) { return m[a] > m[b] or m[a] == m[b] and a < b; });
         return result;  
    }
};

Java:

class Solution {
    public String rankTeams(String[] votes) {
        HashMap<Character, List<Integer>> count = new HashMap<>();
        char[] chars = votes[0].toCharArray();
        int n = votes[0].length();
        for (Character c : chars) {
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < n; i++) list.add(0);
            count.put(c, list);
        }
        for (String vote : votes) for (int i = 0; i < n; i++) count.get(vote.charAt(i)).set(i, count.get(vote.charAt(i)).get(i) + 1);
        List<Character> keyList = new ArrayList<>(count.keySet());
        Collections.sort(keyList, new Comparator<Character>() {
            @Override
            public int compare(Character c1, Character c2) {
                List<Integer> list1 = count.get(c1), list2 = count.get(c2);
                for (int i = 0; i < n; i++) {
                    if (list1.get(i) > list2.get(i)) return -1;
                    if (list1.get(i) < list2.get(i)) return 1;
                }
                return c1.compareTo(c2);
            }
        });
        StringBuilder sb = new StringBuilder();
        for (Character c : keyList) sb.append(c);
        return sb.toString();
    }
}

Python:

class Solution:
    def rankTeams(self, votes: List[str]) -> str:
        d = defaultdict(lambda: [0] * len(votes[0]))
        for vote in votes:
            for i, v in enumerate(vote):
                d[v][i] -= 1
        return ''.join(sorted(votes[0], key=lambda x: (d[x], x)))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章