Remoteness in Linear Time
Approach here could be using either a queue or stack, but basically for each cell, traverse the connected nodes, calculate the sum of the connected notes. To avoid a second pass, use a proxy hash table. At the same time calculate the total sum of the grid. Do another linear pass at the end to calculate the remoteness since you have all the data that you need pre-calculated and stored in the hash tables. Code is down below, cheers, ACC. Sum of Remoteness of All Cells - LeetCode 2852. Sum of Remoteness of All Cells Medium 9 0 Add to List Share You are given a 0-indexed matrix grid of order n * n . Each cell in this matrix has a value grid[i][j] , which is either a positive integer or -1 representing a blocked cell. You can move from a non-blocked cell to any non-blocked cell that shares an edge. For any cell (i, j) , we represent its remoteness as R[i][j] which is defined as the following: If th...