| | | 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 SpecialPositionsInBinaryMatrixSum : ISpecialPositionsInBinaryMatrix |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(m * n) |
| | | 19 | | /// Space complexity - O(m + n) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="mat"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public int NumSpecial(int[][] mat) |
| | 2 | 24 | | { |
| | 2 | 25 | | var m = mat.Length; |
| | 2 | 26 | | var n = mat[0].Length; |
| | | 27 | | |
| | 2 | 28 | | Span<int> rowsSum = stackalloc int[m]; |
| | 2 | 29 | | Span<int> columnsSum = stackalloc int[n]; |
| | | 30 | | |
| | 16 | 31 | | for (var i = 0; i < m; i++) |
| | 6 | 32 | | { |
| | 48 | 33 | | for (var j = 0; j < n; j++) |
| | 18 | 34 | | { |
| | 18 | 35 | | if (mat[i][j] != 1) |
| | 12 | 36 | | { |
| | 12 | 37 | | continue; |
| | | 38 | | } |
| | | 39 | | |
| | 6 | 40 | | rowsSum[i]++; |
| | 6 | 41 | | columnsSum[j]++; |
| | 6 | 42 | | } |
| | 6 | 43 | | } |
| | | 44 | | |
| | 2 | 45 | | var result = 0; |
| | | 46 | | |
| | 16 | 47 | | for (var i = 0; i < m; i++) |
| | 6 | 48 | | { |
| | 6 | 49 | | if (rowsSum[i] != 1) |
| | 0 | 50 | | { |
| | 0 | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 34 | 54 | | for (var j = 0; j < n; j++) |
| | 15 | 55 | | { |
| | 15 | 56 | | if (mat[i][j] != 1 || columnsSum[j] != 1) |
| | 11 | 57 | | { |
| | 11 | 58 | | continue; |
| | | 59 | | } |
| | | 60 | | |
| | 4 | 61 | | result++; |
| | | 62 | | |
| | 4 | 63 | | break; |
| | | 64 | | } |
| | 6 | 65 | | } |
| | | 66 | | |
| | 2 | 67 | | return result; |
| | 2 | 68 | | } |
| | | 69 | | } |