Skip to content

Commit d9ee24c

Browse files
committed
Sync LeetCode submission Runtime - 16 ms (66.67%), Memory - 19.7 MB (35.58%)
1 parent 0af4c93 commit d9ee24c

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>You are given a map of a server center, represented as a <code>m * n</code> 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 />
2+
<br />
3+
Return the number of servers&nbsp;that communicate with any other server.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-6.jpg" style="width: 202px; height: 203px;" /></p>
9+
10+
<pre>
11+
<strong>Input:</strong> grid = [[1,0],[0,1]]
12+
<strong>Output:</strong> 0
13+
<b>Explanation:</b>&nbsp;No servers can communicate with others.</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
17+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/13/untitled-diagram-4.jpg" style="width: 203px; height: 203px;" /></strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> grid = [[1,0],[1,1]]
21+
<strong>Output:</strong> 3
22+
<b>Explanation:</b>&nbsp;All three servers can communicate with at least one other server.
23+
</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-1-3.jpg" style="width: 443px; height: 443px;" /></p>
28+
29+
<pre>
30+
<strong>Input:</strong> grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
31+
<strong>Output:</strong> 4
32+
<b>Explanation:</b>&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&#39;t communicate with any other server.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>m == grid.length</code></li>
40+
<li><code>n == grid[i].length</code></li>
41+
<li><code>1 &lt;= m &lt;= 250</code></li>
42+
<li><code>1 &lt;= n &lt;= 250</code></li>
43+
<li><code>grid[i][j] == 0 or 1</code></li>
44+
</ul>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Approach 2: Track Using Two Arrays
2+
3+
# m = no. of rows, n = no. of columns
4+
# Time: O(m * n)
5+
# Space: O(m + n)
6+
7+
class Solution:
8+
def countServers(self, grid: List[List[int]]) -> int:
9+
if not grid:
10+
return 0
11+
12+
m, n = len(grid), len(grid[0])
13+
row_count = [0] * m
14+
col_count = [0] * n
15+
16+
for i in range(m):
17+
for j in range(n):
18+
if grid[i][j] == 1:
19+
row_count[i] += 1
20+
col_count[j] += 1
21+
22+
result = 0
23+
for i in range(m):
24+
for j in range(n):
25+
if grid[i][j] == 1:
26+
if row_count[i] > 1 or col_count[j] > 1:
27+
result += 1
28+
29+
return result
30+

0 commit comments

Comments
 (0)