-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathPerfect Rectangle.js
36 lines (33 loc) · 1.35 KB
/
Perfect Rectangle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Runtime: 202 ms (Top 50.00%) | Memory: 51.7 MB (Top 100.00%)
var isRectangleCover = function(R) {
// left to right, bottom to top (so sort on bottom first, then left)
R.sort(([left1, bottom1], [left2, bottom2]) => bottom1 - bottom2 || left1 - left2);
// Find all corners
let leftMost = Infinity,
bottomMost = Infinity,
rightMost = -Infinity,
topMost = -Infinity;
for (let [left, bottom, right, top] of R) {
leftMost = Math.min(leftMost, left);
bottomMost = Math.min(bottomMost, bottom);
rightMost = Math.max(rightMost, right);
topMost = Math.max(topMost, top);
}
// All calculations are with-respect-to large rectangle
let CH = new Array(rightMost - leftMost).fill(0);
for (let [left, bottom, right, top] of R) {
const baseHeight = bottom - bottomMost; // how high base is
const ceilHeight = top - bottomMost; // how high ceil is
for (let tempLeft = left; tempLeft < right; tempLeft++) {
if (CH[tempLeft - leftMost] != baseHeight)
return false; // > is a duplicate cell < is a gap/ missing cell/ hole
CH[tempLeft - leftMost] = ceilHeight;
}
}
const rectHeight = topMost - bottomMost;
for (let ceilHeight of CH) {
if (ceilHeight !== rectHeight)
return false;
}
return true;
}