< Summary

Information
Class: LeetCode.Algorithms.CountUnguardedCellsInTheGrid.CountUnguardedCellsInTheGridSimulation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountUnguardedCellsInTheGrid\CountUnguardedCellsInTheGridSimulation.cs
Line coverage
90%
Covered lines: 70
Uncovered lines: 7
Coverable lines: 77
Total lines: 140
Line coverage: 90.9%
Branch coverage
92%
Covered branches: 39
Total branches: 42
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountUnguarded(...)100%1010100%
Mark(...)90.62%353285.41%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountUnguardedCellsInTheGrid\CountUnguardedCellsInTheGridSimulation.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.CountUnguardedCellsInTheGrid;
 13
 14/// <inheritdoc />
 15public class CountUnguardedCellsInTheGridSimulation : ICountUnguardedCellsInTheGrid
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(m * n)
 20    /// </summary>
 21    /// <param name="m"></param>
 22    /// <param name="n"></param>
 23    /// <param name="guards"></param>
 24    /// <param name="walls"></param>
 25    /// <returns></returns>
 26    public int CountUnguarded(int m, int n, int[][] guards, int[][] walls)
 227    {
 228        var result = m * n;
 29
 230        var cells = new int[m, n];
 31
 1432        foreach (var guard in guards)
 433        {
 434            var row = guard[0];
 435            var col = guard[1];
 36
 437            cells[row, col] = 1;
 38
 439            result--;
 440        }
 41
 2042        foreach (var wall in walls)
 743        {
 744            var row = wall[0];
 745            var col = wall[1];
 46
 747            cells[row, col] = 2;
 48
 749            result--;
 750        }
 51
 1852        for (var i = 0; i < m; i++)
 753        {
 8054            for (var j = 0; j < n; j++)
 3355            {
 3356                if (cells[i, j] == 1)
 457                {
 458                    result -= Mark(cells, m, n, i, j);
 459                }
 3360            }
 761        }
 62
 263        return result;
 264    }
 65
 66    private static int Mark(int[,] cells, int m, int n, int x, int y)
 467    {
 468        var result = 0;
 69
 2070        for (var i = x + 1; i < m; i++)
 771        {
 772            if (cells[i, y] == 2 || cells[i, y] == 1)
 173            {
 174                break;
 75            }
 76
 677            if (cells[i, y] != 0)
 078            {
 079                continue;
 80            }
 81
 682            cells[i, y] = -1;
 83
 684            result++;
 685        }
 86
 1287        for (var i = x - 1; i >= 0; i--)
 488        {
 489            if (cells[i, y] == 2 || cells[i, y] == 1)
 290            {
 291                break;
 92            }
 93
 294            if (cells[i, y] != 0)
 195            {
 196                continue;
 97            }
 98
 199            cells[i, y] = -1;
 100
 1101            result++;
 1102        }
 103
 16104        for (var j = y + 1; j < n; j++)
 7105        {
 7106            if (cells[x, j] == 2 || cells[x, j] == 1)
 3107            {
 3108                break;
 109            }
 110
 4111            if (cells[x, j] != 0)
 0112            {
 0113                continue;
 114            }
 115
 4116            cells[x, j] = -1;
 117
 4118            result++;
 4119        }
 120
 10121        for (var j = y - 1; j >= 0; j--)
 3122        {
 3123            if (cells[x, j] == 2 || cells[x, j] == 1)
 2124            {
 2125                break;
 126            }
 127
 1128            if (cells[x, j] != 0)
 1129            {
 1130                continue;
 131            }
 132
 0133            cells[x, j] = -1;
 134
 0135            result++;
 0136        }
 137
 4138        return result;
 4139    }
 140}