< Summary

Information
Class: LeetCode.Algorithms.IslandPerimeter.IslandPerimeterIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IslandPerimeter\IslandPerimeterIterative.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 60
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
IslandPerimeter(...)100%2222100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\IslandPerimeter\IslandPerimeterIterative.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.IslandPerimeter;
 13
 14/// <inheritdoc />
 15public class IslandPerimeterIterative : IIslandPerimeter
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="grid"></param>
 22    /// <returns></returns>
 23    public int IslandPerimeter(int[][] grid)
 324    {
 325        var perimeter = 0;
 26
 1827        for (var i = 0; i < grid.Length; i++)
 628        {
 5029            for (var j = 0; j < grid[i].Length; j++)
 1930            {
 1931                if (grid[i][j] != 1)
 1032                {
 1033                    continue;
 34                }
 35
 936                if (i - 1 < 0 || grid[i - 1][j] == 0)
 637                {
 638                    perimeter++;
 639                }
 40
 941                if (i + 1 >= grid.Length || grid[i + 1][j] == 0)
 642                {
 643                    perimeter++;
 644                }
 45
 946                if (j - 1 < 0 || grid[i][j - 1] == 0)
 647                {
 648                    perimeter++;
 649                }
 50
 951                if (j + 1 >= grid[i].Length || grid[i][j + 1] == 0)
 652                {
 653                    perimeter++;
 654                }
 955            }
 656        }
 57
 358        return perimeter;
 359    }
 60}