Escape the ghosts

October 25, 2022

math-and-geometry

Problem URL: Escape the ghosts

We will calculate the Manhattan distance between the ghost and the target. If the ghost is closer to the target than the player, we will return False. Otherwise, we will return True.

class Solution:
    def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:
        x, y = target
        distance = abs(x) + abs(y)
        for i, j in ghosts:
            if distance >= abs(x-i) + abs(y-j):
                return False
        return True

Time Complexity: O(n)
Space Complexity: O(1)