| | | 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.MagicSquaresInGrid; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MagicSquaresInGridBruteForce : IMagicSquaresInGrid |
| | | 16 | | { |
| | | 17 | | private const int MagicSquareSum = 15; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Time complexity - O(m * n) |
| | | 21 | | /// Space complexity - O(1) |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="grid"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public int NumMagicSquaresInside(int[][] grid) |
| | 7 | 26 | | { |
| | 7 | 27 | | var result = 0; |
| | | 28 | | |
| | 44 | 29 | | for (var i = 0; i < grid.Length - 2; i++) |
| | 15 | 30 | | { |
| | 180 | 31 | | for (var j = 0; j < grid[i].Length - 2; j++) |
| | 75 | 32 | | { |
| | 75 | 33 | | if (!IsMagicSquare(grid, i, j)) |
| | 70 | 34 | | { |
| | 70 | 35 | | continue; |
| | | 36 | | } |
| | | 37 | | |
| | 5 | 38 | | result++; |
| | 5 | 39 | | } |
| | 15 | 40 | | } |
| | | 41 | | |
| | 7 | 42 | | return result; |
| | 7 | 43 | | } |
| | | 44 | | |
| | | 45 | | private static bool IsMagicSquare(int[][] grid, int row, int column) |
| | 75 | 46 | | { |
| | 75 | 47 | | if (grid[row + 1][column + 1] != 5 || |
| | 75 | 48 | | grid[row + 1][column + 1] == grid[row][column + 1] || |
| | 75 | 49 | | grid[row + 1][column + 1] == grid[row + 1][column]) |
| | 66 | 50 | | { |
| | 66 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | |
| | 9 | 54 | | Span<bool> seen = stackalloc bool[10]; |
| | | 55 | | |
| | 50 | 56 | | for (var i = 0; i < 3; i++) |
| | 20 | 57 | | { |
| | 20 | 58 | | var rowSum = 0; |
| | 20 | 59 | | var columnSum = 0; |
| | | 60 | | |
| | 152 | 61 | | for (var j = 0; j < 3; j++) |
| | 58 | 62 | | { |
| | 58 | 63 | | var value = grid[row + i][column + j]; |
| | | 64 | | |
| | 58 | 65 | | if (value < 1 || value > 9 || seen[value]) |
| | 2 | 66 | | { |
| | 2 | 67 | | return false; |
| | | 68 | | } |
| | | 69 | | |
| | 56 | 70 | | seen[value] = true; |
| | | 71 | | |
| | 56 | 72 | | rowSum += value; |
| | 56 | 73 | | columnSum += grid[row + j][column + i]; |
| | 56 | 74 | | } |
| | | 75 | | |
| | 18 | 76 | | if (rowSum != MagicSquareSum || columnSum != MagicSquareSum) |
| | 2 | 77 | | { |
| | 2 | 78 | | return false; |
| | | 79 | | } |
| | 16 | 80 | | } |
| | | 81 | | |
| | 5 | 82 | | if (grid[row][column] + grid[row + 1][column + 1] + grid[row + 2][column + 2] != MagicSquareSum) |
| | 0 | 83 | | { |
| | 0 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | 5 | 87 | | if (grid[row][column + 2] + grid[row + 1][column + 1] + grid[row + 2][column] != MagicSquareSum) |
| | 0 | 88 | | { |
| | 0 | 89 | | return false; |
| | | 90 | | } |
| | | 91 | | |
| | 5 | 92 | | return true; |
| | 75 | 93 | | } |
| | | 94 | | } |