-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sum-of-remoteness-of-all-cells.py
32 lines (30 loc) · 1.08 KB
/
sum-of-remoteness-of-all-cells.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Time: O(n^2)
# Space: O(n^2)
# flood fill, bfs, math
class Solution(object):
def sumRemoteness(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
DIRECTIONS = ((1, 0), (0, 1), (-1, 0), (0, -1))
def bfs(i, j):
total, cnt = grid[i][j], 1
grid[i][j] = -1
q = [(i, j)]
while q:
new_q = []
for i, j in q:
for di, dj in DIRECTIONS:
ni, nj = i+di, j+dj
if not (0 <= ni < len(grid) and 0 <= nj < len(grid[0]) and grid[ni][nj] != -1):
continue
total += grid[ni][nj]
cnt += 1
grid[ni][nj] = -1
new_q.append((ni, nj))
q = new_q
return total, cnt
groups = [bfs(i, j) for i in xrange(len(grid)) for j in xrange(len(grid[0])) if grid[i][j] != -1]
total = sum(t for t, _ in groups)
return sum((total-t)*c for t, c in groups)