Skip to content

Commit 574340b

Browse files
Update README.md
Solution for the question 3394 using sweep line algorithm
1 parent b26a557 commit 574340b

File tree

1 file changed

+170
-3
lines changed
  • solution/3300-3399/3394.Check if Grid can be Cut into Sections

1 file changed

+170
-3
lines changed

solution/3300-3399/3394.Check if Grid can be Cut into Sections/README.md

+170-3
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,192 @@ tags:
103103
#### Python3
104104

105105
```python
106+
from typing import List
106107

108+
class Solution:
109+
def countLineIntersections(self, coordinates: List[tuple[int, int]]) -> bool:
110+
lines = 0
111+
overlap = 0
112+
for value, marker in coordinates:
113+
if marker == 0:
114+
overlap -= 1
115+
else:
116+
overlap += 1
117+
118+
if overlap == 0:
119+
lines += 1
120+
121+
return lines >= 3
122+
123+
def checkValidCuts(self, n: int, rectangles: List[List[int]]) -> bool:
124+
y_coordinates = []
125+
x_coordinates = []
126+
127+
for rect in rectangles:
128+
x1, y1, x2, y2 = rect
129+
y_coordinates.append((y1, 1)) # start
130+
y_coordinates.append((y2, 0)) # end
131+
132+
x_coordinates.append((x1, 1)) # start
133+
x_coordinates.append((x2, 0)) # end
134+
135+
# Sort by coordinate value, and for tie, put end (0) before start (1)
136+
y_coordinates.sort(key=lambda x: (x[0], x[1]))
137+
x_coordinates.sort(key=lambda x: (x[0], x[1]))
138+
139+
return self.countLineIntersections(y_coordinates) or self.countLineIntersections(x_coordinates)
107140
```
108141

109142
#### Java
110143

111144
```java
112-
145+
class Solution {
146+
// Helper class to mimic C++ pair<int, int>
147+
static class Pair {
148+
int value;
149+
int type;
150+
151+
Pair(int value, int type) {
152+
this.value = value;
153+
this.type = type;
154+
}
155+
}
156+
157+
private boolean countLineIntersections(List<Pair> coordinates) {
158+
int lines = 0;
159+
int overlap = 0;
160+
161+
for (Pair coord : coordinates) {
162+
if (coord.type == 0) {
163+
overlap--;
164+
} else {
165+
overlap++;
166+
}
167+
168+
if (overlap == 0) {
169+
lines++;
170+
}
171+
}
172+
173+
return lines >= 3;
174+
}
175+
176+
public boolean checkValidCuts(int n, int[][] rectangles) {
177+
List<Pair> yCoordinates = new ArrayList<>();
178+
List<Pair> xCoordinates = new ArrayList<>();
179+
180+
for (int[] rectangle : rectangles) {
181+
// rectangle = [x1, y1, x2, y2]
182+
yCoordinates.add(new Pair(rectangle[1], 1)); // y1, start
183+
yCoordinates.add(new Pair(rectangle[3], 0)); // y2, end
184+
185+
xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start
186+
xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end
187+
}
188+
189+
Comparator<Pair> comparator = (a, b) -> {
190+
if (a.value != b.value) return Integer.compare(a.value, b.value);
191+
return Integer.compare(a.type, b.type); // End (0) before Start (1)
192+
};
193+
194+
Collections.sort(yCoordinates, comparator);
195+
Collections.sort(xCoordinates, comparator);
196+
197+
return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates);
198+
}
199+
200+
}
113201
```
114202

115203
#### C++
116204

117205
```cpp
118-
206+
class Solution {
207+
#define pii pair<int,int>
208+
209+
bool countLineIntersections(vector<pii>& coordinates){
210+
int lines = 0;
211+
int overlap = 0;
212+
for(int i=0;i<coordinates.size();++i){
213+
if(coordinates[i].second==0) overlap--;
214+
else overlap++;
215+
if(overlap==0)
216+
lines++;
217+
}
218+
return lines>=3;
219+
}
220+
public:
221+
bool checkValidCuts(int n, vector<vector<int>>& rectangles) {
222+
vector<pii> y_cordinates,x_cordinates;
223+
for(auto& rectangle: rectangles){
224+
y_cordinates.push_back(make_pair(rectangle[1],1));
225+
y_cordinates.push_back(make_pair(rectangle[3],0));
226+
x_cordinates.push_back(make_pair(rectangle[0],1));
227+
x_cordinates.push_back(make_pair(rectangle[2],0));
228+
}
229+
sort(y_cordinates.begin(),y_cordinates.end());
230+
sort(x_cordinates.begin(),x_cordinates.end());
231+
232+
//Line-Sweep on x and y cordinates
233+
return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates));
234+
}
235+
};
119236
```
120237
121238
#### Go
122239
123240
```go
124-
241+
type Pair struct {
242+
val int
243+
typ int // 1 = start, 0 = end
244+
}
245+
246+
func countLineIntersections(coords []Pair) bool {
247+
lines := 0
248+
overlap := 0
249+
for _, p := range coords {
250+
if p.typ == 0 {
251+
overlap--
252+
} else {
253+
overlap++
254+
}
255+
if overlap == 0 {
256+
lines++
257+
}
258+
}
259+
return lines >= 3
260+
}
261+
262+
func checkValidCuts(n int, rectangles [][]int) bool {
263+
var xCoords []Pair
264+
var yCoords []Pair
265+
266+
for _, rect := range rectangles {
267+
x1, y1, x2, y2 := rect[0], rect[1], rect[2], rect[3]
268+
269+
yCoords = append(yCoords, Pair{y1, 1}) // start
270+
yCoords = append(yCoords, Pair{y2, 0}) // end
271+
272+
xCoords = append(xCoords, Pair{x1, 1})
273+
xCoords = append(xCoords, Pair{x2, 0})
274+
}
275+
276+
sort.Slice(yCoords, func(i, j int) bool {
277+
if yCoords[i].val == yCoords[j].val {
278+
return yCoords[i].typ < yCoords[j].typ // end before start
279+
}
280+
return yCoords[i].val < yCoords[j].val
281+
})
282+
283+
sort.Slice(xCoords, func(i, j int) bool {
284+
if xCoords[i].val == xCoords[j].val {
285+
return xCoords[i].typ < xCoords[j].typ
286+
}
287+
return xCoords[i].val < xCoords[j].val
288+
})
289+
290+
return countLineIntersections(yCoords) || countLineIntersections(xCoords)
291+
}
125292
```
126293

127294
<!-- tabs:end -->

0 commit comments

Comments
 (0)