Leetcode 1267: Count Servers that Communicate

grid47
grid47
Exploring patterns and algorithms
Jul 3, 2024 6 min read

You are given a 2D grid where each cell is either 1 (indicating a server) or 0 (no server). Two servers are said to communicate if they are in the same row or column. The task is to find the number of servers that can communicate with at least one other server.
Problem
Approach
Steps
Complexity
Input: A 2D grid of size m x n where each cell is either 1 (server) or 0 (no server).
Example: grid = [[1,0],[0,1]]
Constraints:
• 1 <= m <= 250
• 1 <= n <= 250
• grid[i][j] == 0 or 1
Output: Return the number of servers that can communicate with at least one other server.
Example: Output: 0
Constraints:
• The returned number should be an integer representing the number of servers that can communicate with another server.
Goal: Count how many servers can communicate with at least one other server by checking the row and column of each server.
Steps:
• 1. Iterate through each row and column of the grid, counting the number of servers in each row and column.
• 2. After counting, iterate again to check for servers that have more than one server in their row or column.
• 3. Return the count of servers that can communicate with another server.
Goal: The input grid has size m x n, where m and n are both at least 1 and at most 250. The grid values are either 0 or 1.
Steps:
• 1 <= m <= 250
• 1 <= n <= 250
• grid[i][j] == 0 or 1
Assumptions:
• The grid will contain only 0s and 1s.
Input: grid = [[1,0],[0,1]]
Explanation: This is a simple case with two servers that cannot communicate with each other because they are not in the same row or column.

Input: grid = [[1,0],[1,1]]
Explanation: In this case, all three servers can communicate with at least one other server.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus