LeetCode 题解工作台

通过投票对团队排名

现在有一个特殊的排名系统,依据参赛团队在投票人心中的次序进行排名,每个投票者都需要按从高到低的顺序对参与排名的所有团队进行排位。 排名规则如下: 参赛团队的排名次序依照其所获「排位第一」的票的多少决定。如果存在多个团队并列的情况,将继续考虑其「排位第二」的票的数量。以此类推,直到不再存在并列的情况。…

category

5

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

对于每个候选人,我们可以统计他在每个排位上的票数,然后根据不同的排位依次比较票数,票数相同则比较字母。 时间复杂度 $O(n \times m + m^2 \times \log m)$,空间复杂度 。其中 是 的长度,而 是候选人的数量,即 的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

现在有一个特殊的排名系统,依据参赛团队在投票人心中的次序进行排名,每个投票者都需要按从高到低的顺序对参与排名的所有团队进行排位。

排名规则如下:

  • 参赛团队的排名次序依照其所获「排位第一」的票的多少决定。如果存在多个团队并列的情况,将继续考虑其「排位第二」的票的数量。以此类推,直到不再存在并列的情况。
  • 如果在考虑完所有投票情况后仍然出现并列现象,则根据团队字母的字母顺序进行排名。

给你一个字符串数组 votes 代表全体投票者给出的排位情况,请你根据上述排名规则对所有参赛团队进行排名。

请你返回能表示按排名系统 排序后 的所有团队排名的字符串。

 

示例 1:

输入:votes = ["ABC","ACB","ABC","ACB","ACB"]
输出:"ACB"
解释:
A 队获得五票「排位第一」,没有其他队获得「排位第一」,所以 A 队排名第一。
B 队获得两票「排位第二」,三票「排位第三」。
C 队获得三票「排位第二」,两票「排位第三」。
由于 C 队「排位第二」的票数较多,所以 C 队排第二,B 队排第三。

示例 2:

输入:votes = ["WXYZ","XYZW"]
输出:"XWYZ"
解释:
X 队在并列僵局打破后成为排名第一的团队。X 队和 W 队的「排位第一」票数一样,但是 X 队有一票「排位第二」,而 W 没有获得「排位第二」。 

示例 3:

输入:votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
输出:"ZMNAGUEDSJYLBOPHRQICWFXTVK"
解释:只有一个投票者,所以排名完全按照他的意愿。

 

提示:

  • 1 <= votes.length <= 1000
  • 1 <= votes[i].length <= 26
  • votes[i].length == votes[j].length for 0 <= i, j < votes.length
  • votes[i][j] 是英文 大写 字母
  • votes[i] 中的所有字母都是唯一的
  • votes[0] 中出现的所有字母 同样也 出现在 votes[j] 中,其中 1 <= j < votes.length
lightbulb

解题思路

方法一:计数 + 自定义排序

对于每个候选人,我们可以统计他在每个排位上的票数,然后根据不同的排位依次比较票数,票数相同则比较字母。

时间复杂度 O(n×m+m2×logm)O(n \times m + m^2 \times \log m),空间复杂度 O(m2)O(m^2)。其中 nnvotes\textit{votes} 的长度,而 mm 是候选人的数量,即 votes[0]\textit{votes}[0] 的长度。

1
2
3
4
5
6
7
8
9
class Solution:
    def rankTeams(self, votes: List[str]) -> str:
        m = len(votes[0])
        cnt = defaultdict(lambda: [0] * m)
        for vote in votes:
            for i, c in enumerate(vote):
                cnt[c][i] += 1
        return "".join(sorted(cnt, key=lambda c: (cnt[c], -ord(c)), reverse=True))
speed

复杂度分析

指标
时间complexity is O(T * N + N log N) where T is the number of teams and N is the number of votes, due to scanning votes and sorting teams. Space complexity is O(T * T) for storing counts per team per position.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Checks if you correctly implement position-based counting using an array or hash table.

  • question_mark

    Observes whether tie-breaking by subsequent positions and alphabet is handled accurately.

  • question_mark

    Looks for correct sorting logic combining count comparisons and alphabetical order.

warning

常见陷阱

外企场景
  • error

    Forgetting to count votes for all positions, not just first place.

  • error

    Failing to implement proper tie-breaking logic across all positions.

  • error

    Misordering alphabetically when teams remain tied after considering all positions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Limiting team count to 10, which allows a fixed-size array approach.

  • arrow_right_alt

    Allowing partial votes where voters rank only some teams, requiring default zero counts.

  • arrow_right_alt

    Adding weighted votes where earlier positions carry more points, requiring modified counting logic.

help

常见问题

外企场景

通过投票对团队排名题解:数组·哈希·扫描 | LeetCode #1366 中等