-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinsertInterval.js
More file actions
48 lines (43 loc) · 1.02 KB
/
insertInterval.js
File metadata and controls
48 lines (43 loc) · 1.02 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
/**
* Definition for an interval.
* function Interval(start, end) {
* this.start = start;
* this.end = end;
* }
*/
/**
* @param {Interval[]} intervals
* @param {Interval} newInterval
* @return {Interval[]}
*/
var insert = function(intervals, newInterval) {
let result = [];
let addInterval = true;
intervals.forEach(interval => {
if (interval[1] >= newInterval[0] && addInterval) {
result.push(interval);
result.push(newInterval);
addInterval = false;
} else {
result.push(interval);
}
})
function canCombine(left, right) {
if (left[1] < right[0]) {
return false;
} else if (left[1] >= right[0]) {
return true;
}
}
function combine(left, right) {
if (left[1] === right[0]) {
return [left[0], right[1]];
} else if (left[1] > right[1]) {
return [left[0], left[1]];
} else if (right[1] >= left[1]) {
return [left[0], right[1]];
}
}
return result;
};
console.log(insert([[1,2],[3,5],[6,7],[8,10],[12,16]], [4,8]));