Участник:Plague rat/Count Servers that Communicate

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/count-servers-that-communicate/

class Solution:
    def countServers(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])
 
        server_id = 0
        xCoordinates = [[] for i in range(m)]
        yCoordinates = [[] for i in range(n)]
 
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    xCoordinates[i].append(server_id)
                    yCoordinates[j].append(server_id)
                    server_id += 1
 
        _set = set()
        for i in range(m):
            if len(xCoordinates[i]) > 1:
                _set.update(xCoordinates[i])
        for j in range(n):
            if len(yCoordinates[j]) > 1:
                _set.update(yCoordinates[j])
 
        return len(_set)