Skip to content

Commit 14e79f3

Browse files
committed
Sync LeetCode submission Runtime - 563 ms (59.14%), Memory - 46.4 MB (37.63%)
1 parent fb06b1a commit 14e79f3

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<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>
2+
3+
<p>You can move from a non-blocked cell to any non-blocked cell that shares an edge.</p>
4+
5+
<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>
6+
7+
<ul>
8+
<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>
9+
<li>For blocked cells, <code>R[i][j] == 0</code>.</li>
10+
</ul>
11+
12+
<p>Return<em> the sum of </em><code>R[i][j]</code><em> over all cells.</em></p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/09/12/1-new.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 400px; height: 304px;" /></p>
18+
19+
<pre>
20+
<strong>Input:</strong> grid = [[-1,1,-1],[5,-1,4],[-1,3,-1]]
21+
<strong>Output:</strong> 39
22+
<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.
23+
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.
24+
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.
25+
</pre>
26+
27+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/09/12/2.png" style="width: 400px; height: 302px; background: #fff; border-radius: .5rem;" /></p>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> grid = [[-1,3,4],[-1,-1,-1],[3,-1,-1]]
33+
<strong>Output:</strong> 13
34+
<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.
35+
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.
36+
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.
37+
</pre>
38+
39+
<p><strong class="example">Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> grid = [[1]]
43+
<strong>Output:</strong> 0
44+
<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.
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= n &lt;= 300</code></li>
52+
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code> or <code>grid[i][j] == -1</code></li>
53+
</ul>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Approach 1: Depth First Search
2+
3+
# Time: O(n^2)
4+
# Space: O(n^2)
5+
6+
class Solution:
7+
def sumRemoteness(self, grid: List[List[int]]) -> int:
8+
# Direction arrays
9+
self.dir = [(0, 1), (0, -1), (1, 0), (-1, 0)]
10+
n = len(grid)
11+
12+
# Total sum of all non-blocked cells
13+
total_sum = sum(val for row in grid for val in row if val != -1)
14+
15+
result = 0
16+
for row in range(n):
17+
for col in range(n):
18+
if grid[row][col] > 0:
19+
# arr[0] = sum of reachable cells
20+
# arr[1] = count of reachable cells
21+
arr = [0, 0]
22+
self.dfs(grid, row, col, arr)
23+
result += (total_sum - arr[0]) * arr[1]
24+
25+
return result
26+
27+
# DFS to find sum and count of all cells reachable from (row, col)
28+
def dfs(self, grid, row, col, arr):
29+
arr[0] += grid[row][col]
30+
arr[1] += 1
31+
grid[row][col] = -1 # Mark as visited
32+
33+
for di, dj in self.dir:
34+
new_row, new_col = row + di, col + dj
35+
if self.is_valid(grid, new_row, new_col):
36+
self.dfs(grid, new_row, new_col, arr)
37+
38+
def is_valid(self, grid, row, col):
39+
n = len(grid)
40+
return 0 <= row < n and 0 <= col < n and grid[row][col] > 0
41+
42+
43+

0 commit comments

Comments
 (0)