KatInterview

805. Escape The Ghosts

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The core challenge is to determine if we can reach a target point from the origin (0,0) before any ghost intercepts us. Both our movement and the ghosts' movement are on a 2D grid, restricted to the four cardinal directions (north, east, south, west). This means the shortest travel time between two points is measured by their Manhattan distance: |x1 - x2| + |y1 - y2|.

Solution

Escape The Ghosts in Python

class Solution:
    def escapeGhosts(self, ghosts: list[list[int]], target: list[int]) -> bool:
        # Calculate the Manhattan distance for us to reach the target from the origin (0,0).
        # This is the minimum number of turns required for us.
        my_dist = abs(target[0]) + abs(target[1])

        # Iterate through each ghost to see if any can intercept us.
        for ghost in ghosts:
            # Calculate the Manhattan distance for the ghost to reach the target.
            # This is the minimum number of turns for the ghost.
            ghost_dist = abs(target[0] - ghost[0]) + abs(target[1] - ghost[1])

            # If any ghost can reach the target in the same number of turns or fewer,
            # it can wait at the target and catch us. We cannot escape.
            if ghost_dist <= my_dist:
                return False

        # If no ghost can reach the target faster than or at the same time as us,
        # we can safely reach the target. So, we can escape.
        return True

Escape The Ghosts in Java

class Solution {
    public boolean escapeGhosts(int[][] ghosts, int[] target) {
        // Calculate the Manhattan distance for us to reach the target from the origin (0,0).
        // This is the minimum number of turns required for us.
        int myDist = Math.abs(target[0]) + Math.abs(target[1]);

        // Iterate through each ghost to see if any can intercept us.
        for (int[] ghost : ghosts) {
            // Calculate the Manhattan distance for the ghost to reach the target.
            // This is the minimum number of turns for the ghost.
            int ghostDist = Math.abs(target[0] - ghost[0]) + Math.abs(target[1] - ghost[1]);

            // If any ghost can reach the target in the same number of turns or fewer,
            // it can wait at the target and catch us. We cannot escape.
            if (ghostDist <= myDist) {
                return false;
            }
        }

        // If no ghost can reach the target faster than or at the same time as us,
        // we can safely reach the target. So, we can escape.
        return true;
    }
}

Escape The Ghosts in C++

class Solution {
public:
    bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
        // Calculate the Manhattan distance for us to reach the target from the origin (0,0).
        // This is the minimum number of turns required for us.
        int my_dist = abs(target[0]) + abs(target[1]);

        // Iterate through each ghost to see if any can intercept us.
        for (const auto& ghost : ghosts) {
            // Calculate the Manhattan distance for the ghost to reach the target.
            // This is the minimum number of turns for the ghost.
            int ghost_dist = abs(target[0] - ghost[0]) + abs(target[1] - ghost[1]);

            // If any ghost can reach the target in the same number of turns or fewer,
            // it can wait at the target and catch us. We cannot escape.
            if (ghost_dist <= my_dist) {
                return false;
            }
        }

        // If no ghost can reach the target faster than or at the same time as us,
        // we can safely reach the target. So, we can escape.
        return true;
    }
};

Escape The Ghosts in TypeScript

function escapeGhosts(ghosts: number[][], target: number[]): boolean {
    // Calculate the Manhattan distance for us to reach the target from the origin (0,0).
    // This is the minimum number of turns required for us.
    const myDist = Math.abs(target[0]) + Math.abs(target[1]);

    // Iterate through each ghost to see if any can intercept us.
    for (const ghost of ghosts) {
        // Calculate the Manhattan distance for the ghost to reach the target.
        // This is the minimum number of turns for the ghost.
        const ghostDist = Math.abs(target[0] - ghost[0]) + Math.abs(target[1] - ghost[1]);

        // If any ghost can reach the target in the same number of turns or fewer,
        // it can wait at the target and catch us. We cannot escape.
        if (ghostDist <= myDist) {
            return false;
        }
    }

    // If no ghost can reach the target faster than or at the same time as us,
    // we can safely reach the target. So, we can escape.
    return true;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.