@@ -103,8 +103,6 @@ tags:
103
103
#### Python3
104
104
105
105
``` python
106
- from typing import List
107
-
108
106
class Solution :
109
107
def countLineIntersections (self , coordinates : List[tuple[int , int ]]) -> bool :
110
108
lines = 0
@@ -136,14 +134,16 @@ class Solution:
136
134
y_coordinates.sort(key = lambda x : (x[0 ], x[1 ]))
137
135
x_coordinates.sort(key = lambda x : (x[0 ], x[1 ]))
138
136
139
- return self .countLineIntersections(y_coordinates) or self .countLineIntersections(x_coordinates)
137
+ return self .countLineIntersections(
138
+ y_coordinates
139
+ ) or self .countLineIntersections(x_coordinates)
140
140
```
141
141
142
142
#### Java
143
143
144
144
``` java
145
145
class Solution {
146
- // Helper class to mimic C++ pair<int, int>
146
+ // Helper class to mimic C++ pair<int, int>
147
147
static class Pair {
148
148
int value;
149
149
int type;
@@ -173,63 +173,65 @@ class Solution {
173
173
return lines >= 3 ;
174
174
}
175
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
176
+ public boolean checkValidCuts (int n , int [][] rectangles ) {
177
+ List<Pair > yCoordinates = new ArrayList<> ();
178
+ List<Pair > xCoordinates = new ArrayList<> ();
184
179
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
188
184
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
+ }
193
188
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
+ };
196
193
197
- return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates );
198
- }
194
+ Collections . sort(yCoordinates, comparator );
195
+ Collections . sort(xCoordinates, comparator);
199
196
197
+ return countLineIntersections(yCoordinates) || countLineIntersections(xCoordinates);
198
+ }
200
199
}
201
200
```
202
201
203
202
#### C++
204
203
205
204
``` cpp
206
205
class Solution {
207
- #define pii pair<int,int>
206
+ #define pii pair<int, int>
208
207
209
- bool countLineIntersections(vector<pii>& coordinates){
208
+ bool countLineIntersections(vector<pii>& coordinates) {
210
209
int lines = 0;
211
210
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)
216
217
lines++;
217
218
}
218
- return lines>= 3 ;
219
+ return lines >= 3 ;
219
220
}
221
+
220
222
public:
221
223
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));
228
230
}
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());
231
233
232
- //Line-Sweep on x and y cordinates
234
+ // Line-Sweep on x and y cordinates
233
235
return (countLineIntersections(y_cordinates) or countLineIntersections(x_cordinates));
234
236
}
235
237
};
@@ -239,8 +241,8 @@ public:
239
241
240
242
```go
241
243
type Pair struct {
242
- val int
243
- typ int // 1 = start, 0 = end
244
+ val int
245
+ typ int // 1 = start, 0 = end
244
246
}
245
247
246
248
func countLineIntersections(coords []Pair) bool {
0 commit comments