forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoonDongKang.ts
More file actions
34 lines (27 loc) · 801 Bytes
/
HoonDongKang.ts
File metadata and controls
34 lines (27 loc) · 801 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
27
28
29
30
31
32
33
34
/**
* [Problem]: [57] Insert Interval
* (https://leetcode.com/problems/insert-interval/)
*/
//시간복잡도 O(n)
//공간복잡도 O(n)
function insert(intervals: number[][], newInterval: number[]): number[][] {
const result: number[][] = [];
let idx = 0;
while (idx < intervals.length && intervals[idx][1] < newInterval[0]) {
result.push(intervals[idx]);
idx++;
}
while (idx < intervals.length && intervals[idx][0] <= newInterval[1]) {
newInterval = [
Math.min(intervals[idx][0], newInterval[0]),
Math.max(intervals[idx][1], newInterval[1]),
];
idx++;
}
result.push(newInterval);
while (idx < intervals.length) {
result.push(intervals[idx]);
idx++;
}
return result;
}