Skip to content

Commit 6f41fff

Browse files
committed
Add solution #1267
1 parent 06c35e4 commit 6f41fff

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@
337337
1233|[Remove Sub-Folders from the Filesystem](./1233-remove-sub-folders-from-the-filesystem.js)|Medium|
338338
1249|[Minimum Remove to Make Valid Parentheses](./1249-minimum-remove-to-make-valid-parentheses.js)|Medium|
339339
1252|[Cells with Odd Values in a Matrix](./1252-cells-with-odd-values-in-a-matrix.js)|Easy|
340+
1267|[Count Servers that Communicate](./1267-count-servers-that-communicate.js)|Medium|
340341
1287|[Element Appearing More Than 25% In Sorted Array](./1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
341342
1290|[Convert Binary Number in a Linked List to Integer](./1290-convert-binary-number-in-a-linked-list-to-integer.js)|Easy|
342343
1291|[Sequential Digits](./1291-sequential-digits.js)|Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 1267. Count Servers that Communicate
3+
* https://leetcode.com/problems/count-servers-that-communicate/
4+
* Difficulty: Medium
5+
*
6+
* You are given a map of a server center, represented as a m * n integer matrix grid,
7+
* where 1 means that on that cell there is a server and 0 means that it is no server.
8+
* Two servers are said to communicate if they are on the same row or on the same column.
9+
*
10+
* Return the number of servers that communicate with any other server.
11+
*/
12+
13+
/**
14+
* @param {number[][]} grid
15+
* @return {number}
16+
*/
17+
var countServers = function(grid) {
18+
const rows = new Array(grid.length).fill(0);
19+
const columns = new Array(grid[0].length).fill(0);
20+
21+
for (let i = 0; i < grid.length; i++) {
22+
for (let j = 0; j < grid[i].length; j++) {
23+
if (grid[i][j]) {
24+
rows[i]++;
25+
columns[j]++;
26+
}
27+
}
28+
}
29+
30+
let result = 0;
31+
for (let i = 0; i < grid.length; i++) {
32+
for (let j = 0; j < grid[i].length; j++) {
33+
if (!grid[i][j] || (rows[i] < 2 && columns[j] < 2)) {
34+
continue;
35+
}
36+
result++;
37+
}
38+
}
39+
40+
return result;
41+
};

0 commit comments

Comments
 (0)