[LeetCode] Find Champion I

2923. Find Champion I

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int findChampion(vector<vector<int>>& grid) {
for(int i = 0; i < grid.size(); i++) {
int cnt = 0;
for(int j = 0; j < grid[i].size(); j++) cnt += grid[i][j];
if(cnt == grid[i].size() - 1) return i;
}
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/11/05/PS/Codeforces/find-champion-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.