| | 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.CountServersThatCommunicate; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountServersThatCommunicateCounting : ICountServersThatCommunicate |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * n) |
| | 19 | | /// Space complexity - O(m + n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="grid"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int CountServers(int[][] grid) |
| 3 | 24 | | { |
| 3 | 25 | | var m = grid.Length; |
| 3 | 26 | | var n = grid[0].Length; |
| | 27 | |
|
| 3 | 28 | | var rowCount = new int[m]; |
| 3 | 29 | | var colCount = new int[n]; |
| | 30 | |
|
| 22 | 31 | | for (var i = 0; i < m; i++) |
| 8 | 32 | | { |
| 64 | 33 | | for (var j = 0; j < n; j++) |
| 24 | 34 | | { |
| 24 | 35 | | if (grid[i][j] == 0) |
| 14 | 36 | | { |
| 14 | 37 | | continue; |
| | 38 | | } |
| | 39 | |
|
| 10 | 40 | | rowCount[i]++; |
| 10 | 41 | | colCount[j]++; |
| 10 | 42 | | } |
| 8 | 43 | | } |
| | 44 | |
|
| 3 | 45 | | var result = 0; |
| | 46 | |
|
| 22 | 47 | | for (var i = 0; i < m; i++) |
| 8 | 48 | | { |
| 64 | 49 | | for (var j = 0; j < n; j++) |
| 24 | 50 | | { |
| 24 | 51 | | if (grid[i][j] == 1 && (rowCount[i] > 1 || colCount[j] > 1)) |
| 7 | 52 | | { |
| 7 | 53 | | result++; |
| 7 | 54 | | } |
| 24 | 55 | | } |
| 8 | 56 | | } |
| | 57 | |
|
| 3 | 58 | | return result; |
| 3 | 59 | | } |
| | 60 | | } |