< Summary

Information
Class: LeetCode.Algorithms.FindTheSafestPathInGrid.FindTheSafestPathInGridPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheSafestPathInGrid\FindTheSafestPathInGridPriorityQueue.cs
Line coverage
98%
Covered lines: 65
Uncovered lines: 1
Coverable lines: 66
Total lines: 119
Line coverage: 98.4%
Branch coverage
97%
Covered branches: 43
Total branches: 44
Branch coverage: 97.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaximumSafenessFactor(...)97.72%444498.48%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheSafestPathInGrid\FindTheSafestPathInGridPriorityQueue.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.FindTheSafestPathInGrid;
 13
 14/// <inheritdoc />
 15public class FindTheSafestPathInGridPriorityQueue : IFindTheSafestPathInGrid
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2 log n^2)
 19    ///     Space complexity - O(n^2)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int MaximumSafenessFactor(IList<IList<int>> grid)
 324    {
 325        var n = grid.Count;
 26
 327        var thieves = new List<(int, int)>();
 28
 2629        for (var r = 0; r < n; r++)
 1030        {
 8831            for (var c = 0; c < n; c++)
 3432            {
 3433                if (grid[r][c] == 1)
 534                {
 535                    thieves.Add((r, c));
 536                }
 3437            }
 1038        }
 39
 340        var distance = new int[n][];
 41
 2642        for (var i = 0; i < n; i++)
 1043        {
 1044            distance[i] = new int[n];
 45
 1046            Array.Fill(distance[i], int.MaxValue);
 1047        }
 48
 349        var queue = new Queue<(int, int)>();
 50
 1951        foreach (var thief in thieves)
 552        {
 553            queue.Enqueue(thief);
 54
 555            distance[thief.Item1][thief.Item2] = 0;
 556        }
 57
 358        int[][] directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
 59
 3760        while (queue.Count > 0)
 3461        {
 3462            var (r, c) = queue.Dequeue();
 63
 37464            foreach (var dir in directions)
 13665            {
 27266                int nr = r + dir[0], nc = c + dir[1];
 67
 13668                if (nr < 0 || nr >= n || nc < 0 || nc >= n || distance[nr][nc] != int.MaxValue)
 10769                {
 10770                    continue;
 71                }
 72
 2973                distance[nr][nc] = distance[r][c] + 1;
 74
 2975                queue.Enqueue((nr, nc));
 2976            }
 3477        }
 78
 6279        var priorityQueue = new PriorityQueue<(int, int, int), int>(Comparer<int>.Create((a, b) => b - a));
 80
 381        priorityQueue.Enqueue((distance[0][0], 0, 0), distance[0][0]);
 82
 383        var visited = new bool[n][];
 84
 2685        for (var i = 0; i < n; i++)
 1086        {
 1087            visited[i] = new bool[n];
 1088        }
 89
 390        visited[0][0] = true;
 91
 2492        while (priorityQueue.Count > 0)
 2493        {
 2494            var (minDist, r, c) = priorityQueue.Dequeue();
 95
 2496            if (r == n - 1 && c == n - 1)
 397            {
 398                return minDist;
 99            }
 100
 231101            foreach (var dir in directions)
 84102            {
 168103                int nr = r + dir[0], nc = c + dir[1];
 104
 84105                if (nr < 0 || nr >= n || nc < 0 || nc >= n || visited[nr][nc])
 56106                {
 56107                    continue;
 108                }
 109
 28110                visited[nr][nc] = true;
 111
 28112                priorityQueue.Enqueue((Math.Min(minDist, distance[nr][nc]), nr, nc),
 28113                    Math.Min(minDist, distance[nr][nc]));
 28114            }
 21115        }
 116
 0117        return 0;
 3118    }
 119}