| | | 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.SpecialPositionsInBinaryMatrix; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class SpecialPositionsInBinaryMatrixBruteForce : ISpecialPositionsInBinaryMatrix |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(m * n * (m + n)) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="mat"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public int NumSpecial(int[][] mat) |
| | 2 | 24 | | { |
| | 2 | 25 | | var result = 0; |
| | | 26 | | |
| | 2 | 27 | | var m = mat.Length; |
| | 2 | 28 | | var n = mat[0].Length; |
| | | 29 | | |
| | 16 | 30 | | for (var i = 0; i < m; i++) |
| | 6 | 31 | | { |
| | 48 | 32 | | for (var j = 0; j < n; j++) |
| | 18 | 33 | | { |
| | 18 | 34 | | if (IsSpecial(mat, i, j, m, n)) |
| | 4 | 35 | | { |
| | 4 | 36 | | result++; |
| | 4 | 37 | | } |
| | 18 | 38 | | } |
| | 6 | 39 | | } |
| | | 40 | | |
| | 2 | 41 | | return result; |
| | 2 | 42 | | } |
| | | 43 | | |
| | | 44 | | private static bool IsSpecial(int[][] mat, int i, int j, int m, int n) |
| | 18 | 45 | | { |
| | 18 | 46 | | if (mat[i][j] != 1) |
| | 12 | 47 | | { |
| | 12 | 48 | | return false; |
| | | 49 | | } |
| | | 50 | | |
| | 48 | 51 | | for (var c = 0; c < n; c++) |
| | 18 | 52 | | { |
| | 18 | 53 | | if (c == j) |
| | 6 | 54 | | { |
| | 6 | 55 | | continue; |
| | | 56 | | } |
| | | 57 | | |
| | 12 | 58 | | if (mat[i][c] == 1) |
| | 0 | 59 | | { |
| | 0 | 60 | | return false; |
| | | 61 | | } |
| | 12 | 62 | | } |
| | | 63 | | |
| | 40 | 64 | | for (var r = 0; r < m; r++) |
| | 16 | 65 | | { |
| | 16 | 66 | | if (r == i) |
| | 5 | 67 | | { |
| | 5 | 68 | | continue; |
| | | 69 | | } |
| | | 70 | | |
| | 11 | 71 | | if (mat[r][j] == 1) |
| | 2 | 72 | | { |
| | 2 | 73 | | return false; |
| | | 74 | | } |
| | 9 | 75 | | } |
| | | 76 | | |
| | 4 | 77 | | return true; |
| | 18 | 78 | | } |
| | | 79 | | } |