Skip to content

Commit 1646256

Browse files
authored
Create count-servers-that-communicate.cpp
1 parent 3d3b94d commit 1646256

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(m * n)
2+
// Space: O(m + n)
3+
4+
class Solution {
5+
public:
6+
int countServers(vector<vector<int>>& grid) {
7+
vector<int> rows(grid.size()), columns(grid[0].size());
8+
for (int i = 0; i < grid.size(); ++i) {
9+
for (int j = 0; j < grid[i].size(); ++j) {
10+
if (grid[i][j]) {
11+
++rows[i], ++columns[j];
12+
}
13+
}
14+
}
15+
int result = 0;
16+
for (int i = 0; i < grid.size(); ++i) {
17+
for (int j = 0; j < grid[i].size(); ++j) {
18+
if (grid[i][j] && (rows[i] > 1 || columns[j] > 1)) {
19+
++result;
20+
}
21+
}
22+
}
23+
return result;
24+
}
25+
};

0 commit comments

Comments
 (0)