> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Escape The Ghosts Python Solution with Tests

> Tested Python solution for LeetCode 789 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 789, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/escape-the-ghosts/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 789   # by problem number
lcpy gen -s escape_the_ghosts   # by problem name
```

## Problem

You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, yi]` represents the starting position of the ith ghost. All inputs are **integral coordinates**.

Each turn, you and all the ghosts may independently choose to either **move 1 unit** in any of the four cardinal directions: north, east, south, or west, or **stay still**. All actions happen **simultaneously**.

You escape if and only if you can reach the target **before** any ghost reaches you. If you reach any square (including the target) at the **same time** as a ghost, it **does not** count as an escape.

Return `true` if it is possible to escape regardless of how the ghosts move, otherwise return `false`.

### Examples

```
Input: ghosts = [[1,0],[0,3]], target = [0,1]
Output: true
```

**Explanation:** You can reach the destination (0, 1) after 1 turn, while the ghosts located at (1, 0) and (0, 3) cannot catch up with you.

```
Input: ghosts = [[1,0]], target = [2,0]
Output: false
```

**Explanation:** You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.

```
Input: ghosts = [[2,0]], target = [1,0]
Output: false
```

**Explanation:** The ghost can reach the target at the same time as you.

### Constraints

* 1 \<= ghosts.length \<= 100
* ghosts\[i].length == 2
* -10^4 \<= xi, yi \<= 10^4
* There can be multiple ghosts in the same location.
* target.length == 2
* -10^4 \<= xtarget, ytarget \<= 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/escape_the_ghosts/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/escape_the_ghosts/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def escape_ghosts(self, ghosts: list[list[int]], target: list[int]) -> bool:
        my_dist = abs(target[0]) + abs(target[1])
        return all(
            abs(ghost[0] - target[0]) + abs(ghost[1] - target[1]) > my_dist for ghost in ghosts
        )
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.