< Summary

Information
Class: LeetCode.Algorithms.FindAllGroupOfFarmland.FindAllGroupOfFarmlandIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindAllGroupOfFarmland\FindAllGroupOfFarmlandIterative.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
FindFarmland(...)100%66100%
FindFarmlandCorner(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindAllGroupOfFarmland\FindAllGroupOfFarmlandIterative.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.FindAllGroupOfFarmland;
 13
 14/// <inheritdoc />
 15public class FindAllGroupOfFarmlandIterative : IFindAllGroupOfFarmland
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n)
 19    ///     Space complexity - O(k), where k is the number of distinct rectangles found
 20    /// </summary>
 21    /// <param name="land"></param>
 22    /// <returns></returns>
 23    public int[][] FindFarmland(int[][] land)
 624    {
 625        var result = new List<int[]>();
 26
 4227        for (var i = 0; i < land.Length; i++)
 1528        {
 12429            for (var j = 0; j < land[0].Length; j++)
 4730            {
 4731                if (land[i][j] == 1)
 632                {
 633                    result.Add(FindFarmlandCorner(land, i, j));
 634                }
 4735            }
 1536        }
 37
 638        return [.. result];
 639    }
 40
 41    private static int[] FindFarmlandCorner(IReadOnlyList<int[]> land, int x, int y)
 642    {
 643        var dx = x;
 644        var dy = y;
 45
 1146        while (dx < land.Count - 1 && land[dx + 1][dy] == 1)
 547        {
 548            dx++;
 549        }
 50
 1151        while (dy < land[0].Length - 1 && land[dx][dy + 1] == 1)
 552        {
 553            dy++;
 554        }
 55
 3456        for (var i = x; i <= dx; i++)
 1157        {
 6658            for (var j = y; j <= dy; j++)
 2259            {
 2260                land[i][j] = 0;
 2261            }
 1162        }
 63
 664        return [x, y, dx, dy];
 665    }
 66}