Total Distance to the Nearest Store in a Grid

Asked at GE Healthcare · Coding Questions · Difficulty: Medium

A city is modelled as an `M x N` grid of `0`s and `1`s. A cell holding `1` contains a store; a cell holding `0` is empty. The distance between two cells is the number of steps needed to walk from one to the other, where each step moves one cell up, down, left or right. For every cell in the grid, find the distance to the closest store, and return the sum of these distances over all cells.

Loading the problem…