< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
MinimumObstacles(...)100%1818100%
InitializeMinimumObstacles(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumObstacleRemovalToReachCorner\MinimumObstacleRemovalToReachCornerPriorityQueue.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.MinimumObstacleRemovalToReachCorner;
 13
 14/// <inheritdoc />
 15public sealed class MinimumObstacleRemovalToReachCornerPriorityQueue : IMinimumObstacleRemovalToReachCorner
 16{
 117    private static readonly (int X, int Y)[] Directions = [(-1, 0), (1, 0), (0, -1), (0, 1)];
 18
 19    /// <summary>
 20    ///     Time complexity - O(m * n * log(m * n)), where m is the number of rows and n is the number of columns in the
 21    ///     Space complexity - O(m * n), where m is the number of rows and n is the number of columns in the grid
 22    /// </summary>
 23    /// <param name="grid"></param>
 24    /// <returns></returns>
 25    public int MinimumObstacles(int[][] grid)
 226    {
 227        var rowsCount = grid.Length;
 228        var columnsCount = grid[0].Length;
 29
 230        var minimumObstacles = InitializeMinimumObstacles(rowsCount, columnsCount);
 31
 232        var cellStatesPriorityQueue = new PriorityQueue<(int Row, int Column), int>();
 33
 234        cellStatesPriorityQueue.Enqueue((0, 0), 0);
 35
 1936        while (cellStatesPriorityQueue.Count > 0)
 1937        {
 1938            var (row, column) = cellStatesPriorityQueue.Dequeue();
 39
 1940            if (row == rowsCount - 1 && column == columnsCount - 1)
 241            {
 242                break;
 43            }
 44
 18745            foreach (var direction in Directions)
 6846            {
 6847                var nextRow = row + direction.X;
 6848                var nextColumn = column + direction.Y;
 49
 6850                if (nextRow < 0 || nextRow >= rowsCount || nextColumn < 0 || nextColumn >= columnsCount)
 2151                {
 2152                    continue;
 53                }
 54
 4755                var nextObstaclesCount = minimumObstacles[row, column] + grid[nextRow][nextColumn];
 56
 4757                if (nextObstaclesCount >= minimumObstacles[nextRow, nextColumn])
 2558                {
 2559                    continue;
 60                }
 61
 2262                minimumObstacles[nextRow, nextColumn] = nextObstaclesCount;
 63
 2264                cellStatesPriorityQueue.Enqueue((nextRow, nextColumn), nextObstaclesCount);
 2265            }
 1766        }
 67
 268        return minimumObstacles[rowsCount - 1, columnsCount - 1];
 269    }
 70
 71    private static int[,] InitializeMinimumObstacles(int rowsCount, int columnsCount)
 272    {
 273        var minimumObstacles = new int[rowsCount, columnsCount];
 74
 1675        for (var row = 0; row < rowsCount; row++)
 676        {
 6077            for (var column = 0; column < columnsCount; column++)
 2478            {
 2479                minimumObstacles[row, column] = int.MaxValue;
 2480            }
 681        }
 82
 283        minimumObstacles[0, 0] = 0;
 84
 285        return minimumObstacles;
 286    }
 87}