diff --git a/leetcode/2901-3000/2924.Find-Champion-II/1.png b/leetcode/2901-3000/2924.Find-Champion-II/1.png new file mode 100644 index 000000000..f474a6491 Binary files /dev/null and b/leetcode/2901-3000/2924.Find-Champion-II/1.png differ diff --git a/leetcode/2901-3000/2924.Find-Champion-II/2.png b/leetcode/2901-3000/2924.Find-Champion-II/2.png new file mode 100644 index 000000000..9efdcd573 Binary files /dev/null and b/leetcode/2901-3000/2924.Find-Champion-II/2.png differ diff --git a/leetcode/2901-3000/2924.Find-Champion-II/README.md b/leetcode/2901-3000/2924.Find-Champion-II/README.md index e70b0d4b0..ac7cd1951 100755 --- a/leetcode/2901-3000/2924.Find-Champion-II/README.md +++ b/leetcode/2901-3000/2924.Find-Champion-II/README.md @@ -1,28 +1,40 @@ # [2924.Find Champion II][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +There are `n` teams numbered from `0` to `n - 1` in a tournament; each team is also a node in a **DAG**. + +You are given the integer `n` and a **0-indexed** 2D integer array `edges` of length `m` representing the **DAG**, where `edges[i] = [ui, vi]` indicates that there is a directed edge from team `ui` to team `vi` in the graph. + +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`. + +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 if there is a **unique** champion, otherwise, return `-1`. -**Example 1:** +**Notes** + +- 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]`. +- A **DAG** is a directed graph that does not have any **cycle**. + +**Example 1:** + +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 3, edges = [[0,1],[1,2]] +Output: 0 +Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.png) -### 思路1 -> ... -Find Champion II -```go ``` - +Input: n = 4, edges = [[0,2],[1,3],[1,2]] +Output: -1 +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. +``` ## 结语 diff --git a/leetcode/2901-3000/2924.Find-Champion-II/Solution.go b/leetcode/2901-3000/2924.Find-Champion-II/Solution.go index d115ccf5e..77ba2e1f7 100644 --- a/leetcode/2901-3000/2924.Find-Champion-II/Solution.go +++ b/leetcode/2901-3000/2924.Find-Champion-II/Solution.go @@ -1,5 +1,20 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, edges [][]int) int { + in := make(map[int]int) + for _, e := range edges { + in[e[1]]++ + } + ans := -1 + cur := 0 + for i := range n { + if in[i] == 0 { + cur++ + ans = i + if cur > 1 { + return -1 + } + } + } + return ans } diff --git a/leetcode/2901-3000/2924.Find-Champion-II/Solution_test.go b/leetcode/2901-3000/2924.Find-Champion-II/Solution_test.go index 14ff50eb4..88c2e064a 100644 --- a/leetcode/2901-3000/2924.Find-Champion-II/Solution_test.go +++ b/leetcode/2901-3000/2924.Find-Champion-II/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n int + edges [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, [][]int{{0, 1}, {1, 2}}, 0}, + {"TestCase2", 4, [][]int{{0, 2}, {1, 3}, {1, 2}}, -1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.edges) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n, c.edges) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }