返回介绍

solution / 1300-1399 / 1366.Rank Teams by Votes / README_EN

发布于 2024-06-17 01:03:20 字数 5802 浏览 0 评论 0 收藏 0

1366. Rank Teams by Votes

中文文档

Description

In a special ranking system, each voter gives a rank from highest to lowest to all teams participating 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.

You are 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 ranked third by 3 voters.
Team C was ranked second by 3 voters and 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 the tie-breaking rule. X has the same votes as W for the first position, but X has one vote in the second position, while W does not have any votes in the second position. 

Example 3:

Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
Explanation: Only one voter, so their votes are used for the ranking.

 

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 uppercase 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.

Solutions

Solution 1: Counting + Custom Sorting

For each candidate, we can count the number of votes they receive in each ranking, and then compare the number of votes according to different rankings. If the number of votes is the same, we compare the letters.

The time complexity is $O(n^2 \times \log n)$, and the space complexity is $O(n^2)$. Where $n$ is the number of candidates.

class Solution:
  def rankTeams(self, votes: List[str]) -> str:
    n = len(votes[0])
    cnt = defaultdict(lambda: [0] * n)
    for vote in votes:
      for i, c in enumerate(vote):
        cnt[c][i] += 1
    return "".join(sorted(votes[0], key=lambda x: (cnt[x], -ord(x)), reverse=True))
class Solution {
  public String rankTeams(String[] votes) {
    int n = votes[0].length();
    int[][] cnt = new int[26][n];
    for (var vote : votes) {
      for (int i = 0; i < n; ++i) {
        cnt[vote.charAt(i) - 'A'][i]++;
      }
    }
    Character[] cs = new Character[n];
    for (int i = 0; i < n; ++i) {
      cs[i] = votes[0].charAt(i);
    }
    Arrays.sort(cs, (a, b) -> {
      int i = a - 'A', j = b - 'A';
      for (int k = 0; k < n; ++k) {
        int d = cnt[i][k] - cnt[j][k];
        if (d != 0) {
          return d > 0 ? -1 : 1;
        }
      }
      return a - b;
    });
    StringBuilder ans = new StringBuilder();
    for (char c : cs) {
      ans.append(c);
    }
    return ans.toString();
  }
}
class Solution {
public:
  string rankTeams(vector<string>& votes) {
    int n = votes[0].size();
    int cnt[26][n];
    memset(cnt, 0, sizeof cnt);
    for (auto& vote : votes) {
      for (int i = 0; i < n; ++i) {
        cnt[vote[i] - 'A'][i]++;
      }
    }
    string ans = votes[0];
    sort(ans.begin(), ans.end(), [&](auto& a, auto& b) {
      int i = a - 'A', j = b - 'A';
      for (int k = 0; k < n; ++k) {
        if (cnt[i][k] != cnt[j][k]) {
          return cnt[i][k] > cnt[j][k];
        }
      }
      return a < b;
    });
    return ans;
  }
};
func rankTeams(votes []string) string {
  cnt := [26][26]int{}
  for _, vote := range votes {
    for i, c := range vote {
      cnt[c-'A'][i]++
    }
  }
  ans := []byte(votes[0])
  sort.Slice(ans, func(i, j int) bool {
    cnt1, cnt2 := cnt[ans[i]-'A'], cnt[ans[j]-'A']
    for k, a := range cnt1 {
      b := cnt2[k]
      if a != b {
        return a > b
      }
    }
    return ans[i] < ans[j]
  })
  return string(ans)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文