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

# Check if Grid can be Cut into Sections

> Tested Python solution for LeetCode 3394 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 3394, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/check-if-grid-can-be-cut-into-sections/description/).

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

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

## Problem

You are given an integer \<code>n\</code> representing the dimensions of an \<code>n x n\</code> grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates \<code>rectangles\</code>, where \<code>rectangles\[i]\</code> is in the form \<code>\[start\<sub>x\</sub>, start\<sub>y\</sub>, end\<sub>x\</sub>, end\<sub>y\</sub>]\</code>, representing a rectangle on the grid. Each rectangle is defined as follows:

* \<code>(start\<sub>x\</sub>, start\<sub>y\</sub>)\</code>: The bottom-left corner of the rectangle.
* \<code>(end\<sub>x\</sub>, end\<sub>y\</sub>)\</code>: The top-right corner of the rectangle.

\<strong>Note \</strong>that the rectangles do not overlap. Your task is to determine if it is possible to make \<strong>either two horizontal or two vertical cuts\</strong> on the grid such that:

* Each of the three resulting sections formed by the cuts contains \<strong>at least\</strong> one rectangle.
* Every rectangle belongs to \<strong>exactly\</strong> one section.

Return \<code>true\</code> if such cuts can be made; otherwise, return \<code>false\</code>.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png)

```
Input: n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]
Output: true
```

**Explanation:** The grid is shown in the diagram. We can make horizontal cuts at \<code>y = 2\</code> and \<code>y = 4\</code>. Hence, the output is true.

![Example 2](https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png)

```
Input: n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]
Output: true
```

**Explanation:** We can make vertical cuts at \<code>x = 2\</code> and \<code>x = 3\</code>. Hence, the output is true.

```
Input: n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]
Output: false
```

**Explanation:** We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, the output is false.

### Constraints

* 3 \<= n \<= 10^9
* 3 \<= rectangles.length \<= 10^5
* 0 \<= rectangles\[i]\[0] \< rectangles\[i]\[2] \<= n
* 0 \<= rectangles\[i]\[1] \< rectangles\[i]\[3] \<= n
* No two rectangles overlap.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m log m)
    # Space: O(m)
    def check_valid_cuts(self, n: int, rectangles: list[list[int]]) -> bool:
        return self._has_two_gaps(rectangles, 0) or self._has_two_gaps(rectangles, 1)

    def _has_two_gaps(self, rectangles: list[list[int]], axis: int) -> bool:
        rects = sorted(rectangles, key=lambda rect: (rect[axis], rect[axis + 2]))
        gaps = 0
        end = rects[0][axis + 2]
        for rect in rects:
            if rect[axis] >= end:
                gaps += 1
                if gaps >= 2:
                    return True
            end = max(end, rect[axis + 2])
        return False
```

## Complexity

| Time | Space |
| - | - |
| O(m log m) | O(m) |

## Tags

[NeetCode All](/catalog/neetcode).


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