Skip to content

Commit 7c15d7e

Browse files
committed
Feat: 57. Insert Interval
1 parent 5ba1835 commit 7c15d7e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

insert-interval/HC-kang.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* https://leetcode.com/problems/insert-interval
3+
* T.C. O(n)
4+
* S.C. O(n)
5+
*/
6+
function insert(intervals: number[][], newInterval: number[]): number[][] {
7+
const result: number[][] = [];
8+
9+
for (const [start, end] of intervals) {
10+
if (end < newInterval[0]) {
11+
result.push([start, end]);
12+
} else if (newInterval[1] < start) {
13+
result.push(newInterval);
14+
newInterval = [start, end];
15+
} else {
16+
newInterval[0] = Math.min(newInterval[0], start);
17+
newInterval[1] = Math.max(newInterval[1], end);
18+
}
19+
}
20+
21+
result.push(newInterval);
22+
return result;
23+
}

0 commit comments

Comments
 (0)