< 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
90%
Covered branches: 27
Total branches: 30
Branch coverage: 90%
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(...)87.5%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) 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.DesignNeighborSumService;
 13
 14/// <inheritdoc />
 15public class DesignNeighborSumServiceBruteForce : IDesignNeighborSumService
 16{
 17    private readonly int[][] _grid;
 18
 119    public DesignNeighborSumServiceBruteForce(int[][] grid)
 120    {
 121        _grid = grid;
 122    }
 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)
 231    {
 232        var (x, y) = FindPosition(value);
 33
 234        var sum = 0;
 35
 236        if (x > 0)
 137        {
 138            sum += _grid[x - 1][y];
 139        }
 40
 241        if (x < _grid.Length - 1)
 242        {
 243            sum += _grid[x + 1][y];
 244        }
 45
 246        if (y > 0)
 247        {
 248            sum += _grid[x][y - 1];
 249        }
 50
 251        if (y < _grid[x].Length - 1)
 252        {
 253            sum += _grid[x][y + 1];
 254        }
 55
 256        return sum;
 257    }
 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)
 266    {
 267        var (x, y) = FindPosition(value);
 68
 269        var sum = 0;
 70
 271        if (x > 0 && y > 0)
 272        {
 273            sum += _grid[x - 1][y - 1];
 274        }
 75
 276        if (x > 0 && y < _grid[x].Length - 1)
 177        {
 178            sum += _grid[x - 1][y + 1];
 179        }
 80
 281        if (x < _grid.Length - 1 && y > 0)
 182        {
 183            sum += _grid[x + 1][y - 1];
 184        }
 85
 286        if (x < _grid.Length - 1 && y < _grid.Length - 1)
 187        {
 188            sum += _grid[x + 1][y + 1];
 189        }
 90
 291        return sum;
 292    }
 93
 94    private (int X, int Y) FindPosition(int value)
 495    {
 1696        for (var i = 0; i < _grid.Length; i++)
 897        {
 5098            for (var j = 0; j < _grid[i].Length; j++)
 2199            {
 21100                if (_grid[i][j] == value)
 4101                {
 4102                    return (i, j);
 103                }
 17104            }
 4105        }
 106
 0107        return (-1, -1);
 4108    }
 109}