Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Last active October 8, 2020 05:49
Show Gist options
  • Select an option

  • Save blacksheep557/984616b45dd6d46c0b99c343c9cd76a0 to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/984616b45dd6d46c0b99c343c9cd76a0 to your computer and use it in GitHub Desktop.
function boxShadows(rectangles = [[]]) {
rectangles.sort((a, b) => a[0] - b[0]);
// console.log(rectangles);
let linearMap = new Map();
for (let [x, y, width, height] of rectangles) {
let currentWidth = linearMap.get(x);
if (currentWidth === undefined) {
currentWidth = width + x
} else {
currentWidth = x + width > currentWidth ? width : currentWidth
}
linearMap.set(x, currentWidth)
}
// getRanges(linearMap)
let linearArray = Array.from(linearMap.entries());
// console.log(linearArray)
let ranges = [];
for (let i = 0; i < linearArray.length - 1; i++) {
for (let j = i + 1; i < linearArray.length - 1; j++) {
let [x1, thickness1] = linearArray[i];
let [x2, thickness2] = linearArray[j];
if (x2 > thickness1) break;
if (thickness1 >= thickness2) {
linearArray.splice(j, 1);
// ranges.push([s1, t1]);
j--;
} else if (thickness1 >= x2 && thickness2 > thickness1) {
linearArray.splice(j, 1);
// ranges.push([s1, t2]);
linearArray[i] = [x1, thickness2];
j--
}
}
}
// console.log(linearArray)
return linearArray;
}
@McLarenCollege
Copy link

Score: 85 / 100

Correctness: 60 / 60

Code Quality: 25/40

  • What do variables s1 and t1 represent? Name them better.
  • remove unused variables and console statements before submitting them.
  • Is the purpose of nested for loops to merge shadows? if that's the case you can sort the list and then you will have to check just the next element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment