Problem Β· Matrix

Closest DashMart

● MediumDoorDashPHONE SCREEN

A DashMart is a warehouse run by DoorDash that houses items found in convenience stores, grocery stores, and restaurants. We have a city with open roads, blocked-off roads, and DashMarts.

City planners want you to identify how far a location is from its closest DashMart.

You can only travel over open roads (up, down, left, right).

Locations are given in [row, col] format.

Provided:

  • city: char[][] - A 2D array representing the city where ' ' represents an open road, 'X' represents a blocked road, and 'D' represents a DashMart.
  • locations: int[][2] - A list of pairs [row, col] representing the locations to check.

Return:

answer: int[] - Return a list of the distances from a given point to its closest DashMart. If a DashMart cannot be reached from a location, return -1 for that location.

Examples
01 Β· Example 1
city = [
  ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'],
  ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'],
  [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '],
  [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '],
  [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'],
  [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X']
]
locations = [[200, 200], [1, 4], [0, 3], [5, 8], [1, 8], [5, 5]]
return = [-1, 2, 0, -1, 6, 9]
πŸ₯
More DoorDash problems
drafts saved locally
public int[] closestDashMart(char[][] city, int[][] locations) {
  // write your code here
}
city[ ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'], ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'], [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '], [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'], [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X'] ]
locations[[200, 200], [1, 4], [0, 3], [5, 8], [1, 8], [5, 5]]
expected[-1, 2, 0, -1, 6, 9]
sign in to submit