Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7e3c32d

Browse files
authored
feat: add solutions to lc problem: No.3394 (#4299)
1 parent 629557d commit 7e3c32d

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

‎solution/3300-3399/3394.Check if Grid can be Cut into Sections/README.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,66 @@ func checkValidCuts(n int, rectangles [][]int) bool {
291291
}
292292
```
293293

294+
#### TypeScript
295+
296+
```ts
297+
function checkValidCuts(n: number, rectangles: number[][]): boolean {
298+
const check = (arr: number[][], getVals: (x: number[]) => number[]) => {
299+
let [c, longest] = [3, 0];
300+
301+
for (const x of arr) {
302+
const [start, end] = getVals(x);
303+
304+
if (start < longest) {
305+
longest = Math.max(longest, end);
306+
} else {
307+
longest = end;
308+
if (--c === 0) return true;
309+
}
310+
}
311+
312+
return false;
313+
};
314+
315+
const sortByX = ([a]: number[], [b]: number[]) => a - b;
316+
const sortByY = ([, a]: number[], [, b]: number[]) => a - b;
317+
const getX = ([x1, , x2]: number[]) => [x1, x2];
318+
const getY = ([, y1, , y2]: number[]) => [y1, y2];
319+
320+
return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
321+
}
322+
```
323+
324+
#### JavaScript
325+
326+
```js
327+
function checkValidCuts(n, rectangles) {
328+
const check = (arr, getVals) => {
329+
let [c, longest] = [3, 0];
330+
331+
for (const x of arr) {
332+
const [start, end] = getVals(x);
333+
334+
if (start < longest) {
335+
longest = Math.max(longest, end);
336+
} else {
337+
longest = end;
338+
if (--c === 0) return true;
339+
}
340+
}
341+
342+
return false;
343+
};
344+
345+
const sortByX = ([a], [b]) => a - b;
346+
const sortByY = ([, a], [, b]) => a - b;
347+
const getX = ([x1, , x2]) => [x1, x2];
348+
const getY = ([, y1, , y2]) => [y1, y2];
349+
350+
return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
351+
}
352+
```
353+
294354
<!-- tabs:end -->
295355

296356
<!-- solution:end -->

‎solution/3300-3399/3394.Check if Grid can be Cut into Sections/README_EN.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,66 @@ tags:
121121

122122
```
123123

124+
#### TypeScript
125+
126+
```ts
127+
function checkValidCuts(n: number, rectangles: number[][]): boolean {
128+
const check = (arr: number[][], getVals: (x: number[]) => number[]) => {
129+
let [c, longest] = [3, 0];
130+
131+
for (const x of arr) {
132+
const [start, end] = getVals(x);
133+
134+
if (start < longest) {
135+
longest = Math.max(longest, end);
136+
} else {
137+
longest = end;
138+
if (--c === 0) return true;
139+
}
140+
}
141+
142+
return false;
143+
};
144+
145+
const sortByX = ([a]: number[], [b]: number[]) => a - b;
146+
const sortByY = ([, a]: number[], [, b]: number[]) => a - b;
147+
const getX = ([x1, , x2]: number[]) => [x1, x2];
148+
const getY = ([, y1, , y2]: number[]) => [y1, y2];
149+
150+
return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
151+
}
152+
```
153+
154+
#### JavaScript
155+
156+
```js
157+
function checkValidCuts(n, rectangles) {
158+
const check = (arr, getVals) => {
159+
let [c, longest] = [3, 0];
160+
161+
for (const x of arr) {
162+
const [start, end] = getVals(x);
163+
164+
if (start < longest) {
165+
longest = Math.max(longest, end);
166+
} else {
167+
longest = end;
168+
if (--c === 0) return true;
169+
}
170+
}
171+
172+
return false;
173+
};
174+
175+
const sortByX = ([a], [b]) => a - b;
176+
const sortByY = ([, a], [, b]) => a - b;
177+
const getX = ([x1, , x2]) => [x1, x2];
178+
const getY = ([, y1, , y2]) => [y1, y2];
179+
180+
return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
181+
}
182+
```
183+
124184
<!-- tabs:end -->
125185

126186
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function checkValidCuts(n, rectangles) {
2+
const check = (arr, getVals) => {
3+
let [c, longest] = [3, 0];
4+
5+
for (const x of arr) {
6+
const [start, end] = getVals(x);
7+
8+
if (start < longest) {
9+
longest = Math.max(longest, end);
10+
} else {
11+
longest = end;
12+
if (--c === 0) return true;
13+
}
14+
}
15+
16+
return false;
17+
};
18+
19+
const sortByX = ([a], [b]) => a - b;
20+
const sortByY = ([, a], [, b]) => a - b;
21+
const getX = ([x1, , x2]) => [x1, x2];
22+
const getY = ([, y1, , y2]) => [y1, y2];
23+
24+
return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function checkValidCuts(n: number, rectangles: number[][]): boolean {
2+
const check = (arr: number[][], getVals: (x: number[]) => number[]) => {
3+
let [c, longest] = [3, 0];
4+
5+
for (const x of arr) {
6+
const [start, end] = getVals(x);
7+
8+
if (start < longest) {
9+
longest = Math.max(longest, end);
10+
} else {
11+
longest = end;
12+
if (--c === 0) return true;
13+
}
14+
}
15+
16+
return false;
17+
};
18+
19+
const sortByX = ([a]: number[], [b]: number[]) => a - b;
20+
const sortByY = ([, a]: number[], [, b]: number[]) => a - b;
21+
const getX = ([x1, , x2]: number[]) => [x1, x2];
22+
const getY = ([, y1, , y2]: number[]) => [y1, y2];
23+
24+
return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /