Repository navigation
Expand file tree
/
Copy pathinsertInterval.js
More file actions
65 lines (60 loc) · 2.17 KB
/
Copy pathinsertInterval.js
File metadata and controls
65 lines (60 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//TC:O(n) SC:O(n)
function insertInterval(newInterval, intervals) {
let result = [];
let [newStart, newEnd] = newInterval;
let isIntervalInserted = false;
for(let [currStart, currEnd] of intervals) {
if(newEnd < currStart) {
// If the new interval ends before the current interval starts
if(!isIntervalInserted) {
result.push([newStart, newEnd]);
isIntervalInserted = true;
}
result.push([currStart, currEnd]);
} else if(currEnd < newStart) {
// If the current interval ends before the new interval starts
result.push([currStart, currEnd]);
} else {
// Overlapping intervals
newStart = Math.min(newStart, currStart);
newEnd = Math.max(newEnd, currEnd);
}
}
if(!isIntervalInserted) {
result.push([newStart, newEnd]);
}
return result;
}
let newInterval = [4, 8];
let intervals = [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]];
console.log(insertInterval(newInterval, intervals));
const tests = [
{
label: "Original test",
newInterval: [4, 8],
intervals: [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]],
expected: [[1, 2], [3, 10], [12, 16]],
},
{
label: "Insert before all intervals (no overlap)",
newInterval: [0, 1],
intervals: [[2, 3], [4, 5]],
expected: [[0, 1], [2, 3], [4, 5]],
},
{
label: "Insert after all intervals (no overlap)",
newInterval: [10, 12],
intervals: [[1, 2], [3, 5], [6, 8]],
expected: [[1, 2], [3, 5], [6, 8], [10, 12]],
}
];
// ---- Run All Tests ----
tests.forEach(({ label, newInterval, intervals, expected }, i) => {
const result = insertInterval([...newInterval], JSON.parse(JSON.stringify(intervals)));
const passed = JSON.stringify(result) === JSON.stringify(expected);
console.log(`Test ${i + 1} - ${label}: ${passed ? "✅ Passed" : "❌ Failed"}`);
if (!passed) {
console.log(` Expected: ${JSON.stringify(expected)}`);
console.log(` Received: ${JSON.stringify(result)}`);
}
});