Skip to content

Commit 9acfae1

Browse files
committed
add non-overlapping intervals solution
1 parent 477d47c commit 9acfae1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
public int eraseOverlapIntervals(int[][] intervals) {
5+
6+
// 끝점 기준 정렬
7+
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[1], o2[1]));
8+
9+
int overlap = 0;
10+
11+
int end = intervals[0][1];
12+
13+
for (int i = 1; i < intervals.length; i++) {
14+
int currentStart = intervals[i][0];
15+
int currentEnd = intervals[i][1];
16+
17+
if (currentStart < end) {
18+
overlap++;
19+
} else {
20+
end = currentEnd;
21+
}
22+
23+
}
24+
25+
return overlap;
26+
}
27+
}
28+

0 commit comments

Comments
 (0)