MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / countServers

Method countServers

CountServersThatCommunicate.java:2–27  ·  view source on GitHub ↗
(int[][] grid)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int countServers(int[][] grid) {
3 int n = grid.length;
4 int m = grid[0].length;
5 int rowCount[] = new int[n];
6 int colCount[] = new int[m];
7 int count=0;
8 for(int i=0;i<n;i++){
9 for(int j=0;j<m;j++){
10 if(grid[i][j] == 1){
11 rowCount[i]++;
12 colCount[j]++;
13 count++;
14 }
15 }
16 }
17 for(int i=0;i<n;i++){
18 for(int j=0;j<m;j++){
19 if(grid[i][j] == 1){
20 // remove unreachable servers
21 if(rowCount[i] == 1 && colCount[j]==1)
22 count--;
23 }
24 }
25 }
26 return count;
27 }
28}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected