Skip to content

Commit 0c0f57b

Browse files
committed
Add solution #3394
1 parent c849589 commit 0c0f57b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@
10041004
3306|[Count of Substrings Containing Every Vowel and K Consonants II](./solutions/3306-count-of-substrings-containing-every-vowel-and-k-consonants-ii.js)|Medium|
10051005
3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium|
10061006
3392|[Count Subarrays of Length Three With a Condition](./solutions/3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
1007+
3394|[Check if Grid can be Cut into Sections](./solutions/3394-check-if-grid-can-be-cut-into-sections.js)|Medium|
10071008
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./solutions/3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
10081009
3397|[Maximum Number of Distinct Elements After Operations](./solutions/3397-maximum-number-of-distinct-elements-after-operations.js)|Medium|
10091010
3402|[Minimum Operations to Make Columns Strictly Increasing](./solutions/3402-minimum-operations-to-make-columns-strictly-increasing.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 3394. Check if Grid can be Cut into Sections
3+
* https://leetcode.com/problems/check-if-grid-can-be-cut-into-sections/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer n representing the dimensions of an n x n grid, with the origin at the
7+
* bottom-left corner of the grid. You are also given a 2D array of coordinates rectangles, where
8+
* rectangles[i] is in the form [startx, starty, endx, endy], representing a rectangle on the grid.
9+
* Each rectangle is defined as follows:
10+
* - (startx, starty): The bottom-left corner of the rectangle.
11+
* - (endx, endy): The top-right corner of the rectangle.
12+
*
13+
* Note that the rectangles do not overlap. Your task is to determine if it is possible to make
14+
* either two horizontal or two vertical cuts on the grid such that:
15+
* - Each of the three resulting sections formed by the cuts contains at least one rectangle.
16+
* - Every rectangle belongs to exactly one section.
17+
*
18+
* Return true if such cuts can be made; otherwise, return false.
19+
*/
20+
21+
/**
22+
* @param {number} n
23+
* @param {number[][]} rectangles
24+
* @return {boolean}
25+
*/
26+
var checkValidCuts = function(n, rectangles) {
27+
return canPartition(rectangles.map(r => [r[0], r[2]]))
28+
|| canPartition(rectangles.map(r => [r[1], r[3]]));
29+
};
30+
31+
function canPartition(intervals, cuts = 0) {
32+
intervals.sort(([start1, end1], [start2, end2]) => start1 - start2 || end2 - end1);
33+
let maxReach = intervals[0][1];
34+
for (let i = 1; i < intervals.length; i++) {
35+
if (intervals[i][0] >= maxReach && ++cuts === 2) return true;
36+
maxReach = Math.max(intervals[i][1], maxReach);
37+
}
38+
return false;
39+
}

0 commit comments

Comments
 (0)