math and geometry October 25, 2022

Escape the ghosts

Time O(n) Space O(1) Open original problem

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)