File tree 1 file changed +7
-6
lines changed
scripts/algorithms/P/Perfect Rectangle
1 file changed +7
-6
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 54 ms (Top 81.33%) | Memory: 57.5 MB (Top 77.78%)
1
2
class Solution {
2
3
// Rectangle x0,y0,x1,y1
3
4
public boolean isRectangleCover (int [][] rectangles ) {
@@ -6,21 +7,21 @@ public boolean isRectangleCover(int[][] rectangles) {
6
7
if (r1 [1 ]==r2 [1 ]) return r1 [0 ]-r2 [0 ];
7
8
return r1 [1 ]-r2 [1 ];
8
9
});
9
-
10
+
10
11
// Layering rectangles with pq, ordered by y1 first and x0 second
11
12
PriorityQueue <int []> pq = new PriorityQueue <>((r1 ,r2 )->{
12
13
if (r1 [3 ]==r2 [3 ]) return r1 [0 ]-r2 [0 ];
13
14
return r1 [3 ]-r2 [3 ];
14
15
});
15
-
16
+
16
17
// Create first layer
17
18
pq .offer (rectangles [0 ]);
18
19
int i =1 ;
19
20
while (i <rectangles .length &&rectangles [i ][1 ]==rectangles [i -1 ][1 ]){
20
21
if (rectangles [i ][0 ]!=rectangles [i -1 ][2 ]) return false ;
21
22
pq .offer (rectangles [i ++]);
22
23
}
23
-
24
+
24
25
while (i <rectangles .length ){
25
26
int [] curr = rectangles [i ++];
26
27
int x =curr [0 ];
@@ -38,12 +39,12 @@ public boolean isRectangleCover(int[][] rectangles) {
38
39
if (x <curr [2 ]) return false ;
39
40
pq .offer (curr );
40
41
}
41
-
42
+
42
43
int [] prev =pq .poll ();
43
44
while (!pq .isEmpty ()){
44
45
if (pq .poll ()[3 ]!=prev [3 ]) return false ;
45
46
}
46
-
47
+
47
48
return true ;
48
49
}
49
- }
50
+ }
You can’t perform that action at this time.
0 commit comments