leetcode-1366-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:
1 | Input: votes = ["ABC","ACB","ABC","ACB","ACB"] |
Example 2:
1 | Input: votes = ["WXYZ","XYZW"] |
Example 3:
1 | Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"] |
Example 4:
1 | Input: votes = ["BCA","CAB","CBA","ABC","ACB","BAC"] |
Example 5:
1 | Input: votes = ["M","M","M","M"] |
Constraints:
1 <= votes.length <= 1000
1 <= votes[i].length <= 26
votes[i].length == votes[j].length
for0 <= 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 invotes[j]
where1 <= j < votes.length
.
分析
对于每个团队采用一个26个int的数组来记录其投票情况,然后按照其投票情况进行排序。这里使用相反数的方式进行具体“票数”的存储,从而可以使用到 C++ STL 中默认的比较函数。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/04/leetcode-1366/
License: 知识共享署名-非商业性使用 4.0 国际许可协议