Skip to content

Commit 0498b47

Browse files
committed
Feat: 435. Non-overlapping Intervals
1 parent ef9182e commit 0498b47

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

non-overlapping-intervals/HC-kang.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* https://leetcode.com/problems/non-overlapping-intervals
3+
* T.C. O(n logn)
4+
* S.C. O(1)
5+
*
6+
* [[1,2],[2,3],[3,4],[1,3]] =(sort by end)=> [[1,2],[2,3],[1,3],[3,4]]
7+
*
8+
* 0 1 2 3 4...
9+
* [=) pass
10+
* [=) pass
11+
* [===) count++
12+
* ^^^
13+
* [=) pass
14+
*/
15+
function eraseOverlapIntervals(intervals: number[][]): number {
16+
intervals.sort((a, b) => a[1] - b[1]);
17+
18+
let count = 0;
19+
let end = -Infinity;
20+
21+
for (let i = 0; i < intervals.length; i++) {
22+
if (intervals[i][0] < end) { //
23+
count++;
24+
} else {
25+
end = intervals[i][1];
26+
}
27+
}
28+
return count;
29+
}

0 commit comments

Comments
 (0)