Skip to content

Commit d81768c

Browse files
authored
Merge pull request #1044 from 0xff-dev/2924
Add solution and test-cases for problem 2924
2 parents f7e9312 + e8f374e commit d81768c

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed
9.83 KB
Loading
10.7 KB
Loading

leetcode/2901-3000/2924.Find-Champion-II/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [2924.Find Champion II][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There are `n` teams numbered from `0` to `n - 1` in a tournament; each team is also a node in a **DAG**.
5+
6+
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.
7+
8+
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`.
9+
10+
Team `a` will be the **champion** of the tournament if there is no team `b` that is **stronger** than team `a`.
11+
12+
Return the team that will be the **champion** of the tournament if there is a **unique** champion, otherwise, return `-1`.
713

8-
**Example 1:**
14+
**Notes**
15+
16+
- 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]`.
17+
- A **DAG** is a directed graph that does not have any **cycle**.
18+
19+
**Example 1:**
20+
21+
![1](./1.png)
922

1023
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
24+
Input: n = 3, edges = [[0,1],[1,2]]
25+
Output: 0
26+
Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
1327
```
1428

15-
## 题意
16-
> ...
29+
**Example 2:**
1730

18-
## 题解
31+
![2](./2.png)
1932

20-
### 思路1
21-
> ...
22-
Find Champion II
23-
```go
2433
```
25-
34+
Input: n = 4, edges = [[0,2],[1,3],[1,2]]
35+
Output: -1
36+
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.
37+
```
2638

2739
## 结语
2840

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, edges [][]int) int {
4+
in := make(map[int]int)
5+
for _, e := range edges {
6+
in[e[1]]++
7+
}
8+
ans := -1
9+
cur := 0
10+
for i := range n {
11+
if in[i] == 0 {
12+
cur++
13+
ans = i
14+
if cur > 1 {
15+
return -1
16+
}
17+
}
18+
}
19+
return ans
520
}

leetcode/2901-3000/2924.Find-Champion-II/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
edges [][]int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 3, [][]int{{0, 1}, {1, 2}}, 0},
18+
{"TestCase2", 4, [][]int{{0, 2}, {1, 3}, {1, 2}}, -1},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.n, c.edges)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.n, c.edges)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)