#2923

Find Champion I

newbie · 270 · lc easy +19 · verified · 73.3% accepted · 187 likes · top 83%

Description

A tournament has n teams numbered 0 to n - 1. A 0-indexed n * n boolean matrix grid encodes strength: for distinct i and j, grid[i][j] == 1 means team i beats team j.

The champion is the team that defeats every other team.

Return that team's index.

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.

Code

1
2
3