Back to problems

Minimize Total Grid Distance to Every Building

Algorithm · Waymo · Hard

You are given a rectangular 2D integer array grid where each cell is one of three types: 0 represents empty land, 1 represents a building, and 2 represents an obstacle. Choose an empty cell that can reach every building. Among those cells, find the one with the smallest total shortest-path distance to all buildings, and return that total distance. If no empty cell can reach every building, return -1. Distance is measured using orthogonal moves only: up, down, left, or right.…

Checking your access…