< Summary

Information
Class: LeetCode.Algorithms.MinimumNumberOfDaysToDisconnectIsland.MinimumNumberOfDaysToDisconnectIslandBruteForceCopyArray
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumNumberOfDaysToDisconnectIsland\MinimumNumberOfDaysToDisconnectIslandBruteForceCopyArray.cs
Line coverage
100%
Covered lines: 69
Uncovered lines: 0
Coverable lines: 69
Total lines: 121
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
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%
GetArrayCopy(...)100%44100%
GetIslandsCount(...)100%66100%
MarkAsVisited(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimumNumberOfDaysToDisconnectIsland\MinimumNumberOfDaysToDisconnectIslandBruteForceCopyArray.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 MinimumNumberOfDaysToDisconnectIslandBruteForceCopyArray : 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(GetArrayCopy(grid)) != 1)
 226        {
 227            return 0;
 28        }
 29
 5830        for (var i = 0; i < grid.Length; i++)
 2531        {
 35032            for (var j = 0; j < grid[i].Length; j++)
 15533            {
 15534                if (grid[i][j] != 1)
 4435                {
 4436                    continue;
 37                }
 38
 11139                var newGrid = GetArrayCopy(grid);
 40
 11141                newGrid[i][j] = 0;
 42
 11143                if (GetIslandsCount(newGrid) != 1)
 544                {
 545                    return 1;
 46                }
 10647            }
 2048        }
 49
 450        return 2;
 1151    }
 52
 53    private static int[][] GetArrayCopy(IReadOnlyList<int[]> array)
 12254    {
 12255        var newArray = new int[array.Count][];
 56
 165857        for (var i = 0; i < array.Count; i++)
 70758        {
 70759            newArray[i] = new int[array[i].Length];
 60
 1528261            for (var j = 0; j < array[i].Length; j++)
 693462            {
 693463                newArray[i][j] = array[i][j];
 693464            }
 70765        }
 66
 12267        return newArray;
 12268    }
 69
 70    private static int GetIslandsCount(IReadOnlyList<int[]> grid)
 12271    {
 12272        var islandsCount = 0;
 73
 165874        for (var i = 0; i < grid.Count; i++)
 70775        {
 1528276            for (var j = 0; j < grid[i].Length; j++)
 693477            {
 693478                if (grid[i][j] != 1)
 680679                {
 680680                    continue;
 81                }
 82
 12883                MarkAsVisited(grid, i, j);
 84
 12885                islandsCount++;
 12886            }
 70787        }
 88
 12289        return islandsCount;
 12290    }
 91
 92    private static void MarkAsVisited(IReadOnlyList<int[]> grid, int i, int j)
 1975093    {
 1975094        if (grid[i][j] != 1)
 1407095        {
 1407096            return;
 97        }
 98
 568099        grid[i][j] = -1;
 100
 5680101        if (i - 1 >= 0)
 4841102        {
 4841103            MarkAsVisited(grid, i - 1, j);
 4841104        }
 105
 5680106        if (i + 1 < grid.Count)
 4725107        {
 4725108            MarkAsVisited(grid, i + 1, j);
 4725109        }
 110
 5680111        if (j - 1 >= 0)
 5028112        {
 5028113            MarkAsVisited(grid, i, j - 1);
 5028114        }
 115
 5680116        if (j + 1 < grid[i].Length)
 5028117        {
 5028118            MarkAsVisited(grid, i, j + 1);
 5028119        }
 19750120    }
 121}