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.
class Solution { public String rankTeams(String[] votes) { Map<Character, int[]> map = new HashMap<>(); int l = votes[0].length(); for(String vote : votes){ for(int i = 0; i < l; i++){ char c = vote.charAt(i); map.putIfAbsent(c, new int[l]); map.get(c)[i]++; } } List<Character> list = new ArrayList<>(map.keySet()); Collections.sort(list, (a,b) -> { for(int i = 0; i < l; i++){ if(map.get(a)[i] != map.get(b)[i]){ return map.get(b)[i] - map.get(a)[i]; } } return a - b; }); StringBuilder sb = new StringBuilder(); for(char c : list){ sb.append(c); } return sb.toString(); } }
上面的方法是用一个char和出现位数的数组的map,然后用lambda表达式对字母进行排列,排列规则按照高位出现的频率,如果相等就再往下比,直到分出大小。如果还想等,就按字母从小到大排列。
最开始想了一种,是按score排列所有的字母,后来被大神指点看了题目后发现理解错了。题目要求比较高位,比如第一位,如果相等就看第二位的,如果第二位多就是这个字母。我下面这种方法错误的原因正是因为按score比,所以如果下位有稍微多的上位稍微少的就会出现计算错误。
class Solution { public String rankTeams(String[] votes) { Map<Character, Integer> map = new HashMap<>(); int l = votes[0].length(); for(String vote : votes){ for(int i = 0; i < l; i++){ char c = vote.charAt(i); map.putIfAbsent(c, 0); map.put(c, map.get(c) + l - i); } } List<Character> list = new ArrayList<>(map.keySet()); Collections.sort(list, (a,b) -> { for(int i = 0; i < l; i++){ if(map.get(a) != map.get(b)){ return map.get(b) - map.get(a); } } return a - b; }); StringBuilder sb = new StringBuilder(); for(char c : list){ sb.append(c); } return sb.toString(); } }