< Summary

Information
Class: LeetCode.Algorithms.MinimumCostToMakeAtLeastOneValidPathInGrid.MinimumCostToMakeAtLeastOneValidPathInGridBreadthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumCostToMakeAtLeastOneValidPathInGrid\MinimumCostToMakeAtLeastOneValidPathInGridBreadthFirstSearch.cs
Line coverage
100%
Covered lines: 52
Uncovered lines: 0
Coverable lines: 52
Total lines: 95
Line coverage: 100%
Branch coverage
100%
Covered branches: 24
Total branches: 24
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
MinCost(...)100%2424100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumCostToMakeAtLeastOneValidPathInGrid\MinimumCostToMakeAtLeastOneValidPathInGridBreadthFirstSearch.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.MinimumCostToMakeAtLeastOneValidPathInGrid;
 13
 14/// <inheritdoc />
 15public class MinimumCostToMakeAtLeastOneValidPathInGridBreadthFirstSearch : IMinimumCostToMakeAtLeastOneValidPathInGrid
 16{
 317    private readonly (int X, int Y)[] _directions =
 318    [
 319        (0, 1),
 320        (0, -1),
 321        (1, 0),
 322        (-1, 0)
 323    ];
 24
 25    /// <summary>
 26    ///     Time complexity - O(n * m)
 27    ///     Space complexity - O(n * m)
 28    /// </summary>
 29    /// <param name="grid"></param>
 30    /// <returns></returns>
 31    public int MinCost(int[][] grid)
 332    {
 333        var m = grid.Length;
 334        var n = grid[0].Length;
 35
 336        var cost = new int[m, n];
 37
 2438        for (var i = 0; i < m; i++)
 939        {
 7640            for (var j = 0; j < n; j++)
 2941            {
 2942                cost[i, j] = int.MaxValue;
 2943            }
 944        }
 45
 346        cost[0, 0] = 0;
 47
 348        var linkedList = new LinkedList<(int X, int Y, int CurrentCost)>();
 49
 350        linkedList.AddFirst((0, 0, 0));
 51
 3652        while (linkedList.First != null)
 3353        {
 3354            var (x, y, currentCost) = linkedList.First.Value;
 55
 3356            linkedList.RemoveFirst();
 57
 3358            if (currentCost > cost[x, y])
 459            {
 460                continue;
 61            }
 62
 29063            for (var d = 0; d < _directions.Length; d++)
 11664            {
 11665                var nx = x + _directions[d].X;
 11666                var ny = y + _directions[d].Y;
 67
 11668                var newCost = currentCost;
 69
 11670                if (grid[x][y] != d + 1)
 8771                {
 8772                    newCost++;
 8773                }
 74
 11675                if (nx < 0 || ny < 0 || nx >= m || ny >= n || newCost >= cost[nx, ny])
 8676                {
 8677                    continue;
 78                }
 79
 3080                cost[nx, ny] = newCost;
 81
 3082                if (grid[x][y] == d + 1)
 1283                {
 1284                    linkedList.AddFirst((nx, ny, newCost));
 1285                }
 86                else
 1887                {
 1888                    linkedList.AddLast((nx, ny, newCost));
 1889                }
 3090            }
 2991        }
 92
 393        return cost[m - 1, n - 1];
 394    }
 95}

Methods/Properties

.ctor()
MinCost(System.Int32[][])