# Count Servers that Communicate

Dec 29, 2019

int countServers(vector<vector<int>>& grid) {
int m = grid.size();
if (m == 0) return 0;
int n = grid[0].size();
if (n == 0) return 0;
vector<int> a(m, 0);
vector<int> b(n, 0);

for(int i = 0;i < m; i++) {
for(int j = 0;j < n; j++) {
if (grid[i][j]) {
a[i]++;
b[j]++;
}
}
}
int res = 0;
for(int i = 0;i < m; i++) {
for(int j = 0;j < n; j++) {
if (grid[i][j] && (a[i] > 1 || b[j] > 1))
res++;
}
}
return res;

}


int countServers(vector<vector<int>>& grid) {
int m = grid.size();
if (m == 0) return 0;
int n = grid[0].size();
if (n == 0) return 0;
vector<int> vec;
int res = 0;
for(int i = 0;i < m; i++) {
vector<int> temp;
for(int j = 0;j < n; j++) {
if (grid[i][j]) temp.push_back(j);
}

if (temp.size() > 1) res += temp.size();
else if (temp.size() == 1) {
vec.push_back(temp[0]);
}
}

for(auto &j: vec) {
int count = 0;
for(int i = 0;i < m; i++) {
if (grid[i][j]) count++;
if (count > 1) {
res += 1;
break;
}
}
}

return res;
}

LeetCodeLeetCode

Implement Rand10() Using Rand7()

Satisfiability of Equality Equations