< Summary

Information
Class: LeetCode.Algorithms.CountSubIslands.CountSubIslandsDepthFirstSearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountSubIslands\CountSubIslandsDepthFirstSearch.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CountSubIslands(...)100%1010100%
GetIsSubIsland(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\CountSubIslands\CountSubIslandsDepthFirstSearch.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.CountSubIslands;
 13
 14/// <inheritdoc />
 15public class CountSubIslandsDepthFirstSearch : ICountSubIslands
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(m * n)
 20    /// </summary>
 21    /// <param name="grid1"></param>
 22    /// <param name="grid2"></param>
 23    /// <returns></returns>
 24    public int CountSubIslands(int[][] grid1, int[][] grid2)
 225    {
 226        var count = 0;
 27
 2428        for (var i = 0; i < grid1.Length; i++)
 1029        {
 12030            for (var j = 0; j < grid1[i].Length; j++)
 5031            {
 5032                if (grid2[i][j] != 1 || grid1[i][j] != 1)
 4333                {
 4334                    continue;
 35                }
 36
 737                if (GetIsSubIsland(grid1, grid2, i, j))
 538                {
 539                    count++;
 540                }
 741            }
 1042        }
 43
 244        return count;
 245    }
 46
 47    private static bool GetIsSubIsland(int[][] grid1, int[][] grid2, int i, int j)
 9548    {
 9549        if (i < 0 || j < 0 || i >= grid1.Length || j >= grid1[i].Length || grid2[i][j] == 0)
 7350        {
 7351            return true;
 52        }
 53
 2254        grid2[i][j] = 0;
 55
 2256        var isSubIsland = grid1[i][j] == 1;
 57
 2258        isSubIsland &= GetIsSubIsland(grid1, grid2, i + 1, j);
 2259        isSubIsland &= GetIsSubIsland(grid1, grid2, i - 1, j);
 2260        isSubIsland &= GetIsSubIsland(grid1, grid2, i, j + 1);
 2261        isSubIsland &= GetIsSubIsland(grid1, grid2, i, j - 1);
 62
 2263        return isSubIsland;
 9564    }
 65}