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.
Input: grid =[[0,1],[0,0]]Output: 0Explanation: There are two teams inthis tournament.grid[0][1]==1 means that team 0is stronger than team 1. So team 0 will be the champion.
Input: grid =[[0,0,1],[1,0,1],[0,0,0]]Output: 1Explanation: There are three teams inthis tournament.grid[1][0]==1 means that team 1is stronger than team 0.grid[1][2]==1 means that team 1is stronger than team 2.So team 1 will be the champion.
The champion is the team that is not beaten by any other team. In the grid, this means the row for the champion has all 1s (except the diagonal), i.e., the champion beats every other team.
classSolution {
publicintfindChampion(int[][] grid) {
int n = grid.length;
for (int i = 0; i < n; i++) {
boolean champ =true;
for (int j = 0; j < n; j++) {
if (i != j && grid[i][j]== 0) {
champ =false;
break;
}
}
if (champ) return i;
}
return-1;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funfindChampion(grid: Array<IntArray>): Int {
val n = grid.size
for (i in0 until n) {
var champ = truefor (j in0 until n) {
if (i != j && grid[i][j] ==0) {
champ = falsebreak }
}
if (champ) return i
}
return -1 }
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
deffindChampion(self, grid: list[list[int]]) -> int:
n = len(grid)
for i in range(n):
champ =Truefor j in range(n):
if i != j and grid[i][j] ==0:
champ =Falsebreakif champ:
return i
return-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnfind_champion(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
for i in0..n {
letmut champ =true;
for j in0..n {
if i != j && grid[i][j] ==0 {
champ =false;
break;
}
}
if champ {
return i asi32;
}
}
-1 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
findChampion(grid: number[][]):number {
constn=grid.length;
for (leti=0; i<n; i++) {
letchamp=true;
for (letj=0; j<n; j++) {
if (i!==j&&grid[i][j] ===0) {
champ=false;
break;
}
}
if (champ) returni;
}
return-1;
}
}