Back to November Back to Year 2026
MEDIUM 08 Nov 2026 View on LeetCode

1267. Count Servers that Communicate

</> Solution

/**
 * Problem: LeetCode 1267 - Count Servers that Communicate
 * Difficulty: Medium
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public int countServers(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[] rowCount = new int[m];
        int[] colCount = new int[n];

        // First pass: count servers in each row and column
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    rowCount[i]++;
                    colCount[j]++;
                }
            }
        }

        // Second pass: count servers that can communicate
        int result = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1 && (rowCount[i] > 1 || colCount[j] > 1)) {
                    result++;
                }
            }
        }

        return result;
    }
}

TIME COMPLEXITY

O(m × n)

SPACE COMPLEXITY

O(m + n)

TOPICS

Array Counting Frequency Counting Hash Table