< Summary

Information
Class: LeetCode.Algorithms.FindAllGroupOfFarmland.FindAllGroupOfFarmlandBruteForce
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindAllGroupOfFarmland\FindAllGroupOfFarmlandBruteForce.cs
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 92
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
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%
FindFarmland(...)100%1616100%
MarkAsVisited(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindAllGroupOfFarmland\FindAllGroupOfFarmlandBruteForce.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 FindAllGroupOfFarmlandBruteForce : 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[i].Length; j++)
 4730            {
 4731                if (land[i][j] != 1)
 4132                {
 4133                    continue;
 34                }
 35
 636                FindFarmland(land, result, i, j, i, j);
 637            }
 1538        }
 39
 640        return [.. result];
 641    }
 42
 43    private static void FindFarmland(IReadOnlyList<int[]> land, ICollection<int[]> result, int r1, int c1, int r2,
 44        int c2)
 645    {
 1246        while (true)
 1247        {
 1248            if (r2 + 1 < land.Count && land[r2 + 1][c2] == 1 && c2 + 1 < land[r2].Length && land[r2][c2 + 1] == 1)
 449            {
 450                MarkAsVisited(land, r1, c1, r2 + 1, c2 + 1);
 51
 452                r2 += 1;
 453                c2 += 1;
 54
 455                continue;
 56            }
 57
 858            if (r2 + 1 < land.Count && land[r2 + 1][c2] == 1)
 159            {
 160                MarkAsVisited(land, r1, c1, r2 + 1, c2);
 61
 162                r2 += 1;
 63
 164                continue;
 65            }
 66
 767            if (c2 + 1 < land[r2].Length && land[r2][c2 + 1] == 1)
 168            {
 169                MarkAsVisited(land, r1, c1, r2, c2 + 1);
 70
 171                c2 += 1;
 72
 173                continue;
 74            }
 75
 676            result.Add([r1, c1, r2, c2]);
 77
 678            break;
 79        }
 680    }
 81
 82    private static void MarkAsVisited(IReadOnlyList<int[]> land, int r1, int c1, int r2, int c2)
 683    {
 3684        for (var i = r1; i <= r2; i++)
 1285        {
 7486            for (var j = c1; j <= c2; j++)
 2587            {
 2588                land[i][j] = -1;
 2589            }
 1290        }
 691    }
 92}