< Summary

Information
Class: LeetCode.Algorithms.DesignNeighborSumService.DesignNeighborSumServiceDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNeighborSumService\DesignNeighborSumServiceDictionary.cs
Line coverage
100%
Covered lines: 54
Uncovered lines: 0
Coverable lines: 54
Total lines: 107
Line coverage: 100%
Branch coverage
100%
Covered branches: 28
Total branches: 28
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
AdjacentSum(...)100%88100%
DiagonalSum(...)100%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNeighborSumService\DesignNeighborSumServiceDictionary.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 DesignNeighborSumServiceDictionary : IDesignNeighborSumService
 16{
 17    private readonly int[][] _grid;
 418    private readonly Dictionary<int, (int X, int Y)> _neighborsDictionary = [];
 19
 20    /// <summary>
 21    ///     Time complexity - O(n)
 22    ///     Space complexity - O(n)
 23    /// </summary>
 24    /// <param name="grid"></param>
 425    public DesignNeighborSumServiceDictionary(int[][] grid)
 426    {
 427        _grid = grid;
 28
 2629        for (var i = 0; i < grid.Length; i++)
 930        {
 6431            for (var j = 0; j < grid[i].Length; j++)
 2332            {
 2333                _neighborsDictionary.Add(grid[i][j], (i, j));
 2334            }
 935        }
 436    }
 37
 38    /// <summary>
 39    ///     Time complexity - O(1)
 40    ///     Space complexity - O(1)
 41    /// </summary>
 42    /// <param name="value"></param>
 43    /// <returns></returns>
 44    public int AdjacentSum(int value)
 645    {
 646        var (x, y) = _neighborsDictionary[value];
 47
 648        var sum = 0;
 49
 650        if (x > 0)
 251        {
 252            sum += _grid[x - 1][y];
 253        }
 54
 655        if (x < _grid.Length - 1)
 456        {
 457            sum += _grid[x + 1][y];
 458        }
 59
 660        if (y > 0)
 361        {
 362            sum += _grid[x][y - 1];
 363        }
 64
 665        if (y < _grid[x].Length - 1)
 466        {
 467            sum += _grid[x][y + 1];
 468        }
 69
 670        return sum;
 671    }
 72
 73    /// <summary>
 74    ///     Time complexity - O(1)
 75    ///     Space complexity - O(1)
 76    /// </summary>
 77    /// <param name="value"></param>
 78    /// <returns></returns>
 79    public int DiagonalSum(int value)
 680    {
 681        var (x, y) = _neighborsDictionary[value];
 82
 683        var sum = 0;
 84
 685        if (x > 0 && y > 0)
 386        {
 387            sum += _grid[x - 1][y - 1];
 388        }
 89
 690        if (x > 0 && y < _grid[x].Length - 1)
 191        {
 192            sum += _grid[x - 1][y + 1];
 193        }
 94
 695        if (x < _grid.Length - 1 && y > 0)
 196        {
 197            sum += _grid[x + 1][y - 1];
 198        }
 99
 6100        if (x < _grid.Length - 1 && y < _grid.Length - 1)
 3101        {
 3102            sum += _grid[x + 1][y + 1];
 3103        }
 104
 6105        return sum;
 6106    }
 107}