Skip to content

Commit c069c06

Browse files
yanglbmeidoocs
andauthored
feat: add solutions to lc problem: No.2852 (#1622)
No.2852.Sum of Remoteness of All Cells --------- Co-authored-by: Doocs Bot <doocs-bot@outlook.com>
1 parent 006d491 commit c069c06

File tree

13 files changed

+735
-0
lines changed

13 files changed

+735
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
# [2852. Sum of Remoteness of All Cells](https://leetcode.cn/problems/sum-of-remoteness-of-all-cells)
2+
3+
[English Version](/solution/2800-2899/2852.Sum%20of%20Remoteness%20of%20All%20Cells/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a <strong>0-indexed</strong> matrix <code>grid</code> of order <code>n * n</code>. Each cell in this matrix has a value <code>grid[i][j]</code>, which is either a <strong>positive</strong> integer or <code>-1</code> representing a blocked cell.</p>
10+
11+
<p>You can move from a non-blocked cell to any non-blocked cell that shares an edge.</p>
12+
13+
<p>For any cell <code>(i, j)</code>, we represent its <strong>remoteness</strong> as <code>R[i][j]</code> which is defined as the following:</p>
14+
15+
<ul>
16+
<li>If the cell <code>(i, j)</code> is a <strong>non-blocked</strong> cell, <code>R[i][j]</code> is the sum of the values <code>grid[x][y]</code> such that there is <strong>no path</strong> from the <strong>non-blocked</strong> cell <code>(x, y)</code> to the cell <code>(i, j)</code>.</li>
17+
<li>For blocked cells, <code>R[i][j] == 0</code>.</li>
18+
</ul>
19+
20+
<p>Return<em> the sum of </em><code>R[i][j]</code><em> over all cells.</em></p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong class="example">Example 1:</strong></p>
24+
25+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2852.Sum%20of%20Remoteness%20of%20All%20Cells/images/1-new.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 400px; height: 304px;" /></p>
26+
27+
<pre>
28+
<strong>Input:</strong> grid = [[-1,1,-1],[5,-1,4],[-1,3,-1]]
29+
<strong>Output:</strong> 39
30+
<strong>Explanation:</strong> In the picture above, there are four grids. The top-left grid contains the initial values in the grid. Blocked cells are colored black, and other cells get their values as it is in the input. In the top-right grid, you can see the value of R[i][j] for all cells. So the answer would be the sum of them. That is: 0 + 12 + 0 + 8 + 0 + 9 + 0 + 10 + 0 = 39.
31+
Let&#39;s jump on the bottom-left grid in the above picture and calculate R[0][1] (the target cell is colored green). We should sum up the value of cells that can&#39;t be reached by the cell (0, 1). These cells are colored yellow in this grid. So R[0][1] = 5 + 4 + 3 = 12.
32+
Now let&#39;s jump on the bottom-right grid in the above picture and calculate R[1][2] (the target cell is colored green). We should sum up the value of cells that can&#39;t be reached by the cell (1, 2). These cells are colored yellow in this grid. So R[1][2] = 1 + 5 + 3 = 9.
33+
</pre>
34+
35+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2800-2899/2852.Sum%20of%20Remoteness%20of%20All%20Cells/images/2.png" style="width: 400px; height: 302px; background: #fff; border-radius: .5rem;" /></p>
36+
37+
<p><strong class="example">Example 2:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong> grid = [[-1,3,4],[-1,-1,-1],[3,-1,-1]]
41+
<strong>Output:</strong> 13
42+
<strong>Explanation:</strong> In the picture above, there are four grids. The top-left grid contains the initial values in the grid. Blocked cells are colored black, and other cells get their values as it is in the input. In the top-right grid, you can see the value of R[i][j] for all cells. So the answer would be the sum of them. That is: 3 + 3 + 0 + 0 + 0 + 0 + 7 + 0 + 0 = 13.
43+
Let&#39;s jump on the bottom-left grid in the above picture and calculate R[0][2] (the target cell is colored green). We should sum up the value of cells that can&#39;t be reached by the cell (0, 2). This cell is colored yellow in this grid. So R[0][2] = 3.
44+
Now let&#39;s jump on the bottom-right grid in the above picture and calculate R[2][0] (the target cell is colored green). We should sum up the value of cells that can&#39;t be reached by the cell (2, 0). These cells are colored yellow in this grid. So R[2][0] = 3 + 4 = 7.
45+
</pre>
46+
47+
<p><strong class="example">Example 3:</strong></p>
48+
49+
<pre>
50+
<strong>Input:</strong> grid = [[1]]
51+
<strong>Output:</strong> 0
52+
<strong>Explanation:</strong> Since there are no other cells than (0, 0), R[0][0] is equal to 0. So the sum of R[i][j] over all cells would be 0.
53+
</pre>
54+
55+
<p>&nbsp;</p>
56+
<p><strong>Constraints:</strong></p>
57+
58+
<ul>
59+
<li><code>1 &lt;= n &lt;= 300</code></li>
60+
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code> or <code>grid[i][j] == -1</code></li>
61+
</ul>
62+
63+
## 解法
64+
65+
<!-- 这里可写通用的实现逻辑 -->
66+
67+
**方法一:DFS**
68+
69+
我们先统计矩阵中非阻塞的格子的个数,记为 $cnt$,然后从每个非阻塞的格子出发,使用 DFS 计算出每个连通块中格子之和 $s$ 以及格子个数 $t$,那么其它连通块的所有 $(cnt - t)$ 个格子都可以累加上 $s$。我们累加所有连通块的结果即可。
70+
71+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是矩阵的边长。
72+
73+
<!-- tabs:start -->
74+
75+
### **Python3**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
78+
79+
```python
80+
class Solution:
81+
def sumRemoteness(self, grid: List[List[int]]) -> int:
82+
def dfs(i: int, j: int) -> (int, int):
83+
s, t = grid[i][j], 1
84+
grid[i][j] = 0
85+
for a, b in pairwise(dirs):
86+
x, y = i + a, j + b
87+
if 0 <= x < n and 0 <= y < n and grid[x][y] > 0:
88+
s1, t1 = dfs(x, y)
89+
s, t = s + s1, t + t1
90+
return s, t
91+
92+
n = len(grid)
93+
dirs = (-1, 0, 1, 0, -1)
94+
cnt = sum(x > 0 for row in grid for x in row)
95+
ans = 0
96+
for i, row in enumerate(grid):
97+
for j, x in enumerate(row):
98+
if x > 0:
99+
s, t = dfs(i, j)
100+
ans += (cnt - t) * s
101+
return ans
102+
```
103+
104+
### **Java**
105+
106+
<!-- 这里可写当前语言的特殊实现逻辑 -->
107+
108+
```java
109+
class Solution {
110+
private int n;
111+
private int[][] grid;
112+
private final int[] dirs = {-1, 0, 1, 0, -1};
113+
114+
public long sumRemoteness(int[][] grid) {
115+
n = grid.length;
116+
this.grid = grid;
117+
int cnt = 0;
118+
for (int[] row : grid) {
119+
for (int x : row) {
120+
if (x > 0) {
121+
++cnt;
122+
}
123+
}
124+
}
125+
long ans = 0;
126+
for (int i = 0; i < n; ++i) {
127+
for (int j = 0; j < n; ++j) {
128+
if (grid[i][j] > 0) {
129+
long[] res = dfs(i, j);
130+
ans += (cnt - res[1]) * res[0];
131+
}
132+
}
133+
}
134+
return ans;
135+
}
136+
137+
private long[] dfs(int i, int j) {
138+
long[] res = new long[2];
139+
res[0] = grid[i][j];
140+
res[1] = 1;
141+
grid[i][j] = 0;
142+
for (int k = 0; k < 4; ++k) {
143+
int x = i + dirs[k], y = j + dirs[k + 1];
144+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] > 0) {
145+
long[] tmp = dfs(x, y);
146+
res[0] += tmp[0];
147+
res[1] += tmp[1];
148+
}
149+
}
150+
return res;
151+
}
152+
}
153+
```
154+
155+
### **C++**
156+
157+
```cpp
158+
class Solution {
159+
public:
160+
long long sumRemoteness(vector<vector<int>>& grid) {
161+
using pli = pair<long long, int>;
162+
int n = grid.size();
163+
int cnt = 0;
164+
for (auto& row : grid) {
165+
for (int x : row) {
166+
cnt += x > 0;
167+
}
168+
}
169+
int dirs[5] = {-1, 0, 1, 0, -1};
170+
function<pli(int, int)> dfs = [&](int i, int j) {
171+
long long s = grid[i][j];
172+
int t = 1;
173+
grid[i][j] = 0;
174+
for (int k = 0; k < 4; ++k) {
175+
int x = i + dirs[k], y = j + dirs[k + 1];
176+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] > 0) {
177+
auto [ss, tt] = dfs(x, y);
178+
s += ss;
179+
t += tt;
180+
}
181+
}
182+
return pli(s, t);
183+
};
184+
long long ans = 0;
185+
for (int i = 0; i < n; ++i) {
186+
for (int j = 0; j < n; ++j) {
187+
if (grid[i][j] > 0) {
188+
auto [s, t] = dfs(i, j);
189+
ans += (cnt - t) * s;
190+
}
191+
}
192+
}
193+
return ans;
194+
}
195+
};
196+
```
197+
198+
### **Go**
199+
200+
```go
201+
func sumRemoteness(grid [][]int) (ans int64) {
202+
n := len(grid)
203+
cnt := 0
204+
for _, row := range grid {
205+
for _, x := range row {
206+
if x > 0 {
207+
cnt++
208+
}
209+
}
210+
}
211+
var dfs func(i, j int) (int, int)
212+
dfs = func(i, j int) (int, int) {
213+
s, t := grid[i][j], 1
214+
grid[i][j] = 0
215+
dirs := [5]int{-1, 0, 1, 0, -1}
216+
for k := 0; k < 4; k++ {
217+
x, y := i+dirs[k], j+dirs[k+1]
218+
if x >= 0 && x < n && y >= 0 && y < n && grid[x][y] > 0 {
219+
ss, tt := dfs(x, y)
220+
s += ss
221+
t += tt
222+
}
223+
}
224+
return s, t
225+
}
226+
for i := range grid {
227+
for j := range grid[i] {
228+
if grid[i][j] > 0 {
229+
s, t := dfs(i, j)
230+
ans += int64(cnt-t) * int64(s)
231+
}
232+
}
233+
}
234+
return
235+
}
236+
```
237+
238+
### **TypeScript**
239+
240+
```ts
241+
function sumRemoteness(grid: number[][]): number {
242+
const n = grid.length;
243+
let cnt = 0;
244+
for (const row of grid) {
245+
for (const x of row) {
246+
if (x > 0) {
247+
cnt++;
248+
}
249+
}
250+
}
251+
const dirs = [-1, 0, 1, 0, -1];
252+
const dfs = (i: number, j: number): [number, number] => {
253+
let s = grid[i][j];
254+
let t = 1;
255+
grid[i][j] = 0;
256+
for (let k = 0; k < 4; ++k) {
257+
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
258+
if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] > 0) {
259+
const [ss, tt] = dfs(x, y);
260+
s += ss;
261+
t += tt;
262+
}
263+
}
264+
return [s, t];
265+
};
266+
let ans = 0;
267+
for (let i = 0; i < n; ++i) {
268+
for (let j = 0; j < n; ++j) {
269+
if (grid[i][j] > 0) {
270+
const [s, t] = dfs(i, j);
271+
ans += (cnt - t) * s;
272+
}
273+
}
274+
}
275+
return ans;
276+
}
277+
```
278+
279+
### **...**
280+
281+
```
282+
283+
```
284+
285+
<!-- tabs:end -->

0 commit comments

Comments
 (0)