| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.DesignNeighborSumService; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class DesignNeighborSumServiceDictionary : IDesignNeighborSumService |
| | 16 | | { |
| | 17 | | private readonly int[][] _grid; |
| 1 | 18 | | 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> |
| 1 | 25 | | public DesignNeighborSumServiceDictionary(int[][] grid) |
| 1 | 26 | | { |
| 1 | 27 | | _grid = grid; |
| | 28 | |
|
| 8 | 29 | | for (var i = 0; i < grid.Length; i++) |
| 3 | 30 | | { |
| 24 | 31 | | for (var j = 0; j < grid[i].Length; j++) |
| 9 | 32 | | { |
| 9 | 33 | | _neighborsDictionary.Add(grid[i][j], (i, j)); |
| 9 | 34 | | } |
| 3 | 35 | | } |
| 1 | 36 | | } |
| | 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) |
| 2 | 45 | | { |
| 2 | 46 | | var (x, y) = _neighborsDictionary[value]; |
| | 47 | |
|
| 2 | 48 | | var sum = 0; |
| | 49 | |
|
| 2 | 50 | | if (x > 0) |
| 1 | 51 | | { |
| 1 | 52 | | sum += _grid[x - 1][y]; |
| 1 | 53 | | } |
| | 54 | |
|
| 2 | 55 | | if (x < _grid.Length - 1) |
| 2 | 56 | | { |
| 2 | 57 | | sum += _grid[x + 1][y]; |
| 2 | 58 | | } |
| | 59 | |
|
| 2 | 60 | | if (y > 0) |
| 2 | 61 | | { |
| 2 | 62 | | sum += _grid[x][y - 1]; |
| 2 | 63 | | } |
| | 64 | |
|
| 2 | 65 | | if (y < _grid[x].Length - 1) |
| 2 | 66 | | { |
| 2 | 67 | | sum += _grid[x][y + 1]; |
| 2 | 68 | | } |
| | 69 | |
|
| 2 | 70 | | return sum; |
| 2 | 71 | | } |
| | 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) |
| 2 | 80 | | { |
| 2 | 81 | | var (x, y) = _neighborsDictionary[value]; |
| | 82 | |
|
| 2 | 83 | | var sum = 0; |
| | 84 | |
|
| 2 | 85 | | if (x > 0 && y > 0) |
| 2 | 86 | | { |
| 2 | 87 | | sum += _grid[x - 1][y - 1]; |
| 2 | 88 | | } |
| | 89 | |
|
| 2 | 90 | | if (x > 0 && y < _grid[x].Length - 1) |
| 1 | 91 | | { |
| 1 | 92 | | sum += _grid[x - 1][y + 1]; |
| 1 | 93 | | } |
| | 94 | |
|
| 2 | 95 | | if (x < _grid.Length - 1 && y > 0) |
| 1 | 96 | | { |
| 1 | 97 | | sum += _grid[x + 1][y - 1]; |
| 1 | 98 | | } |
| | 99 | |
|
| 2 | 100 | | if (x < _grid.Length - 1 && y < _grid.Length - 1) |
| 1 | 101 | | { |
| 1 | 102 | | sum += _grid[x + 1][y + 1]; |
| 1 | 103 | | } |
| | 104 | |
|
| 2 | 105 | | return sum; |
| 2 | 106 | | } |
| | 107 | | } |