From d09fb689e0e3a860bbe5b4d55e881d6e2b73aea5 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 25 Mar 2025 23:40:39 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.3394 --- .../README.md | 60 +++++++++++++++++++ .../README_EN.md | 60 +++++++++++++++++++ .../Solution.js | 25 ++++++++ .../Solution.ts | 25 ++++++++ 4 files changed, 170 insertions(+) create mode 100644 solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.js create mode 100644 solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.ts diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README.md b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README.md index 8c12993e4a46b..2ca663d21d04f 100644 --- a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README.md +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README.md @@ -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); +} +``` + diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README_EN.md b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README_EN.md index e1fafb9fd0973..0a0f7fcc471cb 100644 --- a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README_EN.md +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/README_EN.md @@ -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); +} +``` + diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.js b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.js new file mode 100644 index 0000000000000..287c1148bd43d --- /dev/null +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.js @@ -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); +} diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.ts b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.ts new file mode 100644 index 0000000000000..43f7499fb8b5d --- /dev/null +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.ts @@ -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); +}