C/C++教程

LeetCode C++ 1267. Count Servers that Communicate【Array】中等

本文主要是介绍LeetCode C++ 1267. Count Servers that Communicate【Array】中等,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

题意:这里有一幅服务器分布图,服务器的位置标识在 m * n 的整数矩阵网格 grid 中,1 表示单元格上有服务器,0 表示没有。如果两台服务器位于同一行或者同一列,我们就认为它们之间可以进行通信。请你统计并返回能够与至少一台其他服务器进行通信的服务器的数量。


解法1 暴力

对于每个矩阵元素,如果有服务器,就遍历同行或者同列,看有没有其他的服务器,如果有,能和其他服务器进行通信的服务器的数量就加一:

class Solution {
public:
    int countServers(vector<vector<int>>& grid) {
        int ans = 0, m = grid.size(), n = grid[0].size(); 
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                bool flag = grid[i][j];
                for (int k = 0; flag && k < m; ++k) if (k != i && grid[k][j]) { flag = false; ++ans; }
                for (int k = 0; flag && k < n; ++k) if (k != j && grid[i][k]) { flag = false; ++ans; }
            }
        }
        return ans;
    }
};

O ( n 3 ) O(n^3) O(n3) 解法,运行效率如下:

执行用时:64 ms, 在所有 C++ 提交中击败了63.90% 的用户
内存消耗:21.6 MB, 在所有 C++ 提交中击败了86.10% 的用户

解法2 存储每行/每列的元素和

我们可以存储每行和每列的元素和,然后双重循环遍历服务器,看同行或者同列的服务器数量是否 > 1 ,是则该服务器必然能够和其他服务器通信:

class Solution {
public:
    int countServers(vector<vector<int>>& grid) {
        int ans = 0, m = grid.size(), n = grid[0].size();
        vector<int> rowsum(m), colsum(n);
        for (int i = 0; i < m; ++i) 
            for (int j = 0; j < n; ++j) 
                if (grid[i][j]) ++rowsum[i], ++colsum[j];
        for (int i = 0; i < m; ++i) 
            for (int j = 0; j < n; ++j) 
                //这个判断保证当前服务器的同行或同列必然有至少一个服务器和当前服务器连通
                if (grid[i][j] && (rowsum[i] > 1 || colsum[j] > 1)) ++ans;
        return ans;
    }
}; 

O ( n 2 ) O(n^2) O(n2) 解法,运行效率如下:

执行用时:52 ms, 在所有 C++ 提交中击败了95.72% 的用户
内存消耗:21.7 MB, 在所有 C++ 提交中击败了80.21% 的用户
这篇关于LeetCode C++ 1267. Count Servers that Communicate【Array】中等的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!