zoukankan      html  css  js  c++  java
  • 【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:

    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.

    解题思路:统计出每个元素的出现的次数,然后根据次数进行排序即可。

    代码如下:

    class Solution(object):
        def rankTeams(self, votes):
            """
            :type votes: List[str]
            :rtype: str
            """
            dic = {}
            for vote in votes:
                for i in range(len(vote)):
                    if vote[i] not in dic:
                        dic[vote[i]] = [0]*26 + [vote[i]]
                    dic[vote[i]][i] += 1
            rank = []
    
            for val in dic.itervalues():
                rank.append(val)
    
            def cmpf(item1,item2):
                for i in range(len(item1)-1):
                    if item1[i] != item2[i]:
                        return item2[i] - item1[i]
                return ord(item1[-1]) - ord(item2[-1])
    
            rank.sort(cmp=cmpf)
    
            res = ''
            for r in rank:
                res += r[-1]
    
            return res
  • 相关阅读:
    Smarty 模板 insert 局部刷新不缓存功能
    批量选择图片上传的jquery插件
    (转)国外15个前端开发CSS框架介绍
    IE6 下 zindex 设置的 DIV 偏移位置的解决方法
    ecshop的 transport.js 文件和 Jquery 冲突解决方案
    (转) javascript 匿名函数的理解,js括号中括function 如(function(){})
    (转)javascript匿名函数
    jQuery 的 hover 方法等同于 mouseenter + mouseleave 方法
    php set_magic_quotes_runtime() 函数过时
    同域名不同主机下的iframe高度调整
  • 原文地址:https://www.cnblogs.com/seyjs/p/12416386.html
Copyright © 2011-2022 走看看