< Summary

Information
Class: LeetCode.Algorithms.MinimumObstacleRemovalToReachCorner.MinimumObstacleRemovalToReachCornerBreadthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumObstacleRemovalToReachCorner\MinimumObstacleRemovalToReachCornerBreadthFirstSearch.cs
Line coverage
100%
Covered lines: 46
Uncovered lines: 0
Coverable lines: 46
Total lines: 91
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
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%1616100%
InitializeMinimumObstacles(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumObstacleRemovalToReachCorner\MinimumObstacleRemovalToReachCornerBreadthFirstSearch.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 MinimumObstacleRemovalToReachCornerBreadthFirstSearch : 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), where m is the number of rows and n is the number of columns in the grid
 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 cellStatesLinkedList = new LinkedList<(int Row, int Column)>();
 33
 234        cellStatesLinkedList.AddFirst((0, 0));
 35
 2636        while (cellStatesLinkedList.First != null)
 2437        {
 2438            var (row, column) = cellStatesLinkedList.First.Value;
 39
 2440            cellStatesLinkedList.RemoveFirst();
 41
 26442            foreach (var direction in Directions)
 9643            {
 9644                var nextRow = row + direction.X;
 9645                var nextColumn = column + direction.Y;
 46
 9647                if (nextRow < 0 || nextRow >= rowsCount || nextColumn < 0 || nextColumn >= columnsCount)
 2848                {
 2849                    continue;
 50                }
 51
 6852                var nextObstaclesCount = minimumObstacles[row, column] + grid[nextRow][nextColumn];
 53
 6854                if (nextObstaclesCount >= minimumObstacles[nextRow, nextColumn])
 4655                {
 4656                    continue;
 57                }
 58
 2259                minimumObstacles[nextRow, nextColumn] = nextObstaclesCount;
 60
 2261                if (grid[nextRow][nextColumn] == 1)
 1062                {
 1063                    cellStatesLinkedList.AddLast((nextRow, nextColumn));
 1064                }
 65                else
 1266                {
 1267                    cellStatesLinkedList.AddFirst((nextRow, nextColumn));
 1268                }
 2269            }
 2470        }
 71
 272        return minimumObstacles[rowsCount - 1, columnsCount - 1];
 273    }
 74
 75    private static int[,] InitializeMinimumObstacles(int rowsCount, int columnsCount)
 276    {
 277        var minimumObstacles = new int[rowsCount, columnsCount];
 78
 1679        for (var row = 0; row < rowsCount; row++)
 680        {
 6081            for (var column = 0; column < columnsCount; column++)
 2482            {
 2483                minimumObstacles[row, column] = int.MaxValue;
 2484            }
 685        }
 86
 287        minimumObstacles[0, 0] = 0;
 88
 289        return minimumObstacles;
 290    }
 91}