Trains the technique from
LeetCode 296Best Meeting PointThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A yard is given as the grid grid, where 1 marks a cell holding a worker and 0 marks an empty cell.
A worker walks one cell at a time to a cell sharing an edge with the current one, so walking between two cells costs the rows between them plus the columns between them.
Choose one cell as the muster point and add up how far every worker walks to reach it. Return the smallest total any choice of cell gives. The muster point may be any cell of the yard, whether a worker stands on it or not.
Example 1
The two workers stand in opposite corners. Every cell lying on a shortest walk between them costs the same, so the centre serves as well as either corner, and the total is the four steps that separate the two workers.
Example 2
Four workers at the corners of the yard. The two rows apart pair up for two steps, and so do the two columns, twice over, so any cell inside the square they mark costs eight steps altogether.
Example 3
All three workers share a row, so only columns matter. Mustering on the middle worker's cell costs one step from each side and nothing from the middle.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def min_total_distance(grid: list[list[int]]) -> int:public int minTotalDistance(int[][] grid)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.