返回介绍

solution / 2900-2999 / 2923.Find Champion I / README_EN

发布于 2024-06-17 01:02:58 字数 4365 浏览 0 评论 0 收藏 0

2923. Find Champion I

中文文档

Description

There are n teams numbered from 0 to n - 1 in a tournament.

Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n - 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i.

Team a will be the champion of the tournament if there is no team b that is stronger than team a.

Return _the team that will be the champion of the tournament._

 

Example 1:

Input: grid = [[0,1],[0,0]]
Output: 0
Explanation: There are two teams in this tournament.
grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be the champion.

Example 2:

Input: grid = [[0,0,1],[1,0,1],[0,0,0]]
Output: 1
Explanation: There are three teams in this tournament.
grid[1][0] == 1 means that team 1 is stronger than team 0.
grid[1][2] == 1 means that team 1 is stronger than team 2.
So team 1 will be the champion.

 

Constraints:

  • n == grid.length
  • n == grid[i].length
  • 2 <= n <= 100
  • grid[i][j] is either 0 or 1.
  • For all i grid[i][i] is 0.
  • For all i, j that i != j, grid[i][j] != grid[j][i].
  • The input is generated such that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.

Solutions

Solution 1: Enumeration

We can enumerate each team $i$. If team $i$ has won every match, then team $i$ is the champion, and we can directly return $i$.

The time complexity is $O(n^2)$, where $n$ is the number of teams. The space complexity is $O(1)$.

class Solution:
  def findChampion(self, grid: List[List[int]]) -> int:
    for i, row in enumerate(grid):
      if all(x == 1 for j, x in enumerate(row) if i != j):
        return i
class Solution {
  public int findChampion(int[][] grid) {
    int n = grid.length;
    for (int i = 0;; ++i) {
      int cnt = 0;
      for (int j = 0; j < n; ++j) {
        if (i != j && grid[i][j] == 1) {
          ++cnt;
        }
      }
      if (cnt == n - 1) {
        return i;
      }
    }
  }
}
class Solution {
public:
  int findChampion(vector<vector<int>>& grid) {
    int n = grid.size();
    for (int i = 0;; ++i) {
      int cnt = 0;
      for (int j = 0; j < n; ++j) {
        if (i != j && grid[i][j] == 1) {
          ++cnt;
        }
      }
      if (cnt == n - 1) {
        return i;
      }
    }
  }
};
func findChampion(grid [][]int) int {
  n := len(grid)
  for i := 0; ; i++ {
    cnt := 0
    for j, x := range grid[i] {
      if i != j && x == 1 {
        cnt++
      }
    }
    if cnt == n-1 {
      return i
    }
  }
}
function findChampion(grid: number[][]): number {
  for (let i = 0, n = grid.length; ; ++i) {
    let cnt = 0;
    for (let j = 0; j < n; ++j) {
      if (i !== j && grid[i][j] === 1) {
        ++cnt;
      }
    }
    if (cnt === n - 1) {
      return i;
    }
  }
}

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

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

发布评论

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