Ezvor

2852. Sum of Remoteness of All Cells

MediumArray Preparing judge… LeetCode
ArrayHash TableDepth-First SearchBreadth-First SearchUnion-FindMatrix

Hints

Hint 1
We consider each cell of grid a node on a graph, and there is an edge between two nodes if one of them doesn't contain -1 and they share an edge on the grid.
Hint 2
We find the connected components of said graph.
Hint 3
The nodes of each connected component have the same value for R and it's the sum of the values of all nodes in other connected components.