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

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

More Matrices problems