-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountServersThatCommunicate.cpp
More file actions
53 lines (52 loc) · 1.44 KB
/
CountServersThatCommunicate.cpp
File metadata and controls
53 lines (52 loc) · 1.44 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public:
int countServers(vector<vector<int>>& grid) {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
int res = 0;
vector<int> si;
vector<int> sj;
int n = grid.size();
int m = grid[0].size();
vector<vector<bool>> vis(n, vector<bool>(m ,false));
//checking row-wise
for(int i = 0; i<n; i++){
int count = 0;
for(int j = 0; j<m; j++){
if(grid[i][j] == 1){
count++;
}
}
if(count > 1){
for(int j = 0; j<m; j++){
if(grid[i][j] == 1){
vis[i][j] = true;
}
}
res += count;
}
}
//checking column-wise
for(int j = 0; j<m; j++){
int count = 0;
int ncount =0;
for(int i = 0; i<n; i++){
if(grid[i][j] == 1){
ncount++;
if(vis[i][j] == false){
count++;
}
}
}
if(ncount>1){
for(int i = 0; i<n; i++){
if(grid[i][j] == 1 && vis[i][j] == false){
vis[i][j] = true;
}
}
res += count;
}
}
return res;
}
};