Skip to content

Commit 8a26cf0

Browse files
committed
add Non-overlapping Intervals solution
1 parent ab27f8d commit 8a26cf0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* [Problem]: [435] Non-overlapping Intervals
3+
* (https://leetcode.com/problems/non-overlapping-intervals/description/)
4+
*/
5+
6+
//시간복잡도 O(n log n)
7+
//공간복잡도 O(1)
8+
function eraseOverlapIntervals(intervals: number[][]): number {
9+
intervals.sort((a, b) => a[1] - b[1]);
10+
11+
let count = 0;
12+
let prev = intervals[0][1];
13+
14+
for (let i = 1; i < intervals.length; i++) {
15+
let [start, end] = intervals[i];
16+
17+
if (prev > start) {
18+
count++;
19+
} else {
20+
prev = end;
21+
}
22+
}
23+
24+
return count;
25+
}

0 commit comments

Comments
 (0)