Skip to content

Commit 5981886

Browse files
committed
Sync LeetCode submission Runtime - 241 ms (88.96%), Memory - 83.7 MB (25.23%)
1 parent 7abeba3 commit 5981886

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<p>You are given an integer <code>n</code> representing the dimensions of an <code>n x n</code><!-- notionvc: fa9fe4ed-dff8-4410-8196-346f2d430795 --> 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:</p>
2+
3+
<ul>
4+
<li><code>(start<sub>x</sub>, start<sub>y</sub>)</code>: The bottom-left corner of the rectangle.</li>
5+
<li><code>(end<sub>x</sub>, end<sub>y</sub>)</code>: The top-right corner of the rectangle.</li>
6+
</ul>
7+
8+
<p><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:</p>
9+
10+
<ul>
11+
<li>Each of the three resulting sections formed by the cuts contains <strong>at least</strong> one rectangle.</li>
12+
<li>Every rectangle belongs to <strong>exactly</strong> one section.</li>
13+
</ul>
14+
15+
<p>Return <code>true</code> if such cuts can be made; otherwise, return <code>false</code>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io">n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]</span></p>
22+
23+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
24+
25+
<p><strong>Explanation:</strong></p>
26+
27+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png" style="width: 285px; height: 280px;" /></p>
28+
29+
<p>The grid is shown in the diagram. We can make horizontal cuts at <code>y = 2</code> and <code>y = 4</code>. Hence, output is true.</p>
30+
</div>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]]</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png" style="width: 240px; height: 240px;" /></p>
42+
43+
<p>We can make vertical cuts at <code>x = 2</code> and <code>x = 3</code>. Hence, output is true.</p>
44+
</div>
45+
46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>Input:</strong> <span class="example-io">n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]</span></p>
50+
51+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
52+
53+
<p><strong>Explanation:</strong></p>
54+
55+
<p>We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false.</p>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
<p><strong>Constraints:</strong></p>
60+
61+
<ul>
62+
<li><code>3 &lt;= n &lt;= 10<sup>9</sup></code></li>
63+
<li><code>3 &lt;= rectangles.length &lt;= 10<sup>5</sup></code></li>
64+
<li><code>0 &lt;= rectangles[i][0] &lt; rectangles[i][2] &lt;= n</code></li>
65+
<li><code>0 &lt;= rectangles[i][1] &lt; rectangles[i][3] &lt;= n</code></li>
66+
<li>No two rectangles overlap.</li>
67+
</ul>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Approach: Line Sweep
2+
3+
# Time: O(n log n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def checkValidCuts(self, n: int, rectangles: List[List[int]]) -> bool:
8+
# Check if valid cuts can be made in a specific dimension
9+
def _check_cuts(rectangles, dim):
10+
gap_count = 0
11+
rectangles.sort(key=lambda rect: rect[dim])
12+
13+
furthest_end = rectangles[0][dim + 2]
14+
15+
for i in range(1, len(rectangles)):
16+
rect = rectangles[i]
17+
if furthest_end <= rect[dim]:
18+
gap_count += 1
19+
20+
furthest_end = max(furthest_end, rect[dim + 2])
21+
22+
return gap_count >= 2
23+
24+
return _check_cuts(rectangles, 0) or _check_cuts(rectangles, 1)
25+
26+

0 commit comments

Comments
 (0)