< Summary

Information
Class: LeetCode.Algorithms.MinimumNumberOfDaysToDisconnectIsland.MinimumNumberOfDaysToDisconnectIslandBruteForceVisitedArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumNumberOfDaysToDisconnectIsland\MinimumNumberOfDaysToDisconnectIslandBruteForceVisitedArray.cs
Line coverage
100%
Covered lines: 62
Uncovered lines: 0
Coverable lines: 62
Total lines: 111
Line coverage: 100%
Branch coverage
100%
Covered branches: 32
Total branches: 32
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinDays(...)100%1010100%
GetIslandsCount(...)100%1010100%
MarkAsVisited(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumNumberOfDaysToDisconnectIsland\MinimumNumberOfDaysToDisconnectIslandBruteForceVisitedArray.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.MinimumNumberOfDaysToDisconnectIsland;
 13
 14/// <inheritdoc />
 15public class MinimumNumberOfDaysToDisconnectIslandBruteForceVisitedArray : IMinimumNumberOfDaysToDisconnectIsland
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2 * m^2)
 19    ///     Space complexity - O(n * m)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int MinDays(int[][] grid)
 1124    {
 1125        if (GetIslandsCount(grid) != 1)
 226        {
 227            return 0;
 28        }
 29
 7230        foreach (var row in grid)
 2531        {
 35032            for (var j = 0; j < row.Length; j++)
 15533            {
 15534                if (row[j] != 1)
 4435                {
 4436                    continue;
 37                }
 38
 11139                row[j] = 0;
 40
 11141                if (GetIslandsCount(grid) != 1)
 542                {
 543                    return 1;
 44                }
 45
 10646                row[j] = 1;
 10647            }
 2048        }
 49
 450        return 2;
 1151    }
 52
 53    private static int GetIslandsCount(IReadOnlyList<int[]> grid)
 12254    {
 12255        var islandsCount = 0;
 56
 12257        var visited = new bool[grid.Count][];
 58
 165859        for (var i = 0; i < grid.Count; i++)
 70760        {
 70761            visited[i] = new bool[grid[i].Length];
 70762        }
 63
 165864        for (var i = 0; i < grid.Count; i++)
 70765        {
 1528266            for (var j = 0; j < grid[i].Length; j++)
 693467            {
 693468                if (grid[i][j] != 1 || visited[i][j])
 680669                {
 680670                    continue;
 71                }
 72
 12873                MarkAsVisited(grid, visited, i, j);
 74
 12875                islandsCount++;
 12876            }
 70777        }
 78
 12279        return islandsCount;
 12280    }
 81
 82    private static void MarkAsVisited(IReadOnlyList<int[]> grid, IReadOnlyList<bool[]> visited, int i, int j)
 1975083    {
 1975084        if (grid[i][j] == 0 || visited[i][j])
 1407085        {
 1407086            return;
 87        }
 88
 568089        visited[i][j] = true;
 90
 568091        if (i - 1 >= 0)
 484192        {
 484193            MarkAsVisited(grid, visited, i - 1, j);
 484194        }
 95
 568096        if (i + 1 < grid.Count)
 472597        {
 472598            MarkAsVisited(grid, visited, i + 1, j);
 472599        }
 100
 5680101        if (j - 1 >= 0)
 5028102        {
 5028103            MarkAsVisited(grid, visited, i, j - 1);
 5028104        }
 105
 5680106        if (j + 1 < grid[i].Length)
 5028107        {
 5028108            MarkAsVisited(grid, visited, i, j + 1);
 5028109        }
 19750110    }
 111}