< Summary

Information
Class: LeetCode.Algorithms.RegionsCutBySlashes.RegionsCutBySlashesDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RegionsCutBySlashes\RegionsCutBySlashesDepthFirstSearch.cs
Line coverage
100%
Covered lines: 58
Uncovered lines: 0
Coverable lines: 58
Total lines: 107
Line coverage: 100%
Branch coverage
100%
Covered branches: 36
Total branches: 36
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RegionsBySlashes(...)100%2020100%
FindRegions(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RegionsCutBySlashes\RegionsCutBySlashesDepthFirstSearch.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.RegionsCutBySlashes;
 13
 14/// <inheritdoc />
 15public class RegionsCutBySlashesDepthFirstSearch : IRegionsCutBySlashes
 16{
 17    private const int Multiplier = 3;
 18
 19    /// <summary>
 20    ///     Time complexity - O(n^2)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="grid"></param>
 24    /// <returns></returns>
 25    public int RegionsBySlashes(string[] grid)
 626    {
 627        var matrix = new int[grid.Length * Multiplier][];
 28
 6829        for (var i = 0; i < grid.Length; i++)
 2830        {
 2831            var m = i * Multiplier;
 32
 22433            for (var j = 0; j < Multiplier; j++)
 8434            {
 8435                matrix[m + j] = new int[grid[i].Length * Multiplier];
 8436            }
 37
 62838            for (var j = 0; j < grid[i].Length; j++)
 28639            {
 28640                var n = j * Multiplier;
 41
 28642                switch (grid[i][j])
 43                {
 44                    case '/':
 45
 82446                        for (var k = 0; k < Multiplier; k++)
 30947                        {
 30948                            matrix[m + k][n + (Multiplier - 1) - k] = 1;
 30949                        }
 50
 10351                        break;
 52                    case '\\':
 80853                        for (var k = 0; k < Multiplier; k++)
 30354                        {
 30355                            matrix[m + k][n + k] = 1;
 30356                        }
 57
 10158                        break;
 59                }
 28660            }
 2861        }
 62
 663        var regionCount = 0;
 64
 18065        for (var i = 0; i < matrix.Length; i++)
 8466        {
 531667            for (var j = 0; j < matrix[i].Length; j++)
 257468            {
 257469                if (matrix[i][j] != 0)
 254970                {
 254971                    continue;
 72                }
 73
 2574                FindRegions(matrix, i, j);
 75
 2576                regionCount++;
 2577            }
 8478        }
 79
 680        return regionCount;
 681    }
 82
 83    private static void FindRegions(IReadOnlyList<int[]> matrix, int i, int j)
 196284    {
 196285        matrix[i][j] = -1;
 86
 196287        if (i + 1 < matrix.Count && matrix[i + 1][j] == 0)
 67788        {
 67789            FindRegions(matrix, i + 1, j);
 67790        }
 91
 196292        if (i - 1 >= 0 && matrix[i - 1][j] == 0)
 62093        {
 62094            FindRegions(matrix, i - 1, j);
 62095        }
 96
 196297        if (j + 1 < matrix[i].Length && matrix[i][j + 1] == 0)
 27098        {
 27099            FindRegions(matrix, i, j + 1);
 270100        }
 101
 1962102        if (j - 1 >= 0 && matrix[i][j - 1] == 0)
 370103        {
 370104            FindRegions(matrix, i, j - 1);
 370105        }
 1962106    }
 107}