返回介绍

solution / 1200-1299 / 1267.Count Servers that Communicate / README_EN

发布于 2024-06-17 01:03:21 字数 5667 浏览 0 评论 0 收藏 0

1267. Count Servers that Communicate

中文文档

Description

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

Solutions

Solution 1: Counting

We can count the number of servers in each row and each column, then traverse each server. If the number of servers in the current server's row or column exceeds $1$, it means the current server meets the condition, and we increment the result by $1$.

After the traversal, we return the result.

The time complexity is $O(m \times n)$, and the space complexity is $O(m + n)$. Where $m$ and $n$ are the number of rows and columns in the matrix, respectively.

class Solution:
  def countServers(self, grid: List[List[int]]) -> 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]:
          row[i] += 1
          col[j] += 1
    return sum(
      grid[i][j] and (row[i] > 1 or col[j] > 1)
      for i in range(m)
      for j in range(n)
    )
class Solution {
  public int countServers(int[][] grid) {
    int m = grid.length, n = grid[0].length;
    int[] row = new int[m];
    int[] col = new int[n];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 1) {
          row[i]++;
          col[j]++;
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 1 && (row[i] > 1 || col[j] > 1)) {
          ++ans;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countServers(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    vector<int> row(m), col(n);
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j]) {
          ++row[i];
          ++col[j];
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        ans += grid[i][j] && (row[i] > 1 || col[j] > 1);
      }
    }
    return ans;
  }
};
func countServers(grid [][]int) (ans int) {
  m, n := len(grid), len(grid[0])
  row, col := make([]int, m), make([]int, n)
  for i := range grid {
    for j, x := range grid[i] {
      if x == 1 {
        row[i]++
        col[j]++
      }
    }
  }
  for i := range grid {
    for j, x := range grid[i] {
      if x == 1 && (row[i] > 1 || col[j] > 1) {
        ans++
      }
    }
  }
  return
}
function countServers(grid: number[][]): number {
  const m = grid.length;
  const n = grid[0].length;
  const row = new Array(m).fill(0);
  const col = new Array(n).fill(0);
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (grid[i][j] === 1) {
        row[i]++;
        col[j]++;
      }
    }
  }
  let ans = 0;
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (grid[i][j] === 1 && (row[i] > 1 || col[j] > 1)) {
        ans++;
      }
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文