aboutsummaryrefslogtreecommitdiff
path: root/count_servers_communicate.py
blob: 75358cfafcfd9703bd0cb5b8b5fe157fbf2c2769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 1267. Count Servers that Communicate

"""
you are given a map of a server centre, represented as a 'm x 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 the same column. return the number of servers that communicate
with any other server.
"""


class Solution(object):
    def countServers(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        rows, cols = map(sum, grid), map(sum, zip(*grid))
        return sum(
            rows[i] + cols[j] > 2
            for i in range(len(grid))
            for j in range(len(grid[0]))
            if grid[i][j]
        )


if __name__ == "__main__":
    obj = Solution()
    print(obj.countServers(grid=[[1, 0], [0, 1]]))
    print(obj.countServers(grid=[[1, 0], [1, 1]]))
    print(
        obj.countServers(grid=[[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
    )