< Summary

Information
Class: LeetCode.Algorithms.NumberOfIslands.NumberOfIslandsIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfIslands\NumberOfIslandsIterative.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 75
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
NumIslands(...)100%66100%
Find(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NumberOfIslands\NumberOfIslandsIterative.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.NumberOfIslands;
 13
 14/// <inheritdoc />
 15public class NumberOfIslandsIterative : INumberOfIslands
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m)
 19    ///     Space complexity - O(n * m)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int NumIslands(char[][] grid)
 224    {
 225        var num = 0;
 26
 2027        for (var i = 0; i < grid.Length; i++)
 828        {
 9629            for (var j = 0; j < grid[i].Length; j++)
 4030            {
 4031                if (grid[i][j] != '1')
 3632                {
 3633                    continue;
 34                }
 35
 436                num++;
 37
 438                Find(grid, i, j);
 439            }
 840        }
 41
 242        return num;
 243    }
 44
 45    private static void Find(IReadOnlyList<char[]> grid, int i, int j)
 1946    {
 1947        if (i + 1 < grid.Count && grid[i + 1][j] == '1')
 548        {
 549            grid[i + 1][j] = '2';
 50
 551            Find(grid, i + 1, j);
 552        }
 53
 1954        if (i - 1 >= 0 && grid[i - 1][j] == '1')
 355        {
 356            grid[i - 1][j] = '2';
 57
 358            Find(grid, i - 1, j);
 359        }
 60
 1961        if (j + 1 < grid[i].Length && grid[i][j + 1] == '1')
 562        {
 563            grid[i][j + 1] = '2';
 64
 565            Find(grid, i, j + 1);
 566        }
 67
 1968        if (j - 1 >= 0 && grid[i][j - 1] == '1')
 269        {
 270            grid[i][j - 1] = '2';
 71
 272            Find(grid, i, j - 1);
 273        }
 1974    }
 75}