| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.FindChampion2; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindChampion2Array : IFindChampion2 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(e + n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="n"></param> |
| | 22 | | /// <param name="edges"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int FindChampion(int n, int[][] edges) |
| 4 | 25 | | { |
| 4 | 26 | | var hasIncomingEdge = new bool[n]; |
| | 27 | |
|
| 22 | 28 | | foreach (var edge in edges) |
| 5 | 29 | | { |
| 5 | 30 | | hasIncomingEdge[edge[1]] = true; |
| 5 | 31 | | } |
| | 32 | |
|
| 4 | 33 | | var champion = -1; |
| | 34 | |
|
| 20 | 35 | | for (var i = 0; i < n; i++) |
| 8 | 36 | | { |
| 8 | 37 | | if (hasIncomingEdge[i]) |
| 2 | 38 | | { |
| 2 | 39 | | continue; |
| | 40 | | } |
| | 41 | |
|
| 6 | 42 | | if (champion != -1) |
| 2 | 43 | | { |
| 2 | 44 | | return -1; |
| | 45 | | } |
| | 46 | |
|
| 4 | 47 | | champion = i; |
| 4 | 48 | | } |
| | 49 | |
|
| 2 | 50 | | return champion; |
| 4 | 51 | | } |
| | 52 | | } |