-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1267.cpp
More file actions
40 lines (37 loc) · 1003 Bytes
/
Copy path1267.cpp
File metadata and controls
40 lines (37 loc) · 1003 Bytes
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
38
39
40
#include "common.h"
using namespace std;
class Solution {
public:
int countServers(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[0].size();
int communicableServersCount = 0;
vector<int> rowCounts(m, 0), lastServerInCol(n, -1);
for (int i = 0; i < n; i++) {
int server_in_col = 0;
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1) {
server_in_col += 1;
rowCounts[j] += 1;
lastServerInCol[i] = j;
}
}
if (server_in_col > 1) {
communicableServersCount += server_in_col;
lastServerInCol[i] = -1;
}
}
for (int i = 0; i < n; i++) {
if (lastServerInCol[i] != -1 && rowCounts[lastServerInCol[i]] > 1) {
communicableServersCount += 1;
}
}
return communicableServersCount;
}
};
int main() {
Solution s;
vector<vector<int>> v = {{1, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
cout << s.countServers(v) << endl;
return 0;
}