Each day I solve several coding challenges and puzzles from Codr’s ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.
function intersected(a, b) {
if (a[0] > b[1] || a[1] < b[0]) return false;
return true;
}
function mergeTwo(a, b) {
return [Math.min(a[0], b[0]), Math.max(a[1], b[1])];
}
function merge(VLS) {
VLS.sort((a, b) => a[0] - b[0]);
for (let i = 0; i < VLS.length - 1; i++) {
const cur = VLS[i];
const next = VLS[i + 1];
if (intersected(cur, next)) {
VLS[i] = undefined;
VLS[i + 1] = mergeTwo(cur, next);
}
}
return VLS.filter(q => q);
}
let arr = [
[2,10],
[10,11],
[11,12]
]
let A = merge(arr);
A = A[0][1]
// A = ? (number)
We have encountered this challenge a couple of episodes ago (https://dev.to/codr/road-to-genius-superior-52-4b5m), this time we have to solve it and not just fix some bugs.
#computer-science #development #javascript #programming #coding