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)