Place a Service Table Near All Customers
Problem statement
A rectangular restaurant grid contains 0 for an empty cell, 1 for a customer table, and -1 for a wall.
Choose one empty cell for a service table. Movement is allowed one cell up, down, left, or right and cannot pass through walls. Minimize the sum of shortest-path distances from the service table to every customer table.
Return the chosen coordinate [row,column]. Break ties by smaller row, then smaller column. Return [-1,-1] if there is no customer table or no empty cell can reach every customer table.
Function
bestServiceTable(grid: int[][]) → int[]Examples
Example 1
grid = [[1,0,0],[0,-1,0],[0,0,1]]return = [0,1]The cells [0,1] and [1,0] both have total distance four; row-major tie-breaking chooses [0,1].
Example 2
grid = [[1,-1,0],[0,-1,1],[0,0,0]]return = [1,0]Among cells reaching both customers, [1,0] minimizes the total distance.
Example 3
grid = [[1,-1,0],[-1,-1,1],[0,0,0]]return = [-1,-1]No empty cell can reach both customers.
Constraints
1 <= rows, columns <= 200.- Every cell is
-1,0, or1. - The grid contains at least one customer table.