zoukankan      html  css  js  c++  java
  • leetcode1366 Rank Teams by Votes

     1 """
     2 In a special ranking system, each voter gives a rank from highest to lowest to all teams participated in the competition.
     3 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.
     4 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.
     5 Return a string of all teams sorted by the ranking system.
     6 Example 1:
     7 Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
     8 Output: "ACB"
     9 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.
    10 Team B was ranked second by 2 voters and was ranked third by 3 voters.
    11 Team C was ranked second by 3 voters and was ranked third by 2 voters.
    12 As most of the voters ranked C second, team C is the second team and team B is the third.
    13 Example 2:
    14 Input: votes = ["WXYZ","XYZW"]
    15 Output: "XWYZ"
    16 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.
    17 Example 3:
    18 Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
    19 Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
    20 Explanation: Only one voter so his votes are used for the ranking.
    21 Example 4:
    22 Input: votes = ["BCA","CAB","CBA","ABC","ACB","BAC"]
    23 Output: "ABC"
    24 Explanation:
    25 Team A was ranked first by 2 voters, second by 2 voters and third by 2 voters.
    26 Team B was ranked first by 2 voters, second by 2 voters and third by 2 voters.
    27 Team C was ranked first by 2 voters, second by 2 voters and third by 2 voters.
    28 There is a tie and we rank teams ascending by their IDs.
    29 Example 5:
    30 Input: votes = ["M","M","M","M"]
    31 Output: "M"
    32 Explanation: Only team M in the competition so it has the first rank.
    33 """
    34 """
    35 用dict{v:list[]}结构来存储,
    36 其中list[0],list[1],list[2]分别代表字母在该位置出现的次数
    37 最后按照list大小对v排序
    38 """
    39 class Solution:
    40     def rankTeams(self, votes):
    41         count = {v: [0] * len(votes[0]) for v in votes[0]}
    42         for a in votes:
    43             for i, v in enumerate(a):
    44                 count[v][i] -= 1
    45         return ''.join(sorted(votes[0], key=lambda v: count[v] +[v]))
    46                #这里后面加v是针对各个位置出现次数相等的情况
  • 相关阅读:
    Linux.超级管理员root-密码破解
    Hibernate.基础篇《一》.Hibernate工具类.
    不同版本Hibernate.获取SessionFactory的方式
    Hibernate.编写xml文件无自动提示信息
    ubantu中安装TensorFlow遇到的问题
    Ubuntu中安装python3.6(转)
    Linux 用户管理
    虚拟机中Linux安装(转)
    红黑树(转)
    树的遍历方法(转)
  • 原文地址:https://www.cnblogs.com/yawenw/p/12389658.html
Copyright © 2011-2022 走看看