Skip to content

Commit 37fbb18

Browse files
committed
Add: Add 2025/1/23
1 parent 7f51072 commit 37fbb18

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 1267. Count Servers that Communicate
2+
3+
You are given a map of a server center, represented as a `m * n` integer matrix `grid`,
4+
where 1 means that on that cell there is a server and 0 means that it is no server.
5+
Two servers are said to communicate if they are on the same row or on the same column.
6+
7+
Return the number of servers that communicate with any other server.
8+
9+
## 基礎思路
10+
可以溝通的條件是在同一行或同一列,那麼換句話說,就是他所處的行或列上的伺服器數量大於 1,那麼這個伺服器就可以溝通。
11+
那麼問題就分解成了兩個部分,一個是計算每一行與每一列的伺服器數量,然後逐一檢查這個伺服器是否可以溝通。
12+
13+
## 解題步驟
14+
15+
### Step 1: 紀錄每一行與每一列的伺服器數量
16+
17+
```typescript
18+
const m = grid.length;
19+
const n = grid[0].length;
20+
```
21+
22+
### Step 2: 計算每一行與每一列的伺服器數量
23+
24+
```typescript
25+
// 紀錄每一行與每一列的伺服器數量
26+
const serverCountInRow = new Array(m).fill(0);
27+
const serverCountInCol = new Array(n).fill(0);
28+
29+
// 遍歷 grid,計算每一行與每一列的伺服器數量
30+
for (let i = 0; i < m; i++) {
31+
for (let j = 0; j < n; j++) {
32+
if (grid[i][j] === 1) {
33+
serverCountInRow[i]++;
34+
serverCountInCol[j]++;
35+
}
36+
}
37+
}
38+
```
39+
40+
### Step 3: 計算可以溝通的伺服器數量
41+
42+
```typescript
43+
let count = 0;
44+
for (let i = 0; i < m; i++) {
45+
for (let j = 0; j < n; j++) {
46+
// 我們會在以下情況跳過
47+
// 1. 這個位置沒有伺服器
48+
// 2. 這個伺服器所在的行或列只有一個伺服器
49+
if (grid[i][j] === 0 || serverCountInRow[i] === 1 && serverCountInCol[j] === 1) {
50+
continue;
51+
}
52+
53+
count++;
54+
}
55+
}
56+
```
57+
58+
## 時間複雜度
59+
- 計算每一行與每一列的伺服器數量需要 $O(m * n)$ 的時間
60+
- 計算可以溝通的伺服器數量需要 $O(m * n)$ 的時間
61+
- 因此總時間複雜度為 $O(m * n)$
62+
63+
> $O(m * n)$
64+
65+
## 空間複雜度
66+
- 我們需要兩個陣列來記錄每一行與每一列的伺服器數量,因此空間複雜度為 $O(m + n)$
67+
- 紀錄其他變數的空間複雜度為 $O(1)$
68+
- 因此總空間複雜度為 $O(m + n)$
69+
70+
> $O(m + n)$
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function countServers(grid: number[][]): number {
2+
// Get the number of rows and columns
3+
const m = grid.length;
4+
const n = grid[0].length;
5+
6+
// Storage for the number of servers in each row and column
7+
const serverCountInRow = new Array(m).fill(0);
8+
const serverCountInCol = new Array(n).fill(0);
9+
10+
// Count the number of servers in each row and column
11+
for (let i = 0; i < m; i++) {
12+
for (let j = 0; j < n; j++) {
13+
if (grid[i][j] === 1) {
14+
serverCountInRow[i]++;
15+
serverCountInCol[j]++;
16+
}
17+
}
18+
}
19+
20+
// Count the number of servers that can communicate
21+
let count = 0;
22+
for (let i = 0; i < m; i++) {
23+
for (let j = 0; j < n; j++) {
24+
// When the cell is empty
25+
// Or the server is the only server in the row and column
26+
// We skip this cell
27+
if (grid[i][j] === 0 || serverCountInRow[i] === 1 && serverCountInCol[j] === 1) {
28+
continue;
29+
}
30+
31+
count++;
32+
}
33+
}
34+
35+
return count;
36+
}

0 commit comments

Comments
 (0)