1
var isRectangleCover = function (R) {
2
// left to right, bottom to top (so sort on bottom first, then left)
3
R.sort(
4
([left1, bottom1], [left2, bottom2]) => bottom1 - bottom2 || left1 - left2
5
);
6

7
// Find all corners
8
let leftMost = Infinity,
9
bottomMost = Infinity,
10
rightMost = -Infinity,
11
topMost = -Infinity;
12
for (let [left, bottom, right, top] of R) {
13
leftMost = Math.min(leftMost, left);
14
bottomMost = Math.min(bottomMost, bottom);
15
rightMost = Math.max(rightMost, right);
16
topMost = Math.max(topMost, top);
17
}
18

19
// All calculations are with-respect-to large rectangle
20
let CH = new Array(rightMost - leftMost).fill(0);
21
for (let [left, bottom, right, top] of R) {
22
const baseHeight = bottom - bottomMost; // how high base is
23
const ceilHeight = top - bottomMost; // how high ceil is
24
for (let tempLeft = left; tempLeft < right; tempLeft++) {
25
if (CH[tempLeft - leftMost] != baseHeight) return false; // > is a duplicate cell < is a gap/ missing cell/ hole
26
CH[tempLeft - leftMost] = ceilHeight;
27
}
28
}
29

30
const rectHeight = topMost - bottomMost;
31
for (let ceilHeight of CH) {
32
if (ceilHeight !== rectHeight) return false;
33
}
34
return true;
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0