Skip to content

Commit 5cedd10

Browse files
Add Find Champion II
1 parent 1301823 commit 5cedd10

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

Find Champion II/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Find Champion II
2+
3+
## LeetCode Problem
4+
- Problem Number: 2924
5+
- Problem Link: https://leetcode.com/problems/find-champion-ii/description/
6+
- Difficulty: Medium
7+
- Topics: Graph, Directed Acyclic Graph (DAG)
8+
9+
## Problem Description
10+
There are n teams numbered from 0 to n - 1 in a tournament; each team is also a node in a DAG (Directed Acyclic Graph).
11+
12+
Given:
13+
- An integer n (number of teams)
14+
- A 0-indexed 2D integer array `edges` of length m representing the DAG
15+
- `edges[i] = [ui, vi]` indicates a directed edge from team ui to team vi
16+
- A directed edge from a to b means team a is stronger than team b
17+
18+
A team will be the champion if there is no other team that is stronger than it.
19+
20+
Return:
21+
- The team number that will be the champion if there is a unique champion
22+
- -1 if there is no unique champion
23+
24+
## Examples
25+
26+
### Example 1:
27+
28+
### Notes
29+
- A cycle is a series of nodes a1, a2, ..., an, an+1 where:
30+
- Node a1 is the same as node an+1
31+
- Nodes a1, a2, ..., an are distinct
32+
- There is a directed edge from node ai to node ai+1 for every i in [1, n]
33+
- A DAG is a directed graph that does not have any cycle

Find Champion II/Solution.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int findChampion(int n, int[][] edges) {
3+
int[] inDegree = new int[n];
4+
5+
for (int[] edge : edges) {
6+
int from = edge[0];
7+
int to = edge[1];
8+
inDegree[to]++;
9+
}
10+
11+
int champion = -1;
12+
for (int i = 0; i < n; i++) {
13+
if (inDegree[i] == 0) {
14+
if (champion != -1) {
15+
return -1;
16+
}
17+
champion = i;
18+
}
19+
}
20+
21+
return champion;
22+
}
23+
}

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
1-
# Solving-LeetCode-Problems
2-
This repository contains solutions to the problems I have solved on LeetCode. I will continue to update it with my progress.
1+
# Solving LeetCode Problems
2+
3+
This repository contains my solutions to various LeetCode problems. Each solution is implemented in Java and includes detailed explanations.
4+
5+
## Repository Structure
6+
7+
Each problem has its own directory containing:
8+
- `README.md` - Problem description, examples, and approach
9+
- `Solution.java` - Java implementation of the solution
10+
11+
## Problems Solved
12+
13+
| # | Title | Difficulty | Solution | Topics |
14+
|---|-------|------------|----------|---------|
15+
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
16+
17+
## Categories
18+
19+
### By Difficulty
20+
- Easy: 0
21+
- Medium: 1
22+
- Hard: 0
23+
24+
### By Topics
25+
- Graph Problems
26+
- [Find Champion II](Find%20Champion%20II)
27+
28+
## About LeetCode
29+
30+
[LeetCode](https://leetcode.com) is a platform for preparing technical coding interviews. It has a vast collection of problems covering various topics in computer science.
31+
32+
## Contributing
33+
34+
While this repository primarily contains my personal solutions, feel free to:
35+
- Suggest improvements to existing solutions
36+
- Point out bugs or issues
37+
- Discuss alternative approaches
38+
39+
## License
40+
41+
This repository is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)