Skip to content

Commit

Permalink
Feat: 57. Insert Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
HC-kang committed Oct 25, 2024
1 parent 5ba1835 commit 7c15d7e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions insert-interval/HC-kang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* https://leetcode.com/problems/insert-interval
* T.C. O(n)
* S.C. O(n)
*/
function insert(intervals: number[][], newInterval: number[]): number[][] {
const result: number[][] = [];

for (const [start, end] of intervals) {
if (end < newInterval[0]) {
result.push([start, end]);
} else if (newInterval[1] < start) {
result.push(newInterval);
newInterval = [start, end];
} else {
newInterval[0] = Math.min(newInterval[0], start);
newInterval[1] = Math.max(newInterval[1], end);
}
}

result.push(newInterval);
return result;
}

0 comments on commit 7c15d7e

Please sign in to comment.