Easy · Matrices

Nearest bikes

Given a grid of row strings where 'Y' marks a worker, 'X' marks a bike and '.' is an empty cell (with at least one worker and one bike), return the smallest Manhattan distance |Δrow| + |Δcolumn| between any worker and any bike.

Examples

Example 1

{
  "grid": ["....", ".Y..", "...X", "X..."]
}

Output: nearest = 3

Example 2

{
  "grid": ["Y..X", "....", "X.Y."]
}

Output: nearest = 2

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Matrices problems