forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Servers that Communicate.cpp
41 lines (40 loc) · 1.3 KB
/
Count Servers that Communicate.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
38
39
40
41
// Runtime: 72 ms (Top 82.64%) | Memory: 22.7 MB (Top 21.95%)
class Solution {
public:
int countServers(vector<vector<int>>& grid) {
int n=grid.size(),m=grid[0].size();
vector<vector<bool>>visited(n,vector<bool>(m,false));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(grid[i][j] && !visited[i][j])
{
bool flag=false;
for(int x=0;x<n;x++)
{
if(x!=i && grid[x][j] && !visited[x][j])
visited[x][j]=true,flag=true;
if(!flag && x!=i && visited[x][j])
flag=true;
}
for(int x=0;x<m;x++)
{
if(x!=j && grid[i][x] && !visited[i][x])
visited[i][x]=true,flag=true;
if(!flag && x!=j && visited[i][x])
flag=true;
}
if(flag)
visited[i][j]=true;
}
}
}
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(visited[i][j])
ans++;
return ans;
}
};