{"id":5837,"date":"2019-11-23T23:47:55","date_gmt":"2019-11-24T07:47:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5837"},"modified":"2019-11-23T23:48:48","modified_gmt":"2019-11-24T07:48:48","slug":"leetcode-1267-count-servers-that-communicate","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1267-count-servers-that-communicate\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1267. Count Servers that Communicate"},"content":{"rendered":"\n<p>You are given a map of a server center, represented as a&nbsp;<code>m * n<\/code>&nbsp;integer matrix&nbsp;<code>grid<\/code>, 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.<br><br>Return the number of servers&nbsp;that communicate with any other server.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/11\/14\/untitled-diagram-6.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,0],[0,1]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>&nbsp;No servers can communicate with others.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/11\/13\/untitled-diagram-4.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,0],[1,1]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>&nbsp;All three servers can communicate with at least one other server.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/11\/14\/untitled-diagram-1-3.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>&nbsp;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.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == grid.length<\/code><\/li><li><code>n == grid[i].length<\/code><\/li><li><code>1 &lt;= m &lt;= 250<\/code><\/li><li><code>1 &lt;= n &lt;= 250<\/code><\/li><li><code>grid[i][j] == 0 or 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>Two passes:<br>First pass, count number of computers for each row and each column. <br>Second pass, count grid[i][j] if rows[i] or cols[j] has more than 1 computer.<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(m + n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countServers(vector<vector<int>>& grid) {\n    const size_t m = grid.size();\n    const size_t n = grid[0].size();\n    vector<int> rows(m);\n    vector<int> cols(n);\n    for (size_t i = 0; i < m; ++i)\n      for (size_t j = 0; j < n; ++j) {\n        rows[i] += grid[i][j];\n        cols[j] += grid[i][j];\n      }\n    int ans = 0;\n     for (size_t i = 0; i < m; ++i)\n      for (size_t j = 0; j < n; ++j)\n        ans += (rows[i] > 1 || cols[j] > 1) ? grid[i][j] : 0;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a map of a server center, represented as a&nbsp;m * n&nbsp;integer matrix&nbsp;grid, where 1 means that on that cell there is a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[8,278,177,366],"class_list":["post-5837","post","type-post","status-publish","format-standard","hentry","category-array","tag-counting","tag-grid","tag-medium","tag-omn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5837"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5837\/revisions"}],"predecessor-version":[{"id":5840,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5837\/revisions\/5840"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}