class Solution:
def escapeGhosts(self, ghosts, target):
# Calculate the player's minimum distance to the target
player_dist = abs(target[0]) + abs(target[1])
# If any ghost can reach the target as fast or faster, return False
for ghost in ghosts:
ghost_dist = abs(ghost[0] - target[0]) + abs(ghost[1] - target[1])
if ghost_dist <= player_dist:
return False
return True
class Solution {
public:
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
int playerDist = abs(target[0]) + abs(target[1]);
for (const auto& ghost : ghosts) {
int ghostDist = abs(ghost[0] - target[0]) + abs(ghost[1] - target[1]);
if (ghostDist <= playerDist) return false;
}
return true;
}
};
class Solution {
public boolean escapeGhosts(int[][] ghosts, int[] target) {
int playerDist = Math.abs(target[0]) + Math.abs(target[1]);
for (int[] ghost : ghosts) {
int ghostDist = Math.abs(ghost[0] - target[0]) + Math.abs(ghost[1] - target[1]);
if (ghostDist <= playerDist) return false;
}
return true;
}
}
var escapeGhosts = function(ghosts, target) {
const playerDist = Math.abs(target[0]) + Math.abs(target[1]);
for (let ghost of ghosts) {
const ghostDist = Math.abs(ghost[0] - target[0]) + Math.abs(ghost[1] - target[1]);
if (ghostDist <= playerDist) return false;
}
return true;
};
In the "Escape The Ghosts" problem, you start at the origin [0, 0]
on a 2D grid. There are several ghosts, each with their own starting positions given as a list ghosts
, where each ghost's position is a pair of integers. Your goal is to reach a given target
position, also specified as a pair of integers.
Both you and the ghosts can move one unit per turn in any of the four cardinal directions (up, down, left, right). All move simultaneously. If a ghost reaches the target before you, or at the same time as you, you lose. If you can reach the target strictly before any ghost, you win.
Key constraints:
[0, 0]
to target
.At first glance, it may seem that you need to simulate every possible move for both you and each ghost, considering all possible paths. However, both you and the ghosts can move in any direction, and all are allowed to take the shortest possible path to the target.
The key insight is that the only thing that matters is the minimal number of steps required for you and each ghost to reach the target. Since all parties can move optimally, the actual path doesn't matter—just the distance.
This reduces the problem to a simple distance comparison: if any ghost can reach the target in the same number of steps or fewer than you, you cannot escape. Otherwise, you can always reach the target first.
Let's break down the algorithm step by step:
[0, 0]
, your minimum number of steps to the target is the Manhattan distance: abs(target[0]) + abs(target[1])
.abs(ghost[0] - target[0]) + abs(ghost[1] - target[1])
.False
).True
).We use simple arithmetic and a loop to compare distances, making the solution efficient and straightforward.
Let's consider the following example:
ghosts = [[1, 0], [0, 3]]
target = [0, 1]
Your distance:
From [0, 0]
to [0, 1]
is abs(0-0) + abs(0-1) = 1
.
Ghost 1:
From [1, 0]
to [0, 1]
is abs(1-0) + abs(0-1) = 2
.
Ghost 2:
From [0, 3]
to [0, 1]
is abs(0-0) + abs(3-1) = 2
.
Both ghosts are 2 steps away, while you are only 1 step away. Since neither ghost can reach the target as fast as you, you can escape, so the answer is True
.
Step-by-step:
True
Brute-force Approach:
If you tried to simulate every possible move for you and each ghost, the number of possible states would grow exponentially, making it infeasible for large inputs.
Optimized Approach:
The optimized solution only requires:
Time Complexity: O(N), since we check each ghost once.
Space Complexity: O(1), since we use only a few variables and don't store extra data.
The "Escape The Ghosts" problem is elegantly solved by focusing on the minimum steps (Manhattan distance) required for you and each ghost to reach the target. By comparing these distances, we avoid unnecessary simulations and achieve an efficient O(N) solution. The key insight is recognizing that the optimal path for all parties is always the shortest possible, making the problem a simple comparison rather than a complex pathfinding challenge.