From 6fe46bcf4b39471e623a29425798bea3b494ccf0 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 27 Mar 2025 17:49:27 +0800 Subject: [PATCH 1/4] feat: add solutions to lc problem: No.0822 (#4301) No.0822.Card Flipping Game --- .../0822.Card Flipping Game/README.md | 39 ++++++++++++++- .../0822.Card Flipping Game/README_EN.md | 49 ++++++++++++++++++- .../0822.Card Flipping Game/Solution.rs | 32 ++++++++++++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 solution/0800-0899/0822.Card Flipping Game/Solution.rs diff --git a/solution/0800-0899/0822.Card Flipping Game/README.md b/solution/0800-0899/0822.Card Flipping Game/README.md index 916b116f05012..460459ddaa715 100644 --- a/solution/0800-0899/0822.Card Flipping Game/README.md +++ b/solution/0800-0899/0822.Card Flipping Game/README.md @@ -66,7 +66,7 @@ tags: ### 方法一:哈希表 -我们注意到,对于位置 $i$,若 $fronts[i]$ 与 $backs[i]$ 元素相同,则一定不满足条件。 +我们注意到,对于位置 $i$,若 $\textit{fronts}[i]$ 与 $\textit{backs}[i]$ 元素相同,则一定不满足条件。 因此,我们先找出正面与背面相同的元素,记录在哈希表 $s$ 中。 @@ -195,6 +195,43 @@ function flipgame(fronts: number[], backs: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn flipgame(fronts: Vec, backs: Vec) -> i32 { + let n = fronts.len(); + let mut s: HashSet = HashSet::new(); + + for i in 0..n { + if fronts[i] == backs[i] { + s.insert(fronts[i]); + } + } + + let mut ans = 9999; + for &v in fronts.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + for &v in backs.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + + if ans == 9999 { + 0 + } else { + ans + } + } +} +``` + #### C# ```cs diff --git a/solution/0800-0899/0822.Card Flipping Game/README_EN.md b/solution/0800-0899/0822.Card Flipping Game/README_EN.md index 89abfd94a2f80..dca0480e715a6 100644 --- a/solution/0800-0899/0822.Card Flipping Game/README_EN.md +++ b/solution/0800-0899/0822.Card Flipping Game/README_EN.md @@ -59,7 +59,17 @@ There are no good integers no matter how we flip the cards, so we return 0. -### Solution 1 +### Solution 1: Hash Table + +We observe that for position $i$, if $\textit{fronts}[i]$ is equal to $\textit{backs}[i]$, then it certainly does not satisfy the condition. + +Therefore, we first identify all elements that appear the same on both the front and back sides and record them in a hash set $s$. + +Next, we iterate through all elements in both the front and back arrays. For any element $x$ that is **not** in the hash set $s$, we update the minimum value of the answer. + +Finally, if we find any element that satisfies the condition, we return the minimum answer; otherwise, we return $0$. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the arrays. @@ -180,6 +190,43 @@ function flipgame(fronts: number[], backs: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashSet; + +impl Solution { + pub fn flipgame(fronts: Vec, backs: Vec) -> i32 { + let n = fronts.len(); + let mut s: HashSet = HashSet::new(); + + for i in 0..n { + if fronts[i] == backs[i] { + s.insert(fronts[i]); + } + } + + let mut ans = 9999; + for &v in fronts.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + for &v in backs.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + + if ans == 9999 { + 0 + } else { + ans + } + } +} +``` + #### C# ```cs diff --git a/solution/0800-0899/0822.Card Flipping Game/Solution.rs b/solution/0800-0899/0822.Card Flipping Game/Solution.rs new file mode 100644 index 0000000000000..25f15912f0308 --- /dev/null +++ b/solution/0800-0899/0822.Card Flipping Game/Solution.rs @@ -0,0 +1,32 @@ +use std::collections::HashSet; + +impl Solution { + pub fn flipgame(fronts: Vec, backs: Vec) -> i32 { + let n = fronts.len(); + let mut s: HashSet = HashSet::new(); + + for i in 0..n { + if fronts[i] == backs[i] { + s.insert(fronts[i]); + } + } + + let mut ans = 9999; + for &v in fronts.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + for &v in backs.iter() { + if !s.contains(&v) { + ans = ans.min(v); + } + } + + if ans == 9999 { + 0 + } else { + ans + } + } +} From 629557dd21b91a7b483cd442fa864f54a6091f82 Mon Sep 17 00:00:00 2001 From: Rajender Thota Date: Thu, 27 Mar 2025 08:20:23 -0400 Subject: [PATCH 2/4] feat: add solutions to lc problem: No.3394 (#4297) --- .../README.md | 173 +++++++++++++++++- 1 file changed, 170 insertions(+), 3 deletions(-) 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 b7dbdc3ca45bc..8c12993e4a46b 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 @@ -103,25 +103,192 @@ tags: #### Python3 ```python +from typing import List +class Solution: + def countLineIntersections(self, coordinates: List[tuple[int, int]]) -> bool: + lines = 0 + overlap = 0 + for value, marker in coordinates: + if marker == 0: + overlap -= 1 + else: + overlap += 1 + + if overlap == 0: + lines += 1 + + return lines >= 3 + + def checkValidCuts(self, n: int, rectangles: List[List[int]]) -> bool: + y_coordinates = [] + x_coordinates = [] + + for rect in rectangles: + x1, y1, x2, y2 = rect + y_coordinates.append((y1, 1)) # start + y_coordinates.append((y2, 0)) # end + + x_coordinates.append((x1, 1)) # start + x_coordinates.append((x2, 0)) # end + + # Sort by coordinate value, and for tie, put end (0) before start (1) + y_coordinates.sort(key=lambda x: (x[0], x[1])) + x_coordinates.sort(key=lambda x: (x[0], x[1])) + + return self.countLineIntersections(y_coordinates) or self.countLineIntersections(x_coordinates) ``` #### Java ```java - +class Solution { + // Helper class to mimic C++ pair + static class Pair { + int value; + int type; + + Pair(int value, int type) { + this.value = value; + this.type = type; + } + } + + private boolean countLineIntersections(List coordinates) { + int lines = 0; + int overlap = 0; + + for (Pair coord : coordinates) { + if (coord.type == 0) { + overlap--; + } else { + overlap++; + } + + if (overlap == 0) { + lines++; + } + } + + return lines >= 3; + } + + public boolean checkValidCuts(int n, int[][] rectangles) { + List yCoordinates = new ArrayList<>(); + List xCoordinates = new ArrayList<>(); + + for (int[] rectangle : rectangles) { + // rectangle = [x1, y1, x2, y2] + yCoordinates.add(new Pair(rectangle[1], 1)); // y1, start + yCoordinates.add(new Pair(rectangle[3], 0)); // y2, end + + xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start + xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end + } + + Comparator comparator = (a, b) -> { + if (a.value != b.value) return Integer.compare(a.value, b.value); + return Integer.compare(a.type, b.type); // End (0) before Start (1) + }; + + Collections.sort(yCoordinates, comparator); + Collections.sort(xCoordinates, comparator); + + return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates); +} + +} ``` #### C++ ```cpp - +class Solution { +#define pii pair + + bool countLineIntersections(vector& coordinates){ + int lines = 0; + int overlap = 0; + for(int i=0;i=3; + } +public: + bool checkValidCuts(int n, vector>& rectangles) { + vector y_cordinates,x_cordinates; + for(auto& rectangle: rectangles){ + y_cordinates.push_back(make_pair(rectangle[1],1)); + y_cordinates.push_back(make_pair(rectangle[3],0)); + x_cordinates.push_back(make_pair(rectangle[0],1)); + x_cordinates.push_back(make_pair(rectangle[2],0)); + } + sort(y_cordinates.begin(),y_cordinates.end()); + sort(x_cordinates.begin(),x_cordinates.end()); + + //Line-Sweep on x and y cordinates + return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates)); + } +}; ``` #### Go ```go - +type Pair struct { + val int + typ int // 1 = start, 0 = end +} + +func countLineIntersections(coords []Pair) bool { + lines := 0 + overlap := 0 + for _, p := range coords { + if p.typ == 0 { + overlap-- + } else { + overlap++ + } + if overlap == 0 { + lines++ + } + } + return lines >= 3 +} + +func checkValidCuts(n int, rectangles [][]int) bool { + var xCoords []Pair + var yCoords []Pair + + for _, rect := range rectangles { + x1, y1, x2, y2 := rect[0], rect[1], rect[2], rect[3] + + yCoords = append(yCoords, Pair{y1, 1}) // start + yCoords = append(yCoords, Pair{y2, 0}) // end + + xCoords = append(xCoords, Pair{x1, 1}) + xCoords = append(xCoords, Pair{x2, 0}) + } + + sort.Slice(yCoords, func(i, j int) bool { + if yCoords[i].val == yCoords[j].val { + return yCoords[i].typ < yCoords[j].typ // end before start + } + return yCoords[i].val < yCoords[j].val + }) + + sort.Slice(xCoords, func(i, j int) bool { + if xCoords[i].val == xCoords[j].val { + return xCoords[i].typ < xCoords[j].typ + } + return xCoords[i].val < xCoords[j].val + }) + + return countLineIntersections(yCoords) || countLineIntersections(xCoords) +} ``` From 7e3c32dcd4178fb576f6d6f2dac95603df246790 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 27 Mar 2025 15:21:05 +0300 Subject: [PATCH 3/4] feat: add solutions to lc problem: No.3394 (#4299) --- .../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); +} From 1be76361df639597497087e506cc5b2a964f598d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 27 Mar 2025 20:28:12 +0800 Subject: [PATCH 4/4] feat: add solutions to lc problem: No.3394 (#4302) No.3394.Check if Grid can be Cut into Sections close #4295 --- .../README.md | 84 ++++----- .../README_EN.md | 171 +++++++++++++++++- .../Solution.cpp | 33 ++++ .../Solution.go | 51 ++++++ .../Solution.java | 55 ++++++ .../Solution.py | 34 ++++ 6 files changed, 386 insertions(+), 42 deletions(-) create mode 100644 solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.cpp create mode 100644 solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.go create mode 100644 solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.java create mode 100644 solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.py 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 2ca663d21d04f..8510e7223b5ce 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 @@ -103,8 +103,6 @@ tags: #### Python3 ```python -from typing import List - class Solution: def countLineIntersections(self, coordinates: List[tuple[int, int]]) -> bool: lines = 0 @@ -136,14 +134,16 @@ class Solution: y_coordinates.sort(key=lambda x: (x[0], x[1])) x_coordinates.sort(key=lambda x: (x[0], x[1])) - return self.countLineIntersections(y_coordinates) or self.countLineIntersections(x_coordinates) + return self.countLineIntersections( + y_coordinates + ) or self.countLineIntersections(x_coordinates) ``` #### Java ```java class Solution { - // Helper class to mimic C++ pair + // Helper class to mimic C++ pair static class Pair { int value; int type; @@ -173,30 +173,29 @@ class Solution { return lines >= 3; } - public boolean checkValidCuts(int n, int[][] rectangles) { - List yCoordinates = new ArrayList<>(); - List xCoordinates = new ArrayList<>(); - - for (int[] rectangle : rectangles) { - // rectangle = [x1, y1, x2, y2] - yCoordinates.add(new Pair(rectangle[1], 1)); // y1, start - yCoordinates.add(new Pair(rectangle[3], 0)); // y2, end + public boolean checkValidCuts(int n, int[][] rectangles) { + List yCoordinates = new ArrayList<>(); + List xCoordinates = new ArrayList<>(); - xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start - xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end - } + for (int[] rectangle : rectangles) { + // rectangle = [x1, y1, x2, y2] + yCoordinates.add(new Pair(rectangle[1], 1)); // y1, start + yCoordinates.add(new Pair(rectangle[3], 0)); // y2, end - Comparator comparator = (a, b) -> { - if (a.value != b.value) return Integer.compare(a.value, b.value); - return Integer.compare(a.type, b.type); // End (0) before Start (1) - }; + xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start + xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end + } - Collections.sort(yCoordinates, comparator); - Collections.sort(xCoordinates, comparator); + Comparator comparator = (a, b) -> { + if (a.value != b.value) return Integer.compare(a.value, b.value); + return Integer.compare(a.type, b.type); // End (0) before Start (1) + }; - return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates); -} + Collections.sort(yCoordinates, comparator); + Collections.sort(xCoordinates, comparator); + return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates); + } } ``` @@ -204,32 +203,35 @@ class Solution { ```cpp class Solution { -#define pii pair +#define pii pair - bool countLineIntersections(vector& coordinates){ + bool countLineIntersections(vector& coordinates) { int lines = 0; int overlap = 0; - for(int i=0;i=3; + return lines >= 3; } + public: bool checkValidCuts(int n, vector>& rectangles) { - vector y_cordinates,x_cordinates; - for(auto& rectangle: rectangles){ - y_cordinates.push_back(make_pair(rectangle[1],1)); - y_cordinates.push_back(make_pair(rectangle[3],0)); - x_cordinates.push_back(make_pair(rectangle[0],1)); - x_cordinates.push_back(make_pair(rectangle[2],0)); + vector y_cordinates, x_cordinates; + for (auto& rectangle : rectangles) { + y_cordinates.push_back(make_pair(rectangle[1], 1)); + y_cordinates.push_back(make_pair(rectangle[3], 0)); + x_cordinates.push_back(make_pair(rectangle[0], 1)); + x_cordinates.push_back(make_pair(rectangle[2], 0)); } - sort(y_cordinates.begin(),y_cordinates.end()); - sort(x_cordinates.begin(),x_cordinates.end()); + sort(y_cordinates.begin(), y_cordinates.end()); + sort(x_cordinates.begin(), x_cordinates.end()); - //Line-Sweep on x and y cordinates + // Line-Sweep on x and y cordinates return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates)); } }; @@ -239,8 +241,8 @@ public: ```go type Pair struct { - val int - typ int // 1 = start, 0 = end + val int + typ int // 1 = start, 0 = end } func countLineIntersections(coords []Pair) bool { 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 0a0f7fcc471cb..ce63a4085df94 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 @@ -100,25 +100,194 @@ tags: #### Python3 ```python - +class Solution: + def countLineIntersections(self, coordinates: List[tuple[int, int]]) -> bool: + lines = 0 + overlap = 0 + for value, marker in coordinates: + if marker == 0: + overlap -= 1 + else: + overlap += 1 + + if overlap == 0: + lines += 1 + + return lines >= 3 + + def checkValidCuts(self, n: int, rectangles: List[List[int]]) -> bool: + y_coordinates = [] + x_coordinates = [] + + for rect in rectangles: + x1, y1, x2, y2 = rect + y_coordinates.append((y1, 1)) # start + y_coordinates.append((y2, 0)) # end + + x_coordinates.append((x1, 1)) # start + x_coordinates.append((x2, 0)) # end + + # Sort by coordinate value, and for tie, put end (0) before start (1) + y_coordinates.sort(key=lambda x: (x[0], x[1])) + x_coordinates.sort(key=lambda x: (x[0], x[1])) + + return self.countLineIntersections( + y_coordinates + ) or self.countLineIntersections(x_coordinates) ``` #### Java ```java +class Solution { + // Helper class to mimic C++ pair + static class Pair { + int value; + int type; + + Pair(int value, int type) { + this.value = value; + this.type = type; + } + } + + private boolean countLineIntersections(List coordinates) { + int lines = 0; + int overlap = 0; + + for (Pair coord : coordinates) { + if (coord.type == 0) { + overlap--; + } else { + overlap++; + } + + if (overlap == 0) { + lines++; + } + } + + return lines >= 3; + } + + public boolean checkValidCuts(int n, int[][] rectangles) { + List yCoordinates = new ArrayList<>(); + List xCoordinates = new ArrayList<>(); + + for (int[] rectangle : rectangles) { + // rectangle = [x1, y1, x2, y2] + yCoordinates.add(new Pair(rectangle[1], 1)); // y1, start + yCoordinates.add(new Pair(rectangle[3], 0)); // y2, end + + xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start + xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end + } + + Comparator comparator = (a, b) -> { + if (a.value != b.value) return Integer.compare(a.value, b.value); + return Integer.compare(a.type, b.type); // End (0) before Start (1) + }; + Collections.sort(yCoordinates, comparator); + Collections.sort(xCoordinates, comparator); + + return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates); + } +} ``` #### C++ ```cpp +class Solution { +#define pii pair + + bool countLineIntersections(vector& coordinates) { + int lines = 0; + int overlap = 0; + for (int i = 0; i < coordinates.size(); ++i) { + if (coordinates[i].second == 0) + overlap--; + else + overlap++; + if (overlap == 0) + lines++; + } + return lines >= 3; + } + +public: + bool checkValidCuts(int n, vector>& rectangles) { + vector y_cordinates, x_cordinates; + for (auto& rectangle : rectangles) { + y_cordinates.push_back(make_pair(rectangle[1], 1)); + y_cordinates.push_back(make_pair(rectangle[3], 0)); + x_cordinates.push_back(make_pair(rectangle[0], 1)); + x_cordinates.push_back(make_pair(rectangle[2], 0)); + } + sort(y_cordinates.begin(), y_cordinates.end()); + sort(x_cordinates.begin(), x_cordinates.end()); + // Line-Sweep on x and y cordinates + return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates)); + } +}; ``` #### Go ```go +type Pair struct { + val int + typ int // 1 = start, 0 = end +} + +func countLineIntersections(coords []Pair) bool { + lines := 0 + overlap := 0 + for _, p := range coords { + if p.typ == 0 { + overlap-- + } else { + overlap++ + } + if overlap == 0 { + lines++ + } + } + return lines >= 3 +} +func checkValidCuts(n int, rectangles [][]int) bool { + var xCoords []Pair + var yCoords []Pair + + for _, rect := range rectangles { + x1, y1, x2, y2 := rect[0], rect[1], rect[2], rect[3] + + yCoords = append(yCoords, Pair{y1, 1}) // start + yCoords = append(yCoords, Pair{y2, 0}) // end + + xCoords = append(xCoords, Pair{x1, 1}) + xCoords = append(xCoords, Pair{x2, 0}) + } + + sort.Slice(yCoords, func(i, j int) bool { + if yCoords[i].val == yCoords[j].val { + return yCoords[i].typ < yCoords[j].typ // end before start + } + return yCoords[i].val < yCoords[j].val + }) + + sort.Slice(xCoords, func(i, j int) bool { + if xCoords[i].val == xCoords[j].val { + return xCoords[i].typ < xCoords[j].typ + } + return xCoords[i].val < xCoords[j].val + }) + + return countLineIntersections(yCoords) || countLineIntersections(xCoords) +} ``` #### TypeScript diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.cpp b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.cpp new file mode 100644 index 0000000000000..18ed3a53b7c78 --- /dev/null +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.cpp @@ -0,0 +1,33 @@ +class Solution { +#define pii pair + + bool countLineIntersections(vector& coordinates) { + int lines = 0; + int overlap = 0; + for (int i = 0; i < coordinates.size(); ++i) { + if (coordinates[i].second == 0) + overlap--; + else + overlap++; + if (overlap == 0) + lines++; + } + return lines >= 3; + } + +public: + bool checkValidCuts(int n, vector>& rectangles) { + vector y_cordinates, x_cordinates; + for (auto& rectangle : rectangles) { + y_cordinates.push_back(make_pair(rectangle[1], 1)); + y_cordinates.push_back(make_pair(rectangle[3], 0)); + x_cordinates.push_back(make_pair(rectangle[0], 1)); + x_cordinates.push_back(make_pair(rectangle[2], 0)); + } + sort(y_cordinates.begin(), y_cordinates.end()); + sort(x_cordinates.begin(), x_cordinates.end()); + + // Line-Sweep on x and y cordinates + return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates)); + } +}; \ No newline at end of file diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.go b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.go new file mode 100644 index 0000000000000..fa4aa804a6628 --- /dev/null +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.go @@ -0,0 +1,51 @@ +type Pair struct { + val int + typ int // 1 = start, 0 = end +} + +func countLineIntersections(coords []Pair) bool { + lines := 0 + overlap := 0 + for _, p := range coords { + if p.typ == 0 { + overlap-- + } else { + overlap++ + } + if overlap == 0 { + lines++ + } + } + return lines >= 3 +} + +func checkValidCuts(n int, rectangles [][]int) bool { + var xCoords []Pair + var yCoords []Pair + + for _, rect := range rectangles { + x1, y1, x2, y2 := rect[0], rect[1], rect[2], rect[3] + + yCoords = append(yCoords, Pair{y1, 1}) // start + yCoords = append(yCoords, Pair{y2, 0}) // end + + xCoords = append(xCoords, Pair{x1, 1}) + xCoords = append(xCoords, Pair{x2, 0}) + } + + sort.Slice(yCoords, func(i, j int) bool { + if yCoords[i].val == yCoords[j].val { + return yCoords[i].typ < yCoords[j].typ // end before start + } + return yCoords[i].val < yCoords[j].val + }) + + sort.Slice(xCoords, func(i, j int) bool { + if xCoords[i].val == xCoords[j].val { + return xCoords[i].typ < xCoords[j].typ + } + return xCoords[i].val < xCoords[j].val + }) + + return countLineIntersections(yCoords) || countLineIntersections(xCoords) +} \ No newline at end of file diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.java b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.java new file mode 100644 index 0000000000000..f87482bb6e404 --- /dev/null +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.java @@ -0,0 +1,55 @@ +class Solution { + // Helper class to mimic C++ pair + static class Pair { + int value; + int type; + + Pair(int value, int type) { + this.value = value; + this.type = type; + } + } + + private boolean countLineIntersections(List coordinates) { + int lines = 0; + int overlap = 0; + + for (Pair coord : coordinates) { + if (coord.type == 0) { + overlap--; + } else { + overlap++; + } + + if (overlap == 0) { + lines++; + } + } + + return lines >= 3; + } + + public boolean checkValidCuts(int n, int[][] rectangles) { + List yCoordinates = new ArrayList<>(); + List xCoordinates = new ArrayList<>(); + + for (int[] rectangle : rectangles) { + // rectangle = [x1, y1, x2, y2] + yCoordinates.add(new Pair(rectangle[1], 1)); // y1, start + yCoordinates.add(new Pair(rectangle[3], 0)); // y2, end + + xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start + xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end + } + + Comparator comparator = (a, b) -> { + if (a.value != b.value) return Integer.compare(a.value, b.value); + return Integer.compare(a.type, b.type); // End (0) before Start (1) + }; + + Collections.sort(yCoordinates, comparator); + Collections.sort(xCoordinates, comparator); + + return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates); + } +} \ No newline at end of file diff --git a/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.py b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.py new file mode 100644 index 0000000000000..154f5eab8b496 --- /dev/null +++ b/solution/3300-3399/3394.Check if Grid can be Cut into Sections/Solution.py @@ -0,0 +1,34 @@ +class Solution: + def countLineIntersections(self, coordinates: List[tuple[int, int]]) -> bool: + lines = 0 + overlap = 0 + for value, marker in coordinates: + if marker == 0: + overlap -= 1 + else: + overlap += 1 + + if overlap == 0: + lines += 1 + + return lines >= 3 + + def checkValidCuts(self, n: int, rectangles: List[List[int]]) -> bool: + y_coordinates = [] + x_coordinates = [] + + for rect in rectangles: + x1, y1, x2, y2 = rect + y_coordinates.append((y1, 1)) # start + y_coordinates.append((y2, 0)) # end + + x_coordinates.append((x1, 1)) # start + x_coordinates.append((x2, 0)) # end + + # Sort by coordinate value, and for tie, put end (0) before start (1) + y_coordinates.sort(key=lambda x: (x[0], x[1])) + x_coordinates.sort(key=lambda x: (x[0], x[1])) + + return self.countLineIntersections( + y_coordinates + ) or self.countLineIntersections(x_coordinates)