Escape The Ghosts Solution
Intuition\nThe key idea is to determine whether you can reach the target point before any of the ghosts. If a ghost can reach the target at the same time or fas
# Intuition\nThe key idea is to determine whether you can reach the target point before any of the ghosts. If a ghost can reach the target at the same time or faster than you, escaping is impossible. Otherwise, if all ghosts take longer to reach the target, you can escape. The problem boils down to comparing your distance to the target with the ghosts\' distances to the same target.\n\n# Approach\n1-Calculate Your Distance to the Target: Use the Manhattan distance to calculate how far you are from the target.\n\n2-Your distance to the target can be computed as abs(target[0]) + abs(target[1]).\nCalculate Ghosts\' Distances: For each ghost, compute the Manhattan distance from the ghost\'s starting position to the target.\n* For a ghost starting at (xi, yi), the distance to the target is abs(target[0] - xi) + abs(target[1] - yi).\n\n3-Comparison: If any ghost can reach the target as fast as or faster than you (i.e., their distance is less than or equal to yours), return False. If no ghost can reach the target faster, return True.\n\n# Complexity\n- Time complexity: The time complexity is \uD835\uDC42(\uD835\uDC5B) \nO(n), where \uD835\uDC5B is the number of ghosts. For each ghost, we compute the Manhattan distance, which takes constant time. Hence, iterating over all ghosts takes linear time.\n\n- Space complexity: The space complexity is \uD835\uDC42(1)\nO(1) since we only use a few variables to store distances and do not require additional space proportional to the input size.\n\n# Code\n```python3 []\nimport math\nfrom typing import List\n\nclass Solution:\n    def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n        my_distance = abs(target[0]) + abs(target[1])  # Manhattan distance\n        \n        for ghost in ghosts:\n            ghost_distance = abs(target[0] - ghost[0]) + abs(target[1] - ghost[1])\n            \n            if ghost_distance <= my_distance:\n                return False\n        \n        return True\n     \n        \n```
['Python3']