Problem Β· Matrix
Closest DashMart
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
- Maximize Total Profit by Assigning Chefs to DishesPHONE SCREEN Β· Seen May 2026
- Return Priority of Order IDsPHONE SCREEN Β· Seen Jun 2025
- Adjust Prices π±Seen Oct 2024
- Discount EventsSeen Oct 2024
- Team FormationSeen Oct 2024
- Calculate Difference Value π§Seen Mar 2024
- Get Sizes of Friends Groups π₯Seen Mar 2024
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