【array-java】1267. Count Servers that Communicate

You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.

Return the number of servers that communicate with any other server.

Example 1:
在这里插入图片描述
Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.

Example 2:
在这里插入图片描述
Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.

Example 3:
在这里插入图片描述
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can’t communicate with any other server.

Constraints:

m == grid.length
n == grid[i].length
1 <= m <= 250
1 <= n <= 250
grid[i][j] == 0 or 1

Accepted
12,169
Submissions
20,976

answer one

Java | Clean And Simple | Beats 100 %

public int countServers(int[][] grid) {
    if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
    int numRows = grid.length;
    int numCols = grid[0].length;
    int rowCount[] = new int[numRows];
    int colCount[] = new int[numCols];
    int totalServers = 0;
    for (int row = 0; row < numRows; row++) {
        for (int col = 0; col < numCols; col++) {
            if (grid[row][col] == 1) {
                rowCount[row]++;
                colCount[col]++;
                totalServers++;
            }
        }
    }
    for (int row = 0; row < numRows; row++) {
        for (int col = 0; col < numCols; col++) {
            if (grid[row][col] == 1) {
                if (rowCount[row] == 1 && colCount[col] == 1) {
                    totalServers--;
                }
            }
        }
    }
    return totalServers;
}

answer two

[Java] Count servers which are not Connected
While traversing just keep track of the rows or columns where there are servers and also count the total no. of servers in the grid.
After traversing the grid once, traverse the grid again and check for each server (grid[i][j] == 1) if the column in which it lies(j) and the row in which it lies(i) contain any other server or not, if they don’t then subtract this 1 from the total count of servers in the original grid.

class Solution {
    
    public int countServers(int[][] grid) {
        
        int n = grid.length;
        if(n == 0) return 0;
        int m = grid[0].length;
        
        int count = 0;
        HashMap<Integer, Integer> rowCount = new HashMap<>();
        HashMap<Integer, Integer> columnCount = new HashMap<>();
        
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(grid[i][j] == 1){
                    count++;
                    if(!rowCount.containsKey(i)) rowCount.put(i, 1);
                    else rowCount.put(i, rowCount.get(i) + 1);
                    if(!columnCount.containsKey(j)) columnCount.put(j, 1);
                    else columnCount.put(j, columnCount.get(j) + 1);
                }
            }
        }
        
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(grid[i][j] == 1){
                    if(rowCount.get(i) == 1 && columnCount.get(j) == 1) count--;
                }
            }
        }
        
        return count;
        
    }
}

answer three

Java solution using O(columns) space

class Solution {
    public int countServers(int[][] grid) {
        int[] cols = new int[grid[0].length];
        for(int i=0;i<grid.length;++i){
            for(int j=0;j<grid[0].length;++j){
                if(grid[i][j] == 1) cols[j]++;
            }
        }
        
        int comms = 0;
        
        for(int i=0;i<grid.length;++i){
            int index = -1;
            int cnt = 0;
            for(int j=0;j<grid[0].length;++j){
                if(grid[i][j] == 1){
                    index = j;
                    cnt++;
                }
            }
            
            if(cnt == 1 && cols[index] > 1 || cnt > 1){
                comms += cnt;
            }
        }
        
        return comms;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值