Minimum total Manhattan travel distance for friends meeting on a binary grid

Quick Overview

Given a binary grid of friends' homes, compute the smallest total Manhattan distance for everyone to travel to one meeting cell. It tests splitting Manhattan distance into row and column parts and finding the point that minimizes a sum of absolute differences without trying every cell.

Minimum total Manhattan travel distance for friends meeting on a binary grid

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an `m x n` binary grid `grid` where each `1` marks the home of one friend, return the minimal total travel distance. The total travel distance is the sum of the distances between the houses of the friends and the meeting point. The distance is calculated using Manhattan distance, where `distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|`. ### Function Signature ```python def min_total_distance(grid: list[list[int]]) -> int: ``` ### Rules - A cell is identified by its `(row, column)` index, and distances are measured between those indices. - The meeting point can be any cell of the grid, including a cell that holds a home. - Return only the minimal total distance, not the meeting point. ### Constraints - `m == len(grid)` - `n == len(grid[i])` - `1 <= m, n <= 200` - `grid[i][j]` is either `0` or `1` - There are at least two friends in the grid ### Examples ```text Input: grid = [[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]] Output: 6 ``` The homes are at `(0, 0)`, `(0, 4)` and `(2, 2)`. Meeting at `(0, 2)` costs 2 + 2 + 2 = 6, and no cell does better. ```text Input: grid = [[1, 1]] Output: 1 ``` The two friends are adjacent, so meeting at either home gives a total distance of 1.

Overview: Given a binary grid of friends' homes, compute the smallest total Manhattan distance for everyone to travel to one meeting cell. It tests splitting Manhattan distance into row and column parts and finding the point that minimizes a sum of absolute differences without trying every cell.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an m x n binary grid grid where each 1 marks the home of one friend, return the minimal total travel distance.

The total travel distance is the sum of the distances between the houses of the friends and the meeting point.

The distance is calculated using Manhattan distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.

Function Signature

def min_total_distance(grid: list[list[int]]) -> int:

Rules

  • A cell is identified by its (row, column) index, and distances are measured between those indices.
  • The meeting point can be any cell of the grid, including a cell that holds a home.
  • Return only the minimal total distance, not the meeting point.

Constraints

  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 200
  • grid[i][j] is either 0 or 1
  • There are at least two friends in the grid

Examples

Input: grid = [[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]]
Output: 6

The homes are at (0, 0), (0, 4) and (2, 2). Meeting at (0, 2) costs 2 + 2 + 2 = 6, and no cell does better.

Input: grid = [[1, 1]]
Output: 1

The two friends are adjacent, so meeting at either home gives a total distance of 1.