Skip to content

Commit f8876eb

Browse files
committed
solve non overlapping intervals
1 parent 47fcaf5 commit f8876eb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int eraseOverlapIntervals(int[][] intervals) {
3+
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
4+
int count = 0;
5+
6+
int prv_end = intervals[0][1];
7+
for (int i = 1; i < intervals.length; i++) {
8+
int start = intervals[i][0];
9+
int end = intervals[i][1];
10+
if (prv_end > start) {
11+
count++;
12+
prv_end = Math.min(end, prv_end);
13+
} else {
14+
prv_end = end;
15+
}
16+
}
17+
return count;
18+
}
19+
}
20+

0 commit comments

Comments
 (0)