Skip to content

Commit 1be7636

Browse files
authored
feat: add solutions to lc problem: No.3394 (#4302)
No.3394.Check if Grid can be Cut into Sections close #4295
1 parent 7e3c32d commit 1be7636

File tree

6 files changed

+386
-42
lines changed

6 files changed

+386
-42
lines changed

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

+43-41
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ tags:
103103
#### Python3
104104

105105
```python
106-
from typing import List
107-
108106
class Solution:
109107
def countLineIntersections(self, coordinates: List[tuple[int, int]]) -> bool:
110108
lines = 0
@@ -136,14 +134,16 @@ class Solution:
136134
y_coordinates.sort(key=lambda x: (x[0], x[1]))
137135
x_coordinates.sort(key=lambda x: (x[0], x[1]))
138136

139-
return self.countLineIntersections(y_coordinates) or self.countLineIntersections(x_coordinates)
137+
return self.countLineIntersections(
138+
y_coordinates
139+
) or self.countLineIntersections(x_coordinates)
140140
```
141141

142142
#### Java
143143

144144
```java
145145
class Solution {
146-
// Helper class to mimic C++ pair<int, int>
146+
// Helper class to mimic C++ pair<int, int>
147147
static class Pair {
148148
int value;
149149
int type;
@@ -173,63 +173,65 @@ class Solution {
173173
return lines >= 3;
174174
}
175175

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
176+
public boolean checkValidCuts(int n, int[][] rectangles) {
177+
List<Pair> yCoordinates = new ArrayList<>();
178+
List<Pair> xCoordinates = new ArrayList<>();
184179

185-
xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start
186-
xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end
187-
}
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
188184

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-
};
185+
xCoordinates.add(new Pair(rectangle[0], 1)); // x1, start
186+
xCoordinates.add(new Pair(rectangle[2], 0)); // x2, end
187+
}
193188

194-
Collections.sort(yCoordinates, comparator);
195-
Collections.sort(xCoordinates, comparator);
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+
};
196193

197-
return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates);
198-
}
194+
Collections.sort(yCoordinates, comparator);
195+
Collections.sort(xCoordinates, comparator);
199196

197+
return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates);
198+
}
200199
}
201200
```
202201

203202
#### C++
204203

205204
```cpp
206205
class Solution {
207-
#define pii pair<int,int>
206+
#define pii pair<int, int>
208207

209-
bool countLineIntersections(vector<pii>& coordinates){
208+
bool countLineIntersections(vector<pii>& coordinates) {
210209
int lines = 0;
211210
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)
211+
for (int i = 0; i < coordinates.size(); ++i) {
212+
if (coordinates[i].second == 0)
213+
overlap--;
214+
else
215+
overlap++;
216+
if (overlap == 0)
216217
lines++;
217218
}
218-
return lines>=3;
219+
return lines >= 3;
219220
}
221+
220222
public:
221223
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));
224+
vector<pii> y_cordinates, x_cordinates;
225+
for (auto& rectangle : rectangles) {
226+
y_cordinates.push_back(make_pair(rectangle[1], 1));
227+
y_cordinates.push_back(make_pair(rectangle[3], 0));
228+
x_cordinates.push_back(make_pair(rectangle[0], 1));
229+
x_cordinates.push_back(make_pair(rectangle[2], 0));
228230
}
229-
sort(y_cordinates.begin(),y_cordinates.end());
230-
sort(x_cordinates.begin(),x_cordinates.end());
231+
sort(y_cordinates.begin(), y_cordinates.end());
232+
sort(x_cordinates.begin(), x_cordinates.end());
231233

232-
//Line-Sweep on x and y cordinates
234+
// Line-Sweep on x and y cordinates
233235
return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates));
234236
}
235237
};
@@ -239,8 +241,8 @@ public:
239241
240242
```go
241243
type Pair struct {
242-
val int
243-
typ int // 1 = start, 0 = end
244+
val int
245+
typ int // 1 = start, 0 = end
244246
}
245247
246248
func countLineIntersections(coords []Pair) bool {

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

+170-1
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,194 @@ tags:
100100
#### Python3
101101

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

106139
#### Java
107140

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

191+
Collections.sort(yCoordinates, comparator);
192+
Collections.sort(xCoordinates, comparator);
193+
194+
return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates);
195+
}
196+
}
110197
```
111198

112199
#### C++
113200

114201
```cpp
202+
class Solution {
203+
#define pii pair<int, int>
204+
205+
bool countLineIntersections(vector<pii>& coordinates) {
206+
int lines = 0;
207+
int overlap = 0;
208+
for (int i = 0; i < coordinates.size(); ++i) {
209+
if (coordinates[i].second == 0)
210+
overlap--;
211+
else
212+
overlap++;
213+
if (overlap == 0)
214+
lines++;
215+
}
216+
return lines >= 3;
217+
}
218+
219+
public:
220+
bool checkValidCuts(int n, vector<vector<int>>& rectangles) {
221+
vector<pii> y_cordinates, x_cordinates;
222+
for (auto& rectangle : rectangles) {
223+
y_cordinates.push_back(make_pair(rectangle[1], 1));
224+
y_cordinates.push_back(make_pair(rectangle[3], 0));
225+
x_cordinates.push_back(make_pair(rectangle[0], 1));
226+
x_cordinates.push_back(make_pair(rectangle[2], 0));
227+
}
228+
sort(y_cordinates.begin(), y_cordinates.end());
229+
sort(x_cordinates.begin(), x_cordinates.end());
115230

231+
// Line-Sweep on x and y cordinates
232+
return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates));
233+
}
234+
};
116235
```
117236
118237
#### Go
119238
120239
```go
240+
type Pair struct {
241+
val int
242+
typ int // 1 = start, 0 = end
243+
}
244+
245+
func countLineIntersections(coords []Pair) bool {
246+
lines := 0
247+
overlap := 0
248+
for _, p := range coords {
249+
if p.typ == 0 {
250+
overlap--
251+
} else {
252+
overlap++
253+
}
254+
if overlap == 0 {
255+
lines++
256+
}
257+
}
258+
return lines >= 3
259+
}
121260
261+
func checkValidCuts(n int, rectangles [][]int) bool {
262+
var xCoords []Pair
263+
var yCoords []Pair
264+
265+
for _, rect := range rectangles {
266+
x1, y1, x2, y2 := rect[0], rect[1], rect[2], rect[3]
267+
268+
yCoords = append(yCoords, Pair{y1, 1}) // start
269+
yCoords = append(yCoords, Pair{y2, 0}) // end
270+
271+
xCoords = append(xCoords, Pair{x1, 1})
272+
xCoords = append(xCoords, Pair{x2, 0})
273+
}
274+
275+
sort.Slice(yCoords, func(i, j int) bool {
276+
if yCoords[i].val == yCoords[j].val {
277+
return yCoords[i].typ < yCoords[j].typ // end before start
278+
}
279+
return yCoords[i].val < yCoords[j].val
280+
})
281+
282+
sort.Slice(xCoords, func(i, j int) bool {
283+
if xCoords[i].val == xCoords[j].val {
284+
return xCoords[i].typ < xCoords[j].typ
285+
}
286+
return xCoords[i].val < xCoords[j].val
287+
})
288+
289+
return countLineIntersections(yCoords) || countLineIntersections(xCoords)
290+
}
122291
```
123292

124293
#### TypeScript
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
#define pii pair<int, int>
3+
4+
bool countLineIntersections(vector<pii>& coordinates) {
5+
int lines = 0;
6+
int overlap = 0;
7+
for (int i = 0; i < coordinates.size(); ++i) {
8+
if (coordinates[i].second == 0)
9+
overlap--;
10+
else
11+
overlap++;
12+
if (overlap == 0)
13+
lines++;
14+
}
15+
return lines >= 3;
16+
}
17+
18+
public:
19+
bool checkValidCuts(int n, vector<vector<int>>& rectangles) {
20+
vector<pii> y_cordinates, x_cordinates;
21+
for (auto& rectangle : rectangles) {
22+
y_cordinates.push_back(make_pair(rectangle[1], 1));
23+
y_cordinates.push_back(make_pair(rectangle[3], 0));
24+
x_cordinates.push_back(make_pair(rectangle[0], 1));
25+
x_cordinates.push_back(make_pair(rectangle[2], 0));
26+
}
27+
sort(y_cordinates.begin(), y_cordinates.end());
28+
sort(x_cordinates.begin(), x_cordinates.end());
29+
30+
// Line-Sweep on x and y cordinates
31+
return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates));
32+
}
33+
};

0 commit comments

Comments
 (0)