-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.cpp
37 lines (35 loc) · 916 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution
{
public:
int countServers(vector<vector<int>> &grid)
{
int rows = grid.size();
int cols = grid[0].size();
vector<int> rowCount(rows, 0), colCount(cols, 0);
// First pass: Count servers in each row and column
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
if (grid[i][j] == 1)
{
rowCount[i]++;
colCount[j]++;
}
}
}
// Second pass: Count communicable servers
int count = 0;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
if (grid[i][j] == 1 && (rowCount[i] > 1 || colCount[j] > 1))
{
count++;
}
}
}
return count;
}
};