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:

Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.
Example 2:

Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.

Example 3:

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

Difficulty: MEDIUM_HARD
Tags: Graph traversal

Spanning trees

Mathematics

Matrices

Data structures
Skill types: Data structures

Starter code:
class Solution:
    def countServers(self, grid: List[List[int]]) -> int:
        

Solutions:
class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		(xbucket, ybucket, server) = ([0] * len(grid), [0] * len(grid[0]), [])
		for (x, row) in enumerate(grid):
			for (y, cell) in enumerate(row):
				if cell:
					server.append((x, y))
					xbucket[x] += 1
					ybucket[y] += 1
		return sum((xbucket[x] > 1 or ybucket[y] > 1 for (x, y) in server))


class Solution:

	def get_index_ones(self, row_list, current_columns, columns_to_check):
		counter = 0
		copy_list = list(current_columns)
		for i in copy_list:
			if row_list[i] == 1:
				if columns_to_check.get(i) == None:
					columns_to_check[i] = 1
				else:
					columns_to_check[i] = columns_to_check[i] + 1

	def countServers(self, grid: List[List[int]]) -> int:
		row_dim = len(grid)
		column_dim = len(grid[0])
		result = 0
		map_single = []
		current_columns = [i for i in range(column_dim)]
		columns_to_check = {}
		for x in range(row_dim):
			if 1 in grid[x]:
				difference = column_dim - len([i for i in grid[x] if i == 0])
				if difference > 1:
					self.get_index_ones(grid[x], current_columns, columns_to_check)
					result += difference
				else:
					self.get_index_ones(grid[x], current_columns, columns_to_check)
					map_single.append(grid[x])
		for row in map_single:
			for (y, repeat) in columns_to_check.items():
				if row[y] == 1 and repeat > 1:
					result += 1
		return result


class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		d1 = defaultdict(int)
		d2 = defaultdict(int)
		for i in range(len(grid)):
			for j in range(len(grid[i])):
				if grid[i][j] == 1:
					d1[i] += 1
					d2[j] += 1
		c = 0
		for i in range(len(grid)):
			for j in range(len(grid[i])):
				if grid[i][j] == 1 and (d1[i] > 1 or d2[j] > 1):
					c += 1
		return c


class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		for (i, row) in enumerate(grid):
			if row.count(1) > 1:
				for (j, val) in enumerate(row):
					if val == 1:
						grid[i][j] = 2
		for (j, col) in enumerate(zip(*grid)):
			if col.count(1) + col.count(2) > 1:
				for (i, val) in enumerate(col):
					if val == 1:
						grid[i][j] = 2
		return sum((1 for row in grid for val in row if val == 2))


class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		if not grid or not grid[0]:
			return 0
		resset = set()
		for (ri, row) in enumerate(grid):
			tmp = [(ri, ci) for (ci, cell) in enumerate(row) if cell]
			if len(tmp) >= 2:
				resset.update(tmp)
		for (ci, col) in enumerate(zip(*grid)):
			tmp = [(ri, ci) for (ri, cell) in enumerate(col) if cell]
			if len(tmp) >= 2:
				resset.update(tmp)
		return len(resset)


from collections import deque, defaultdict

class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		row = defaultdict(lambda : [0, (0, 0)])
		col = defaultdict(lambda : 0)
		for r in range(len(grid)):
			for c in range(len(grid[r])):
				if grid[r][c] == 1:
					row[r][0] += 1
					col[c] += 1
					row[r][1] = (r, c)
		servers = 0
		for i in row:
			if row[i][0] > 1:
				servers += row[i][0]
			else:
				server = row[i][1][1]
				if col[server] > 1:
					servers += 1
		return servers


class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		row_count = [0 for i in range(len(grid))]
		col_count = [0 for i in range(len(grid[0]))]
		result = 0
		for round in range(1, 3):
			for row in range(len(grid)):
				for col in range(len(grid[row])):
					cell = grid[row][col]
					if cell == 1:
						if round == 1:
							row_count[row] += 1
							col_count[col] += 1
						elif row_count[row] > 1 or col_count[col] > 1:
							result += 1
		return result


class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		(m, n) = (len(grid), len(grid[0]))
		rows = [0] * m
		cols = [0] * n
		for (r, row) in enumerate(grid):
			for (c, server) in enumerate(row):
				if server == 1:
					rows[r] += 1
					cols[c] += 1
		ans = 0
		for (r, row) in enumerate(grid):
			for (c, server) in enumerate(row):
				if server == 1 and (rows[r] > 1 or cols[c] > 1):
					ans += 1
		return ans


class Solution:

	def countServers(self, grid: List[List[int]]) -> int:
		rows = []
		cols = []
		servers = []
		conn = 0
		i = 0
		while i < len(grid):
			j = 0
			while j < len(grid[i]):
				if grid[i][j] == 1:
					rows.append(i)
					cols.append(j)
					servers.append((i, j))
				j += 1
			i += 1
		for server in servers:
			if rows.count(server[0]) > 1 or cols.count(server[1]) > 1:
				conn += 1
		return conn