Skip to content

Latest commit

 

History

History
26 lines (24 loc) · 690 Bytes

1267.md

File metadata and controls

26 lines (24 loc) · 690 Bytes

1267. Count Servers that Communicate

Solution 1 (time O(mn), space O(m+n))

class Solution(object):
    def countServers(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        m, n = len(grid), len(grid[0])
        row = [0] * m
        col = [0] * n
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    row[i] += 1
                    col[j] += 1
        ans = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1 and (row[i] > 1 or col[j] > 1):
                    ans += 1
        return ans