Hard · Matrices

Min distance of meeting point

Given an m × n binary grid where each 1 marks a person’s home, return the minimum total Manhattan distance for all of them to walk to a single meeting cell, which may be any cell of the grid.

Examples

Example 1

{
  "grid": [
    [1, 0, 0],
    [0, 0, 1],
    [0, 1, 0]
  ]
}

Output: min distance = 4

Example 2

{
  "grid": [
    [1, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 1],
    [1, 0, 1, 0]
  ]
}

Output: min distance = 9

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Matrices problems