You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point [0, 0], and you are given a destination point target = [xtarget, ytarget] that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array ghosts, where ghosts[i] = [xi, yi] represents the starting position of the ith ghost. All inputs are integral coordinates.
Each turn, you and all the ghosts may independently choose to either move 1 unit in any of the four cardinal directions: north, east, south, or west, or stay still. All actions happen simultaneously.
You escape if and only if you can reach the target before any ghost reaches you. If you reach any square (including the target) at the same time as a ghost, it does not count as an escape.
Return true if it is possible to escape regardless of how the ghosts move, otherwise return false.
Example 1:
Input: ghosts = [[1,0],[0,3]], target = [0,1] Output: true Explanation: You can reach the destination (0, 1) after 1 turn, while the ghosts located at (1, 0) and (0, 3) cannot catch up with you.
Example 2:
Input: ghosts = [[1,0]], target = [2,0] Output: false Explanation: You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.
Example 3:
Input: ghosts = [[2,0]], target = [1,0] Output: false Explanation: The ghost can reach the target at the same time as you.
Constraints:
1 <= ghosts.length <= 100ghosts[i].length == 2-104 <= xi, yi <= 104target.length == 2-104 <= xtarget, ytarget <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to escaping ghosts means trying every single possible path you could take to get away. We calculate the distance to each ghost from every potential location we could be and see if it's safe to move there.
Here's how the algorithm would work step-by-step:
def escape_the_ghosts(ghosts, player_start, target): maximum_x = 20000
maximum_y = 20000
# Iterate through all possible spots on the grid.
for possible_x in range(-maximum_x, maximum_x + 1):
for possible_y in range(-maximum_x, maximum_x + 1):
possible_position = [possible_x, possible_y]
player_distance_to_position = manhattan_distance(player_start, possible_position)
safe_spot = True
# Check if any ghost can reach the position faster than the player.
for ghost_position in ghosts:
ghost_distance_to_target = manhattan_distance(ghost_position, target)
ghost_distance_to_position = manhattan_distance(ghost_position, possible_position)
# If the ghost can reach the possible location faster, its not safe.
if ghost_distance_to_position <= player_distance_to_position:
safe_spot = False
break
# Check if it is a safe spot and player can reach target before ghosts
if safe_spot and manhattan_distance(player_start, target) > 0:
return True
return False
def manhattan_distance(point_a, point_b):
return abs(point_a[0] - point_b[0]) + abs(point_a[1] - point_b[1])The goal is to determine if you can escape from ghosts in a grid. The optimal strategy involves realizing you only need to compare distances rather than simulate movements. If you can reach the target faster than any ghost, you escape!
Here's how the algorithm would work step-by-step:
def escape_the_ghosts(ghosts, player_location, target):
player_distance_to_target = abs(player_location[0] - target[0]) + abs(player_location[1] - target[1])
# Calculate the manhattan distance for each ghost.
for ghost_location in ghosts:
ghost_distance_to_target = abs(ghost_location[0] - target[0]) + abs(ghost_location[1] - target[1])
# If any ghost is closer, player can't escape.
if ghost_distance_to_target <= player_distance_to_target:
return False
# Player can escape if they are closer than all ghosts.
return True| Case | How to Handle |
|---|---|
| Player starts at the same location as a ghost | Check if the player and a ghost have the same starting coordinates and return false immediately. |
| Ghosts cannot reach the target at all (obstacles prevent this) | The ghost's shortest distance will be infinite, so return true. |
| Player's target destination is the same as their starting position. | Check if the target position is the same as the start position and if no ghosts start at the position, return true, otherwise false. |
| Extremely large coordinate values for player and ghosts. | Ensure that the Manhattan distance calculation doesn't result in integer overflow. |
| A large number of ghosts are very close to the player's start position. | The solution should efficiently compute distances for all ghosts, and return false if at least one ghost can reach the target faster or at same time. |
| One or more ghosts have the exact same starting position as each other | The distance calculations won't be affected as the ghosts are treated individually. |
| Null or empty array of ghosts | Return true, as there are no ghosts to catch the player. |
| Negative coordinate values. | The Manhattan distance calculation should handle negative values correctly by using absolute values. |