forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoyeongkwak.ts
More file actions
26 lines (24 loc) · 691 Bytes
/
hoyeongkwak.ts
File metadata and controls
26 lines (24 loc) · 691 Bytes
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
/*
Time Complexity: O(n)
Space Complexity: O(n)
*/
function insert(intervals: number[][], newInterval: number[]): number[][] {
const newIntervals = []
let idx = 0
const n = intervals.length
while (idx < n && intervals[idx][1] < newInterval[0]) {
newIntervals.push(intervals[idx])
idx++
}
while (idx < n && intervals[idx][0] <= newInterval[1]) {
newInterval[0] = Math.min(newInterval[0], intervals[idx][0])
newInterval[1] = Math.max(newInterval[1], intervals[idx][1])
idx++
}
newIntervals.push(newInterval)
while (idx < n) {
newIntervals.push(intervals[idx])
idx++
}
return newIntervals
};