Leetcode 789: Escape The Ghosts

grid47
grid47
Exploring patterns and algorithms
Aug 20, 2024 6 min read

A set of ghosts where escape routes are traced, with each route softly glowing as it’s followed.
Solution to LeetCode 789: Escape The Ghosts Problem

You are playing a simplified game on an infinite 2-D grid where you start at position [0, 0], and your goal is to reach a destination point target = [xtarget, ytarget]. There are several ghosts on the map, each with its starting position. You and the ghosts can all independently move one unit in any of the four cardinal directions (north, east, south, or west), or stay still. If you can reach the destination before any ghost catches up with you, you escape. You cannot escape if you reach any square at the same time as a ghost. Your task is to return true if it is possible to escape, otherwise return false.
Problem
Approach
Steps
Complexity
Input: You are given the positions of ghosts and a target point. The ghosts' positions are provided as a 2D array, and the target point is also given as an array of two integers.
Example: ghosts = [[1, 0], [0, 3]], target = [0, 1]
Constraints:
• 1 <= ghosts.length <= 100
• ghosts[i].length == 2
• -10^4 <= xi, yi <= 10^4
• target.length == 2
• -10^4 <= xtarget, ytarget <= 10^4
Output: Return a boolean value: true if it is possible to escape, otherwise false.
Example: Output: true
Constraints:
• The output will be a boolean value indicating whether you can escape or not.
Goal: Determine if it is possible to escape by reaching the target point before any ghost can catch up with you.
Steps:
• Calculate the Manhattan distance from the starting position [0, 0] to the target point.
• For each ghost, calculate its Manhattan distance to the target.
• If any ghost's distance to the target is less than or equal to your distance to the target, you cannot escape and return false.
• If none of the ghosts can reach the target faster than you, return true.
Goal: Ensure that the inputs meet the specified constraints regarding the size of the grid and the number of ghosts.
Steps:
• ghosts array length must be between 1 and 100.
• ghosts' coordinates must be between -10^4 and 10^4.
• The target's coordinates must also be between -10^4 and 10^4.
Assumptions:
• The input is always valid within the constraints.
• There are no obstacles between the start and target, only ghosts that can move simultaneously.
Input: Input: ghosts = [[1,0],[0,3]], target = [0,1]
Explanation: In this example, the target [0, 1] can be reached in 1 move, and both ghosts at positions [1, 0] and [0, 3] cannot catch up before you. Hence, the output is true.

Input: Input: ghosts = [[1,0]], target = [2,0]
Explanation: In this case, the ghost at position [1, 0] can reach the target at the same time you do, preventing you from escaping. Thus, the output is false.

Input: Input: ghosts = [[2, 0]], target = [1, 0]
Explanation: Here, the ghost can reach the target at the same time as you, so escape is not possible, resulting in false.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus