> ## 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.

# Self Crossing Python Solution with Tests

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

LeetCode 335, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Geometry](/catalog/topics/geometry). [View on LeetCode](https://leetcode.com/problems/self-crossing/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 335   # by problem number
lcpy gen -s self_crossing   # by problem name
```

## Problem

You are given an array of integers `distance`.

You start at the point `(0, 0)` on an **X-Y plane,** and you move `distance[0]` meters to the north, then `distance[1]` meters to the west, `distance[2]` meters to the south, `distance[3]` meters to the east, and so on. In other words, after each move, your direction changes counter-clockwise.

Return `true` if your path crosses itself or `false` if it does not.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2022/12/21/11.jpg)

```
Input: distance = [2,1,1,2]
Output: true
Explanation: The path crosses itself at the point (0, 1).
```

![Example 2](https://assets.leetcode.com/uploads/2022/12/21/22.jpg)

```
Input: distance = [1,2,3,4]
Output: false
Explanation: The path does not cross itself at any point.
```

![Example 3](https://assets.leetcode.com/uploads/2022/12/21/33.jpg)

```
Input: distance = [1,1,1,2,1]
Output: true
Explanation: The path crosses itself at the point (0, 0).
```

### Constraints

* 1 \<= distance.length \<= 10^5
* 1 \<= distance\[i] \<= 10^5

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def is_self_crossing(self, distance: list[int]) -> bool:
        d = distance
        for i in range(3, len(d)):
            # Fourth segment crosses the first.
            if d[i] >= d[i - 2] and d[i - 3] >= d[i - 1]:
                return True
            # Fifth segment touches the first.
            if i >= 4 and d[i - 1] == d[i - 3] and d[i] + d[i - 4] >= d[i - 2]:
                return True
            # Sixth segment crosses the first after an expanding spiral contracts.
            if (
                i >= 5
                and d[i - 2] > d[i - 4]
                and d[i - 3] > d[i - 1]
                and d[i - 1] + d[i - 5] >= d[i - 3]
                and d[i] + d[i - 4] >= d[i - 2]
            ):
                return True
        return False
```

## Complexity

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

## Tags


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