Skip to content

Commit fd8347b

Browse files
committed
feat: add solutions to lc problem: No.3394
1 parent 350a49f commit fd8347b

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
@@ -124,6 +124,66 @@ tags:
124124

125125
```
126126

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

129189
<!-- 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)