Skip to content

feat: add solutions to lc problem: No.3394 #4299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,66 @@ func checkValidCuts(n int, rectangles [][]int) bool {
}
```

#### TypeScript

```ts
function checkValidCuts(n: number, rectangles: number[][]): boolean {
const check = (arr: number[][], getVals: (x: number[]) => number[]) => {
let [c, longest] = [3, 0];

for (const x of arr) {
const [start, end] = getVals(x);

if (start < longest) {
longest = Math.max(longest, end);
} else {
longest = end;
if (--c === 0) return true;
}
}

return false;
};

const sortByX = ([a]: number[], [b]: number[]) => a - b;
const sortByY = ([, a]: number[], [, b]: number[]) => a - b;
const getX = ([x1, , x2]: number[]) => [x1, x2];
const getY = ([, y1, , y2]: number[]) => [y1, y2];

return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
}
```

#### JavaScript

```js
function checkValidCuts(n, rectangles) {
const check = (arr, getVals) => {
let [c, longest] = [3, 0];

for (const x of arr) {
const [start, end] = getVals(x);

if (start < longest) {
longest = Math.max(longest, end);
} else {
longest = end;
if (--c === 0) return true;
}
}

return false;
};

const sortByX = ([a], [b]) => a - b;
const sortByY = ([, a], [, b]) => a - b;
const getX = ([x1, , x2]) => [x1, x2];
const getY = ([, y1, , y2]) => [y1, y2];

return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,66 @@ tags:

```

#### TypeScript

```ts
function checkValidCuts(n: number, rectangles: number[][]): boolean {
const check = (arr: number[][], getVals: (x: number[]) => number[]) => {
let [c, longest] = [3, 0];

for (const x of arr) {
const [start, end] = getVals(x);

if (start < longest) {
longest = Math.max(longest, end);
} else {
longest = end;
if (--c === 0) return true;
}
}

return false;
};

const sortByX = ([a]: number[], [b]: number[]) => a - b;
const sortByY = ([, a]: number[], [, b]: number[]) => a - b;
const getX = ([x1, , x2]: number[]) => [x1, x2];
const getY = ([, y1, , y2]: number[]) => [y1, y2];

return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
}
```

#### JavaScript

```js
function checkValidCuts(n, rectangles) {
const check = (arr, getVals) => {
let [c, longest] = [3, 0];

for (const x of arr) {
const [start, end] = getVals(x);

if (start < longest) {
longest = Math.max(longest, end);
} else {
longest = end;
if (--c === 0) return true;
}
}

return false;
};

const sortByX = ([a], [b]) => a - b;
const sortByY = ([, a], [, b]) => a - b;
const getX = ([x1, , x2]) => [x1, x2];
const getY = ([, y1, , y2]) => [y1, y2];

return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function checkValidCuts(n, rectangles) {
const check = (arr, getVals) => {
let [c, longest] = [3, 0];

for (const x of arr) {
const [start, end] = getVals(x);

if (start < longest) {
longest = Math.max(longest, end);
} else {
longest = end;
if (--c === 0) return true;
}
}

return false;
};

const sortByX = ([a], [b]) => a - b;
const sortByY = ([, a], [, b]) => a - b;
const getX = ([x1, , x2]) => [x1, x2];
const getY = ([, y1, , y2]) => [y1, y2];

return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function checkValidCuts(n: number, rectangles: number[][]): boolean {
const check = (arr: number[][], getVals: (x: number[]) => number[]) => {
let [c, longest] = [3, 0];

for (const x of arr) {
const [start, end] = getVals(x);

if (start < longest) {
longest = Math.max(longest, end);
} else {
longest = end;
if (--c === 0) return true;
}
}

return false;
};

const sortByX = ([a]: number[], [b]: number[]) => a - b;
const sortByY = ([, a]: number[], [, b]: number[]) => a - b;
const getX = ([x1, , x2]: number[]) => [x1, x2];
const getY = ([, y1, , y2]: number[]) => [y1, y2];

return check(rectangles.toSorted(sortByX), getX) || check(rectangles.toSorted(sortByY), getY);
}