< Summary

Information
Class: LeetCode.Algorithms.DesignNeighborSumService.DesignNeighborSumServiceBruteForce
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNeighborSumService\DesignNeighborSumServiceBruteForce.cs
Line coverage
98%
Covered lines: 57
Uncovered lines: 1
Coverable lines: 58
Total lines: 109
Line coverage: 98.2%
Branch coverage
96%
Covered branches: 29
Total branches: 30
Branch coverage: 96.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
AdjacentSum(...)100%88100%
DiagonalSum(...)100%1616100%
FindPosition(...)83.33%6691.66%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNeighborSumService\DesignNeighborSumServiceBruteForce.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.DesignNeighborSumService;
 13
 14/// <inheritdoc />
 15public sealed class DesignNeighborSumServiceBruteForce : IDesignNeighborSumService
 16{
 17    private readonly int[][] _grid;
 18
 419    public DesignNeighborSumServiceBruteForce(int[][] grid)
 420    {
 421        _grid = grid;
 422    }
 23
 24    /// <summary>
 25    ///     Time complexity - O(m * n)
 26    ///     Space complexity - O(1)
 27    /// </summary>
 28    /// <param name="value"></param>
 29    /// <returns></returns>
 30    public int AdjacentSum(int value)
 631    {
 632        var (x, y) = FindPosition(value);
 33
 634        var sum = 0;
 35
 636        if (x > 0)
 237        {
 238            sum += _grid[x - 1][y];
 239        }
 40
 641        if (x < _grid.Length - 1)
 442        {
 443            sum += _grid[x + 1][y];
 444        }
 45
 646        if (y > 0)
 347        {
 348            sum += _grid[x][y - 1];
 349        }
 50
 651        if (y < _grid[x].Length - 1)
 452        {
 453            sum += _grid[x][y + 1];
 454        }
 55
 656        return sum;
 657    }
 58
 59    /// <summary>
 60    ///     Time complexity - O(m * n)
 61    ///     Space complexity - O(1)
 62    /// </summary>
 63    /// <param name="value"></param>
 64    /// <returns></returns>
 65    public int DiagonalSum(int value)
 666    {
 667        var (x, y) = FindPosition(value);
 68
 669        var sum = 0;
 70
 671        if (x > 0 && y > 0)
 372        {
 373            sum += _grid[x - 1][y - 1];
 374        }
 75
 676        if (x > 0 && y < _grid[x].Length - 1)
 177        {
 178            sum += _grid[x - 1][y + 1];
 179        }
 80
 681        if (x < _grid.Length - 1 && y > 0)
 182        {
 183            sum += _grid[x + 1][y - 1];
 184        }
 85
 686        if (x < _grid.Length - 1 && y < _grid.Length - 1)
 387        {
 388            sum += _grid[x + 1][y + 1];
 389        }
 90
 691        return sum;
 692    }
 93
 94    private (int X, int Y) FindPosition(int value)
 1295    {
 3696        for (var i = 0; i < _grid.Length; i++)
 1897        {
 8298            for (var j = 0; j < _grid[i].Length; j++)
 3599            {
 35100                if (_grid[i][j] == value)
 12101                {
 12102                    return (i, j);
 103                }
 23104            }
 6105        }
 106
 0107        return (-1, -1);
 12108    }
 109}