I’m merging sorted half-open intervals like [start, end), but some overlaps produce duplicate output ranges instead of one merged range. I expected [[1,5],[6,8]] for the sample below. What logic error am I missing?
function merge(ranges) {
const out = [];
for (const cur of ranges) {
const last = out[out.length - 1]
if (!last || cur[0] > last[1]) out.push(cur)
if (last && cur[0] <= last[1]) {
out.push([last[0], Math.max(last[1], cur[1])])
}
}
return out
}
BayMax