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); +}