2924. Find Champion II Medium
1/**
2 * [2924] Find Champion II
3 *
4 * There are n teams numbered from 0 to n - 1 in a tournament; each team is also a node in a DAG.
5 * You are given the integer n and a 0-indexed 2D integer array edges of length <font face="monospace">m</font> representing the DAG, where edges[i] = [ui, vi] indicates that there is a directed edge from team ui to team vi in the graph.
6 * A directed edge from a to b in the graph means that team a is stronger than team b and team b is weaker than team a.
7 * Team a will be the champion of the tournament if there is no team b that is stronger than team a.
8 * Return the team that will be the champion of the tournament if there is a unique champion, otherwise, return -1.
9 * Notes
10 *
11 * A cycle is a series of nodes a1, a2, ..., an, an+1 such that node a1 is the same node as node an+1, the nodes a1, a2, ..., an are distinct, and there is a directed edge from the node ai to node ai+1 for every i in the range [1, n].
12 * A DAG is a directed graph that does not have any cycle.
13 *
14 *
15 * <strong class="example">Example 1:
16 * <img height="300" src="https://assets.leetcode.com/uploads/2023/10/19/graph-3.png" width="300" />
17 *
18 * Input: n = 3, edges = [[0,1],[1,2]]
19 * Output: 0
20 * Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
21 *
22 * <strong class="example">Example 2:
23 * <img height="300" src="https://assets.leetcode.com/uploads/2023/10/19/graph-4.png" width="300" />
24 *
25 * Input: n = 4, edges = [[0,2],[1,3],[1,2]]
26 * Output: -1
27 * Explanation: Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= n <= 100
33 * m == edges.length
34 * 0 <= m <= n * (n - 1) / 2
35 * edges[i].length == 2
36 * 0 <= edge[i][j] <= n - 1
37 * edges[i][0] != edges[i][1]
38 * The input is generated such that if team a is stronger than team b, team b is not stronger than team a.
39 * 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.
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/find-champion-ii/
45// discuss: https://leetcode.com/problems/find-champion-ii/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn find_champion(n: i32, edges: Vec<Vec<i32>>) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_2924() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.