< Summary

Information
Class: LeetCode.Algorithms.FindTheTownJudge.FindTheTownJudgeIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheTownJudge\FindTheTownJudgeIterative.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 56
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindJudge(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheTownJudge\FindTheTownJudgeIterative.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.FindTheTownJudge;
 13
 14/// <inheritdoc />
 15public class FindTheTownJudgeIterative : IFindTheTownJudge
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2 + t)
 19    ///     Space complexity - O(n + t)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <param name="trust"></param>
 23    /// <returns></returns>
 24    public int FindJudge(int n, int[][] trust)
 525    {
 526        if (trust.Length == 0)
 227        {
 228            if (n > 1)
 129            {
 130                return -1;
 31            }
 32
 133            return 1;
 34        }
 35
 336        var sum = n * (n + 1) / 2;
 37
 338        var judges = new Dictionary<int, HashSet<int>>();
 39
 2140        foreach (var t in trust)
 641        {
 642            if (!judges.TryAdd(t[1], [t[0]]))
 243            {
 244                judges[t[1]].Add(t[0]);
 245            }
 646        }
 47
 1148        foreach (var judge in judges.Where(j =>
 1149                     j.Value.Sum() == sum - j.Key && !judges.Any(v => v.Value.Contains(j.Key))))
 250        {
 251            return judge.Key;
 52        }
 53
 154        return -1;
 555    }
 56}