欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【array-java】1267. Count Servers that Communicate

程序员文章站 2022-03-05 12:57:29
...

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:
【array-java】1267. Count Servers that Communicate
Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.

Example 2:
【array-java】1267. Count Servers that Communicate
Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.

Example 3:
【array-java】1267. Count Servers that Communicate
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;
    }
}
相关标签: leetcode中等