< Summary

Information
Class: LeetCode.Algorithms.FindEventualSafeStates.FindEventualSafeStatesDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindEventualSafeStates\FindEventualSafeStatesDepthFirstSearch.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EventualSafeNodes(...)100%44100%
IsSafeNode(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindEventualSafeStates\FindEventualSafeStatesDepthFirstSearch.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.FindEventualSafeStates;
 13
 14/// <inheritdoc />
 15public sealed class FindEventualSafeStatesDepthFirstSearch : IFindEventualSafeStates
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + E), where n is a number of nodes, E is a number of edges
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="graph"></param>
 22    /// <returns></returns>
 23    public IList<int> EventualSafeNodes(int[][] graph)
 224    {
 225        var n = graph.Length;
 26
 227        Span<int> states = stackalloc int[n];
 28
 229        var result = new List<int>();
 30
 2831        for (var node = 0; node < n; node++)
 1232        {
 1233            if (!IsSafeNode(node, graph, states))
 734            {
 735                continue;
 36            }
 37
 538            result.Add(node);
 539        }
 40
 241        return result;
 242    }
 43
 44    private static bool IsSafeNode(int node, int[][] graph, Span<int> states)
 2245    {
 2246        switch (states[node])
 47        {
 348            case 1: return true;
 749            case 2: return false;
 50        }
 51
 1252        var adjacentNodes = graph[node];
 1253        var adjacentNodesLength = adjacentNodes.Length;
 54
 1255        states[node] = 2;
 56
 3057        for (var i = 0; i < adjacentNodesLength; i++)
 1058        {
 1059            var adjacentNode = graph[node][i];
 60
 1061            if (IsSafeNode(adjacentNode, graph, states))
 362            {
 363                continue;
 64            }
 65
 766            return false;
 67        }
 68
 569        states[node] = 1;
 70
 571        return true;
 2272    }
 73}