2923-find-champion-i.rs (2126B)
1/// 2923. Find Champion I (Easy) 2/// 3/// There are n teams numbered from 0 to n - 1 in a tournament. 4/// 5/// Given a *0-indexed* 2D boolean matrix grid of size n * n. For all i, j that 6/// 0 <= i, j <= n - 1 and i != j team i is *stronger* than team j if grid[i][j] 7/// == 1, otherwise, team j is *stronger* than team i. 8/// 9/// Team a will be the *champion* of the tournament if there is no team b that 10/// is stronger than team a. 11/// 12/// Return the team that will be the champion of the tournament. 13/// 14/// *Example 1:* 15/// 16/// *Input:* grid = [[0,1],[0,0]] 17/// *Output:* 0 18/// *Explanation:* There are two teams in this tournament. 19/// grid[0][1] == 1 means that team 0 is stronger than team 1. So team 0 will be 20/// the champion. 21/// 22/// *Example 2:* 23/// 24/// *Input:* grid = [[0,0,1],[1,0,1],[0,0,0]] 25/// *Output:* 1 26/// *Explanation:* There are three teams in this tournament. 27/// grid[1][0] == 1 means that team 1 is stronger than team 0. 28/// grid[1][2] == 1 means that team 1 is stronger than team 2. 29/// So team 1 will be the champion. 30/// 31/// *Constraints:* 32/// 33/// * n == grid.length 34/// * n == grid[i].length 35/// * 2 <= n <= 100 36/// * grid[i][j] is either 0 or 1. 37/// * For all i grid[i][i] is 0. 38/// * For all i, j that i != j, grid[i][j] != grid[j][i]. 39/// * The input is generated such that if team a is stronger than team b and 40/// team b is stronger than team c, then team a is stronger than team c. 41/// 42use leetcode::*; 43 44struct Solution {} 45 46impl Solution { 47 pub fn find_champion(grid: Vec<Vec<i32>>) -> i32 { 48 for (y, row) in grid.iter().enumerate() { 49 if row.iter().sum::<i32>() == row.len() as i32 - 1 && row[y] == 0 { 50 return y as i32; 51 } 52 } 53 unreachable!() 54 } 55} 56 57pub fn main() { 58 println!("{}", Solution::find_champion(arg_into(1))); 59} 60 61#[cfg(test)] 62mod tests { 63 use crate::Solution; 64 use leetcode::vvi; 65 66 #[test] 67 fn test() { 68 assert_eq!(Solution::find_champion(vvi("[[0,1],[0,0]]")), 0); 69 assert_eq!(Solution::find_champion(vvi("[[0,0,1],[1,0,1],[0,0,0]]")), 1); 70 } 71}