< 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
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
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(...)87.5%1616100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignNeighborSumService\DesignNeighborSumServiceDictionary.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 DesignNeighborSumServiceDictionary : IDesignNeighborSumService
 16{
 17    private readonly int[][] _grid;
 118    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>
 125    public DesignNeighborSumServiceDictionary(int[][] grid)
 126    {
 127        _grid = grid;
 28
 829        for (var i = 0; i < grid.Length; i++)
 330        {
 2431            for (var j = 0; j < grid[i].Length; j++)
 932            {
 933                _neighborsDictionary.Add(grid[i][j], (i, j));
 934            }
 335        }
 136    }
 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)
 245    {
 246        var (x, y) = _neighborsDictionary[value];
 47
 248        var sum = 0;
 49
 250        if (x > 0)
 151        {
 152            sum += _grid[x - 1][y];
 153        }
 54
 255        if (x < _grid.Length - 1)
 256        {
 257            sum += _grid[x + 1][y];
 258        }
 59
 260        if (y > 0)
 261        {
 262            sum += _grid[x][y - 1];
 263        }
 64
 265        if (y < _grid[x].Length - 1)
 266        {
 267            sum += _grid[x][y + 1];
 268        }
 69
 270        return sum;
 271    }
 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)
 280    {
 281        var (x, y) = _neighborsDictionary[value];
 82
 283        var sum = 0;
 84
 285        if (x > 0 && y > 0)
 286        {
 287            sum += _grid[x - 1][y - 1];
 288        }
 89
 290        if (x > 0 && y < _grid[x].Length - 1)
 191        {
 192            sum += _grid[x - 1][y + 1];
 193        }
 94
 295        if (x < _grid.Length - 1 && y > 0)
 196        {
 197            sum += _grid[x + 1][y - 1];
 198        }
 99
 2100        if (x < _grid.Length - 1 && y < _grid.Length - 1)
 1101        {
 1102            sum += _grid[x + 1][y + 1];
 1103        }
 104
 2105        return sum;
 2106    }
 107}