1
var isRectangleCover = function (R) {2
// left to right, bottom to top (so sort on bottom first, then left)4
([left1, bottom1], [left2, bottom2]) => bottom1 - bottom2 || left1 - left28
let leftMost = Infinity,10
rightMost = -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);19
// All calculations are with-respect-to large rectangle20
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 is23
const ceilHeight = top - bottomMost; // how high ceil is24
for (let tempLeft = left; tempLeft < right; tempLeft++) {25
if (CH[tempLeft - leftMost] != baseHeight) return false; // > is a duplicate cell < is a gap/ missing cell/ hole26
CH[tempLeft - leftMost] = ceilHeight;30
const rectHeight = topMost - bottomMost;31
for (let ceilHeight of CH) {32
if (ceilHeight !== rectHeight) return false;