1366. Rank Teams by Votes

问题:

给出多次,字母排名次的名次列表。

求的综合名次的结果。(如果两字母分值相同,则按照字母序排列)

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.

  

解法:

名次得分法:

第一位得分最高,第二位得分次之,以此类推,最后一位得分最小为1

累计各个字母的累计分值,

按照分值从大到小,最大分值的字母则综合排名为第一位。

 

vector<vector<int>> count(26, vector<int>(27));

我们利用上述结构。记录A~Z字母出现的次数

count['A'][0~25]表示字母A出现在0~25位上的得分。

最后排序需要另外记录该字母,且若两字母分值相同,按照字母顺序计算分值

因此多加一位count['A'][26],保存该字母。

(这样,即保存了该字母是哪个字母,又将字母序也放入分值计算中)

 

我们遍历所有排序中所有字母的出现,同时计算分值,计入count中。

⚠️ 注意,由于最终按照出现次数最多,排序在前面,

我们若想使用默认从小到大的sort排序,那么,我们使出现次数最多,得分值最小,这样去计算分值。

即,出现一次,值--,那么出现越多,值越小。

 

最后,对count进行默认sort排序。

从头到尾依次输出到res中。

 

代码参考:

 1 class Solution {
 2 public:
 3     string rankTeams(vector<string>& votes) {
 4         string res;
 5         int N=votes.size();
 6         int POINT=votes[0].size();
 7         if(N==1 || POINT==1) return votes[0];
 8         vector<vector<int>> count(26, vector<int>(27));
 9         for(char c:votes[0]){
10             count[c-'A'][26]=c;
11         }
12         for(string s:votes){
13             int po=POINT;
14             for(int i=0; i<POINT; i++){
15                 count[s[i]-'A'][i]--;
16             }
17         }
18         sort(count.begin(), count.end());
19         for(int i=0; i<POINT; i++){
20             res+=count[i][26];
21         }
22         return res;
23     }
24 };

 

posted @ 2020-07-11 16:37  habibah_chang  阅读(252)  评论(0编辑  收藏  举报