Skip to content

Commit 45338f6

Browse files
committed
Runtime: 54 ms (Top 81.33%) | Memory: 57.5 MB (Top 77.78%)
1 parent 589aba1 commit 45338f6

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

scripts/algorithms/P/Perfect Rectangle/Perfect Rectangle.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 54 ms (Top 81.33%) | Memory: 57.5 MB (Top 77.78%)
12
class Solution {
23
// Rectangle x0,y0,x1,y1
34
public boolean isRectangleCover(int[][] rectangles) {
@@ -6,21 +7,21 @@ public boolean isRectangleCover(int[][] rectangles) {
67
if(r1[1]==r2[1]) return r1[0]-r2[0];
78
return r1[1]-r2[1];
89
});
9-
10+
1011
// Layering rectangles with pq, ordered by y1 first and x0 second
1112
PriorityQueue<int[]> pq = new PriorityQueue<>((r1,r2)->{
1213
if(r1[3]==r2[3]) return r1[0]-r2[0];
1314
return r1[3]-r2[3];
1415
});
15-
16+
1617
// Create first layer
1718
pq.offer(rectangles[0]);
1819
int i=1;
1920
while(i<rectangles.length&&rectangles[i][1]==rectangles[i-1][1]){
2021
if(rectangles[i][0]!=rectangles[i-1][2]) return false;
2122
pq.offer(rectangles[i++]);
2223
}
23-
24+
2425
while(i<rectangles.length){
2526
int[] curr = rectangles[i++];
2627
int x=curr[0];
@@ -38,12 +39,12 @@ public boolean isRectangleCover(int[][] rectangles) {
3839
if(x<curr[2]) return false;
3940
pq.offer(curr);
4041
}
41-
42+
4243
int[] prev=pq.poll();
4344
while(!pq.isEmpty()){
4445
if(pq.poll()[3]!=prev[3]) return false;
4546
}
46-
47+
4748
return true;
4849
}
49-
}
50+
}

0 commit comments

Comments
 (0)